From 3c307ea32d92d72e6f506ebcbb676b1e74f64fe2 Mon Sep 17 00:00:00 2001 From: David Santos Date: Thu, 18 Jun 2026 02:15:49 +0000 Subject: [PATCH] feat(hub): accept and expose paperclip_project_id on error records (BIT-25537) - php-error bridge: accept paperclip_project_id / paperclipProjectId in CrmPhpPayload and promote into event context alongside project_slug/gitea_repo - recent-errors: extract paperclip_project_id from context_json and emit as top-level field so error loop can route issues to the correct project board column without falling back to PAPERCLIP_ERROR_LOOP_PROJECT_ID_BY_SLUG - Add tests for include/omit behaviour (mirrors gitea_repo coverage) - Update ERROR_MONITORING_PUBLIC_API.md to document both optional fields No DB migration required: field is stored in existing context_json column. Co-Authored-By: Paperclip --- .../api/error-monitoring/php-error/route.ts | 5 ++ api/docs/ERROR_MONITORING_PUBLIC_API.md | 4 ++ api/src/routes/public/recentErrors.test.ts | 56 +++++++++++++++++++ api/src/routes/public/recentErrors.ts | 13 +++++ 4 files changed, 78 insertions(+) diff --git a/admin-ui/app/api/error-monitoring/php-error/route.ts b/admin-ui/app/api/error-monitoring/php-error/route.ts index 7a46601..e7583fa 100644 --- a/admin-ui/app/api/error-monitoring/php-error/route.ts +++ b/admin-ui/app/api/error-monitoring/php-error/route.ts @@ -35,6 +35,9 @@ interface CrmPhpPayload { project_slug?: unknown; project_name?: unknown; gitea_repo?: unknown; + // Paperclip project UUID — used by error loop to route issues to the correct project board column + paperclip_project_id?: unknown; + paperclipProjectId?: unknown; } function safeStr(v: unknown): string { @@ -94,9 +97,11 @@ export async function POST(request: NextRequest) { const gitea_repo = safeStr(body.gitea_repo); const project_slug = safeStr(body.project_slug); const project_name = safeStr(body.project_name); + const paperclip_project_id = safeStr(body.paperclip_project_id || body.paperclipProjectId); if (gitea_repo) contextData.gitea_repo = gitea_repo; if (project_slug) contextData.project_slug = project_slug; if (project_name) contextData.project_name = project_name; + if (paperclip_project_id) contextData.paperclip_project_id = paperclip_project_id; // Preserve other top-level diagnostic fields. const file_path = safeStr(body.file_path); diff --git a/api/docs/ERROR_MONITORING_PUBLIC_API.md b/api/docs/ERROR_MONITORING_PUBLIC_API.md index 5ba1c88..20fc031 100644 --- a/api/docs/ERROR_MONITORING_PUBLIC_API.md +++ b/api/docs/ERROR_MONITORING_PUBLIC_API.md @@ -55,6 +55,10 @@ Each row includes CRM-compatible fields. For non-resolved incidents, `status` is **`stack_trace`:** Non-empty for every row: missing client stacks are synthesized on ingest; legacy DB rows with null/blank `stack_trace` get the same synthesis when serializing this endpoint (BIT-1008). +**`gitea_repo`** *(optional)*: Gitea `Owner/Repo` path (e.g. `BitReport.pt/WDMI`) if the reporter sent `gitea_repo` in the error payload. Omitted when not present. + +**`paperclip_project_id`** *(optional)*: Paperclip project UUID sent by the reporter (via `paperclip_project_id` or `paperclipProjectId` payload field). When present, the Paperclip error loop uses this UUID directly to route issues to the correct project board column instead of falling back to the `PAPERCLIP_ERROR_LOOP_PROJECT_ID_BY_SLUG` env map. Omitted when not present. + **Example** ```bash diff --git a/api/src/routes/public/recentErrors.test.ts b/api/src/routes/public/recentErrors.test.ts index eaa141b..841c50a 100644 --- a/api/src/routes/public/recentErrors.test.ts +++ b/api/src/routes/public/recentErrors.test.ts @@ -84,4 +84,60 @@ describe('GET /api/error-monitoring/recent-errors', () => { const body = res.json() as { data: Array<{ gitea_repo?: string }> }; expect(body.data[0].gitea_repo).toBeUndefined(); }); + + it('includes top-level paperclip_project_id when stored in context_json', async () => { + const execute = vi.fn().mockResolvedValue([ + [ + { + incident_id: '2', + occurrence_id: '100', + project_slug: 'crm', + incident_severity: 'high', + incident_state: 'open', + message: 'probe', + stack_trace: '', + context_json: JSON.stringify({ + paperclip_project_id: 'dba3fc26-c728-4e51-a23c-083881a2f534', + }), + environment: 'production', + occurred_at: '2026-01-01T00:00:00.000Z', + } as RowDataPacket, + ], + ]); + const app = await buildApp(execute); + const res = await app.inject({ + method: 'GET', + url: '/api/error-monitoring/recent-errors?api_key=test-recent-errors-key', + }); + expect(res.statusCode).toBe(200); + const body = res.json() as { data: Array<{ paperclip_project_id?: string }> }; + expect(body.data[0].paperclip_project_id).toBe('dba3fc26-c728-4e51-a23c-083881a2f534'); + }); + + it('omits paperclip_project_id when not present in context', async () => { + const execute = vi.fn().mockResolvedValue([ + [ + { + incident_id: '3', + occurrence_id: '101', + project_slug: 'wdmi-store', + incident_severity: 'low', + incident_state: 'open', + message: 'y', + stack_trace: '', + context_json: JSON.stringify({ project_slug: 'wdmi-store' }), + environment: 'production', + occurred_at: '2026-01-01T00:00:00.000Z', + } as RowDataPacket, + ], + ]); + const app = await buildApp(execute); + const res = await app.inject({ + method: 'GET', + url: '/api/error-monitoring/recent-errors?api_key=test-recent-errors-key', + }); + expect(res.statusCode).toBe(200); + const body = res.json() as { data: Array<{ paperclip_project_id?: string }> }; + expect(body.data[0].paperclip_project_id).toBeUndefined(); + }); }); diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index 0351447..dc082da 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -74,6 +74,15 @@ function extractGiteaRepo(ctx: Record | null): string { return typeof raw === 'string' ? raw.trim() : ''; } +/** Paperclip error loop reads top-level `paperclip_project_id` to route issues to the correct project. */ +function extractPaperclipProjectId(ctx: Record | null): string { + if (!ctx) { + return ''; + } + const raw = ctx.paperclip_project_id ?? ctx.paperclipProjectId; + return typeof raw === 'string' ? raw.trim() : ''; +} + function parseStatusList(raw: string | undefined): string[] | null { if (raw == null || raw.trim() === '') { return ['open', 'investigating']; @@ -211,6 +220,7 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): const ctx = parseJsonObject(row.context_json != null ? String(row.context_json) : null); const { file_path, line_number } = extractFileLine(ctx); const gitea_repo = extractGiteaRepo(ctx); + const paperclip_project_id = extractPaperclipProjectId(ctx); const incSev = String(row.incident_severity ?? 'medium').toLowerCase(); const sev = ['critical', 'high', 'medium', 'low'].includes(incSev) ? incSev : 'medium'; const st = String(row.incident_state ?? 'open').toLowerCase(); @@ -238,6 +248,9 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): if (gitea_repo !== '') { rowOut.gitea_repo = gitea_repo; } + if (paperclip_project_id !== '') { + rowOut.paperclip_project_id = paperclip_project_id; + } return rowOut; });