feat(api): expose gitea_repo on recent-errors rows (BIT-1291)

Extract gitea_repo from context_json and include it as a top-level field
on each row returned by GET /api/error-monitoring/recent-errors.
Supports gitea_repo, giteaRepo, repo_full_name, and repository keys.

Added unit tests covering the include and omit cases.

Paperclip-Issue: BIT-1291
Co-Authored-By: Paperclip <[email protected]>
This commit is contained in:
David SantosandPaperclip committed 2026-04-08 00:48:43 +00:00
1 parent f8c401a990
commit 17781da9ec
2 files changed
+102 -1

No files matched your search

@@ -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<typeof vi.fn>): Promise<FastifyInstance> {
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();
});
});
+15 -1
View File
@@ -65,6 +65,15 @@ function extractFileLine(ctx: Record<string, unknown> | 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<string, unknown> | 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<string, unknown>[]).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<string, unknown> = {
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({