diff --git a/containers/backupchecks/src/backend/app/ticketing_utils.py b/containers/backupchecks/src/backend/app/ticketing_utils.py index 4156766..115bbc2 100644 --- a/containers/backupchecks/src/backend/app/ticketing_utils.py +++ b/containers/backupchecks/src/backend/app/ticketing_utils.py @@ -177,7 +177,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: db.session.execute( text( """ - SELECT t.id, t.ticket_code + SELECT t.id, t.ticket_code, t.resolved_at, ts.resolved_at as scope_resolved_at FROM tickets t JOIN ticket_scopes ts ON ts.ticket_id = t.id WHERE ts.job_id = :job_id @@ -193,11 +193,21 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: except Exception: rows = [] + # Debug logging + if rows: + try: + from flask import current_app + current_app.logger.info(f"[TICKET_LINK_DEBUG] Found {len(rows)} open tickets for job_id={job.id}, run_id={run.id}") + for tid, code, t_resolved, ts_resolved in rows: + current_app.logger.info(f" - ticket_id={tid}, code={code}, t.resolved_at={t_resolved}, ts.resolved_at={ts_resolved}") + except Exception: + pass + if not rows: return # Link all open tickets to this run (idempotent) - for tid, _code in rows: + for tid, code, t_resolved, ts_resolved in rows: if not TicketJobRun.query.filter_by(ticket_id=int(tid), job_run_id=int(run.id)).first(): db.session.add(TicketJobRun(ticket_id=int(tid), job_run_id=int(run.id), link_source="inherit")) @@ -213,6 +223,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: # The query above only returns unresolved tickets, so we can safely propagate. try: # Use the newest ticket code to find a matching prior Autotask-linked run. + # rows format: (tid, code, t_resolved, ts_resolved) newest_code = (rows[0][1] or "").strip() if rows else "" if newest_code: prior = ( diff --git a/docs/changelog-claude.md b/docs/changelog-claude.md index cbf1430..9452eb7 100644 --- a/docs/changelog-claude.md +++ b/docs/changelog-claude.md @@ -8,6 +8,9 @@ This file documents all changes made to this project via Claude Code. - Fixed Autotask ticket not being automatically linked to new runs when internal ticket is resolved by implementing independent Autotask propagation strategy (now checks for most recent non-deleted and non-resolved Autotask ticket on job regardless of internal ticket status, ensuring PSA ticket reference persists across runs until explicitly resolved or deleted) - Fixed internal and Autotask tickets being linked to new runs even after being resolved by removing date-based "open" logic from ticket query (tickets now only link to new runs if they are genuinely unresolved, not based on run date comparisons) +### Changed +- Added debug logging to ticket linking function to troubleshoot resolved ticket propagation issues (logs ticket_id, ticket_code, resolved_at values for both ticket and scope) + ## [2026-02-09] ### Added