Auto-commit local changes before build (2026-01-06 20:04:54)

This commit is contained in:
Ivo Oskamp 2026-01-06 20:04:54 +01:00
parent af0faa37f8
commit 92716dc13c
3 changed files with 73 additions and 34 deletions

View File

@ -1 +1 @@
backupchecks-v20260106-21-changelog-0.1.17 v20260106-22-ticket-link-multiple-jobs

View File

@ -204,43 +204,77 @@ def api_tickets():
if not re.match(r"^T\d{8}\.\d{4}$", ticket_code): if not re.match(r"^T\d{8}\.\d{4}$", ticket_code):
return jsonify({"status": "error", "message": "Invalid ticket_code format. Expected TYYYYMMDD.####."}), 400 return jsonify({"status": "error", "message": "Invalid ticket_code format. Expected TYYYYMMDD.####."}), 400
# Ensure uniqueness existing = Ticket.query.filter_by(ticket_code=ticket_code).first()
if Ticket.query.filter_by(ticket_code=ticket_code).first():
return jsonify({"status": "error", "message": "ticket_code already exists."}), 409
ticket = Ticket( # If the ticket already exists, link it to this job/run (a single ticket number can apply to multiple jobs).
ticket_code=ticket_code, if existing:
title=None, ticket = existing
description=description, try:
active_from_date=_to_amsterdam_date(run.run_at) or _to_amsterdam_date(now) or now.date(), # Ensure there is at least one job scope for this ticket/job combination.
start_date=now, if job and job.id:
resolved_at=None, scope = TicketScope.query.filter_by(
) ticket_id=ticket.id,
scope_type="job",
job_id=job.id,
).first()
if not scope:
scope = TicketScope(
ticket_id=ticket.id,
scope_type="job",
customer_id=job.customer_id,
backup_software=job.backup_software,
backup_type=job.backup_type,
job_id=job.id,
job_name_match=job.job_name,
job_name_match_mode="exact",
)
db.session.add(scope)
try: # Link the ticket to this specific run (idempotent due to unique constraint).
db.session.add(ticket) existing_link = TicketJobRun.query.filter_by(ticket_id=ticket.id, job_run_id=run.id).first()
db.session.flush() if not existing_link:
link = TicketJobRun(ticket_id=ticket.id, job_run_id=run.id, link_source="manual")
db.session.add(link)
# Minimal scope from job db.session.commit()
scope = TicketScope( except Exception as exc:
ticket_id=ticket.id, db.session.rollback()
scope_type="job", return jsonify({"status": "error", "message": str(exc) or "Failed to link ticket."}), 500
customer_id=job.customer_id if job else None,
backup_software=job.backup_software if job else None, else:
backup_type=job.backup_type if job else None, ticket = Ticket(
job_id=job.id if job else None, ticket_code=ticket_code,
job_name_match=job.job_name if job else None, title=None,
job_name_match_mode="exact", description=description,
active_from_date=_to_amsterdam_date(run.run_at) or _to_amsterdam_date(now) or now.date(),
start_date=now,
resolved_at=None,
) )
db.session.add(scope)
link = TicketJobRun(ticket_id=ticket.id, job_run_id=run.id, link_source="manual") try:
db.session.add(link) db.session.add(ticket)
db.session.flush()
db.session.commit() # Minimal scope from job
except Exception as exc: scope = TicketScope(
db.session.rollback() ticket_id=ticket.id,
return jsonify({"status": "error", "message": str(exc) or "Failed to create ticket."}), 500 scope_type="job",
customer_id=job.customer_id if job else None,
backup_software=job.backup_software if job else None,
backup_type=job.backup_type if job else None,
job_id=job.id if job else None,
job_name_match=job.job_name if job else None,
job_name_match_mode="exact",
)
db.session.add(scope)
link = TicketJobRun(ticket_id=ticket.id, job_run_id=run.id, link_source="manual")
db.session.add(link)
db.session.commit()
except Exception as exc:
db.session.rollback()
return jsonify({"status": "error", "message": str(exc) or "Failed to create ticket."}), 500
return jsonify( return jsonify(
{ {
@ -251,8 +285,8 @@ def api_tickets():
"description": ticket.description or "", "description": ticket.description or "",
"start_date": _format_datetime(ticket.start_date), "start_date": _format_datetime(ticket.start_date),
"active_from_date": str(ticket.active_from_date) if getattr(ticket, "active_from_date", None) else "", "active_from_date": str(ticket.active_from_date) if getattr(ticket, "active_from_date", None) else "",
"resolved_at": "", "resolved_at": _format_datetime(ticket.resolved_at) if getattr(ticket, "resolved_at", None) else "",
"active": True, "active": getattr(ticket, "resolved_at", None) is None,
}, },
} }
) )

View File

@ -1,3 +1,8 @@
## v20260106-22-ticket-link-multiple-jobs
- Fixed ticket linking logic to allow the same existing ticket number to be associated with multiple jobs and job runs.
- Prevented duplicate ticket creation errors when reusing an existing ticket_code.
- Ensured tickets are reused and linked instead of rejected when already present in the system.
================================================================================================================================================ ================================================================================================================================================