Auto-commit local changes before build (2026-01-19 15:59:26)

This commit is contained in:
Ivo Oskamp 2026-01-19 15:59:26 +01:00
parent 4b3b6162a0
commit b56cdacf6b
2 changed files with 50 additions and 3 deletions

View File

@ -1 +1 @@
v20260119-16-fix-runchecks-render-modal v20260119-17-fix-autotask-postcreate-ticketnumber-internal-linking

View File

@ -1331,15 +1331,50 @@ def api_run_checks_create_autotask_ticket():
if not ticket_id: if not ticket_id:
return jsonify({"status": "error", "message": "Autotask did not return a ticket id."}), 400 return jsonify({"status": "error", "message": "Autotask did not return a ticket id."}), 400
# Mandatory post-create retrieval: create response does not reliably include Ticket Number.
ticket_number_str = (str(ticket_number).strip() if ticket_number is not None else "").strip()
try:
if not ticket_number_str:
fetched = client.get_ticket(int(ticket_id))
if isinstance(fetched, dict):
ticket_number_str = (
str(fetched.get("ticketNumber") or fetched.get("number") or fetched.get("ticket_number") or "").strip()
)
except Exception:
ticket_number_str = ticket_number_str
now = datetime.utcnow()
try: try:
run.autotask_ticket_id = int(ticket_id) run.autotask_ticket_id = int(ticket_id)
except Exception: except Exception:
run.autotask_ticket_id = None run.autotask_ticket_id = None
run.autotask_ticket_number = (str(ticket_number).strip() if ticket_number is not None else "") or None run.autotask_ticket_number = (ticket_number_str or "") or None
run.autotask_ticket_created_at = datetime.utcnow() run.autotask_ticket_created_at = now
run.autotask_ticket_created_by_user_id = current_user.id run.autotask_ticket_created_by_user_id = current_user.id
# Propagate linkage to all active (unreviewed) runs of the same job.
active_runs: list[JobRun] = []
try:
active_runs = JobRun.query.filter(JobRun.job_id == job.id, JobRun.reviewed_at.is_(None)).all()
except Exception:
active_runs = [run]
run_ids: list[int] = []
for rr in active_runs or []:
if getattr(rr, "id", None):
run_ids.append(int(rr.id))
if getattr(rr, "autotask_ticket_id", None) is None:
rr.autotask_ticket_id = int(ticket_id)
if ticket_number_str and not (getattr(rr, "autotask_ticket_number", None) or "").strip():
rr.autotask_ticket_number = ticket_number_str
db.session.add(rr)
# Create/repair internal Ticket + TicketJobRun links (legacy UI compatibility).
if ticket_number_str:
_ensure_internal_ticket_for_autotask(ticket_number=ticket_number_str, job=job, run_ids=run_ids, now=now)
try: try:
db.session.add(run) db.session.add(run)
db.session.commit() db.session.commit()
@ -1347,6 +1382,18 @@ def api_run_checks_create_autotask_ticket():
db.session.rollback() db.session.rollback()
return jsonify({"status": "error", "message": f"Failed to store ticket reference: {exc}"}), 500 return jsonify({"status": "error", "message": f"Failed to store ticket reference: {exc}"}), 500
# If Ticket Number is still unknown, surface that explicitly (ticket id is still stored).
if not (run.autotask_ticket_number or "").strip():
return jsonify(
{
"status": "ok",
"ticket_id": int(run.autotask_ticket_id) if run.autotask_ticket_id else None,
"ticket_number": "",
"already_exists": False,
"warning": "Ticket created, but ticket number could not be retrieved.",
}
)
return jsonify( return jsonify(
{ {
"status": "ok", "status": "ok",