diff --git a/api/src/routes/public/recentErrors.test.ts b/api/src/routes/public/recentErrors.test.ts new file mode 100644 index 0000000..28cf109 --- /dev/null +++ b/api/src/routes/public/recentErrors.test.ts @@ -0,0 +1,87 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import Fastify, { type FastifyInstance } from 'fastify'; +import type { RowDataPacket } from 'mysql2'; +import recentErrorsPublicRoutes from './recentErrors.js'; + +describe('GET /api/error-monitoring/recent-errors', () => { + const prevResolve = process.env.ERROR_RESOLVE_API_KEY; + + beforeEach(() => { + process.env.ERROR_RESOLVE_API_KEY = 'test-recent-errors-key'; + }); + + afterEach(() => { + if (prevResolve === undefined) { + delete process.env.ERROR_RESOLVE_API_KEY; + } else { + process.env.ERROR_RESOLVE_API_KEY = prevResolve; + } + }); + + async function buildApp(execute: ReturnType): Promise { + const app = Fastify({ logger: false }); + app.decorate('db', { execute } as unknown as FastifyInstance['db']); + await app.register(recentErrorsPublicRoutes); + return app; + } + + it('includes top-level gitea_repo when stored in context_json', async () => { + const execute = vi.fn().mockResolvedValue([ + [ + { + incident_id: '1', + occurrence_id: '99', + project_slug: 'wdmi-store', + incident_severity: 'high', + incident_state: 'open', + message: 'probe', + stack_trace: '', + context_json: JSON.stringify({ + gitea_repo: 'BitReport.pt/WDMI_STORE', + file_path: '/src/x.ts', + line_number: 3, + }), + 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<{ gitea_repo?: string; file_path?: string }> }; + expect(body.data).toHaveLength(1); + expect(body.data[0].gitea_repo).toBe('BitReport.pt/WDMI_STORE'); + expect(body.data[0].file_path).toBe('/src/x.ts'); + }); + + it('omits gitea_repo when not present in context', async () => { + const execute = vi.fn().mockResolvedValue([ + [ + { + incident_id: '1', + occurrence_id: '99', + project_slug: 'other', + incident_severity: 'medium', + incident_state: 'open', + message: 'x', + stack_trace: '', + context_json: JSON.stringify({ file_path: '/b.php' }), + 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<{ gitea_repo?: string }> }; + expect(body.data[0].gitea_repo).toBeUndefined(); + }); +}); diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index d9be4b3..0351447 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -65,6 +65,15 @@ function extractFileLine(ctx: Record | null): { file_path: stri return { file_path, line_number }; } +/** Paperclip error loop reads top-level `gitea_repo` on each recent-error row. */ +function extractGiteaRepo(ctx: Record | null): string { + if (!ctx) { + return ''; + } + const raw = ctx.gitea_repo ?? ctx.giteaRepo ?? ctx.repo_full_name ?? ctx.repository; + return typeof raw === 'string' ? raw.trim() : ''; +} + function parseStatusList(raw: string | undefined): string[] | null { if (raw == null || raw.trim() === '') { return ['open', 'investigating']; @@ -201,6 +210,7 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): const data = (rows as Record[]).map((row) => { 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 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(); @@ -212,7 +222,7 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): const stack_trace = rawStack !== '' ? rawStack : synthesizeStackTraceForDisplay(message, ctx); - return { + const rowOut: Record = { status: st === 'resolved' ? 'resolved' : st === 'ignored' ? 'ignored' : 'open', message, error_message: message, @@ -225,6 +235,10 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): environment: String(row.environment ?? 'production'), occurred_at: toIsoOccurredAt(row.occurred_at), }; + if (gitea_repo !== '') { + rowOut.gitea_repo = gitea_repo; + } + return rowOut; }); return void reply.send({