|
| 1 | +import { timingSafeEqual } from "node:crypto"; |
| 2 | +import { defineEventHandler, getHeader, getRouterParam, createError } from "h3"; |
| 3 | +import { env } from "../../../env.js"; |
| 4 | +import { createAdapters } from "../../lib/adapters.js"; |
| 5 | +import { logger } from "../../lib/logger.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns the current run state for a ticket, used by the Forge issue panel. |
| 9 | + * |
| 10 | + * Shape is intentionally narrow — only what the panel renders. Phase / PR URL |
| 11 | + * are best-effort: the run registry tracks runId + sandboxId only, so richer |
| 12 | + * state would require either expanding the registry or reading from VCS. |
| 13 | + */ |
| 14 | +export default defineEventHandler(async (event) => { |
| 15 | + if (!env.FORGE_SHARED_SECRET) { |
| 16 | + throw createError({ statusCode: 503, statusMessage: "FORGE_SHARED_SECRET not configured" }); |
| 17 | + } |
| 18 | + |
| 19 | + verifyForgeSecret(getHeader(event, "x-forge-secret")); |
| 20 | + |
| 21 | + const issueKey = getRouterParam(event, "key"); |
| 22 | + if (!issueKey) { |
| 23 | + throw createError({ statusCode: 400, statusMessage: "Missing issueKey" }); |
| 24 | + } |
| 25 | + |
| 26 | + const adapters = createAdapters(); |
| 27 | + const runId = await adapters.runRegistry.getRunId(issueKey); |
| 28 | + |
| 29 | + if (!runId) { |
| 30 | + const failed = await adapters.runRegistry.isTicketFailed(issueKey).catch(() => false); |
| 31 | + if (failed) { |
| 32 | + return { status: "failed", issueKey }; |
| 33 | + } |
| 34 | + setResponseStatus(event, 404); |
| 35 | + return { status: "idle", issueKey }; |
| 36 | + } |
| 37 | + |
| 38 | + const sandboxId = await adapters.runRegistry.getSandboxId(issueKey).catch(() => null); |
| 39 | + |
| 40 | + // Branch convention is set in agent.ts: `blazebot/{ticketKey-lowercase}`. |
| 41 | + // Mirror it here so the panel can show a PR link without expanding the |
| 42 | + // run registry schema. |
| 43 | + let prUrl: string | null = null; |
| 44 | + try { |
| 45 | + const branchName = `blazebot/${issueKey.toLowerCase()}`; |
| 46 | + const pr = await adapters.vcs.findPR(branchName); |
| 47 | + prUrl = pr?.url ?? null; |
| 48 | + } catch (err) { |
| 49 | + logger.debug({ issueKey, error: (err as Error).message }, "forge_runs_pr_lookup_failed"); |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + status: "active", |
| 54 | + issueKey, |
| 55 | + runId, |
| 56 | + sandboxId, |
| 57 | + prUrl, |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +function verifyForgeSecret(received: string | undefined): void { |
| 62 | + if (!received) { |
| 63 | + throw createError({ statusCode: 401, statusMessage: "Missing X-Forge-Secret header" }); |
| 64 | + } |
| 65 | + const expected = env.FORGE_SHARED_SECRET!; |
| 66 | + const a = Buffer.from(received); |
| 67 | + const b = Buffer.from(expected); |
| 68 | + if (a.length !== b.length || !timingSafeEqual(a, b)) { |
| 69 | + throw createError({ statusCode: 401, statusMessage: "Invalid forge secret" }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function setResponseStatus(event: Parameters<typeof getHeader>[0], status: number): void { |
| 74 | + // h3 has setResponseStatus but importing it here to keep the file's imports |
| 75 | + // explicit and self-contained. |
| 76 | + (event.node?.res ?? (event as any).res).statusCode = status; |
| 77 | +} |
0 commit comments