From 027ca2bcca93540717b697980b2e2afd4ad30d35 Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Sat, 26 Sep 2026 15:49:25 +0800 Subject: [PATCH] fix(dispatch): hoist workflow jobs memoization above route loop The workflow_jobsPromise declaration sat after the route dispatch loop, so processRoute hit a temporal dead zone ReferenceError for workflow_run events; Promise.allSettled swallowed it and no message was sent. Move the memoized enrichment above the loop and add a regression test that dispatches a workflow_run event with jobs enrichment. --- server/lib/core/dispatch.ts | 35 ++++++++++----------- tests/discord.test.ts | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/server/lib/core/dispatch.ts b/server/lib/core/dispatch.ts index 0289e59..be2e87e 100644 --- a/server/lib/core/dispatch.ts +++ b/server/lib/core/dispatch.ts @@ -68,6 +68,24 @@ export async function dispatchEvent( const attempts: DispatchAttempt[] = []; const sendLogs: SendRecord[] = []; + + let workflowJobsPromise: Promise | undefined; + function ensureWorkflowJobs(): Promise { + if (event.event !== "workflow_run") return Promise.resolve(); + workflowJobsPromise ??= (async (): Promise => { + const run = event.payload.workflow_run as { jobs_url?: string; jobs?: unknown[] } | undefined; + if (!run?.jobs_url || run.jobs?.length) return; + const jobs = await getWorkflowRunJobs( + run.jobs_url, + env.GITHUB_APP_ID, + env.GITHUB_PRIVATE_KEY, + event.installationId, + ); + if (jobs) run.jobs = jobs; + })(); + return workflowJobsPromise; + } + const tasks: Promise[] = []; for (const route of config.routes) { if (!accepted(route)) continue; @@ -164,23 +182,6 @@ export async function dispatchEvent( } } - let workflowJobsPromise: Promise | undefined; - function ensureWorkflowJobs(): Promise { - if (event.event !== "workflow_run") return Promise.resolve(); - workflowJobsPromise ??= (async (): Promise => { - const run = event.payload.workflow_run as { jobs_url?: string; jobs?: unknown[] } | undefined; - if (!run?.jobs_url || run.jobs?.length) return; - const jobs = await getWorkflowRunJobs( - run.jobs_url, - env.GITHUB_APP_ID, - env.GITHUB_PRIVATE_KEY, - event.installationId, - ); - if (jobs) run.jobs = jobs; - })(); - return workflowJobsPromise; - } - async function processRoute(route: Route): Promise { const targets = route.targets && route.targets.length > 0 ? route.targets : []; if (targets.length === 0) return; diff --git a/tests/discord.test.ts b/tests/discord.test.ts index 24ac52f..a8a4545 100644 --- a/tests/discord.test.ts +++ b/tests/discord.test.ts @@ -552,4 +552,65 @@ describe("dispatchEvent fallback routing", () => { }); expect(titles.sort()).toEqual(["owner/repo", "owner/repo"].sort()); }); + + it("dispatches workflow_run events and enriches them with jobs", async () => { + const sent: string[] = []; + const bodies: string[] = []; + mockFetch((url, init) => { + if (url.includes("api.github.com") && url.includes("/jobs")) { + return Response.json({ + jobs: [ + { + name: "build", + status: "completed", + conclusion: "success", + started_at: "2026-01-01T00:00:00Z", + completed_at: "2026-01-01T00:01:30Z", + }, + ], + }); + } + sent.push(url); + bodies.push(String(init?.body ?? "")); + return new Response("{}", { status: 200 }); + }); + const env = createEnv({ KV: createMockKV(), DB: createMockDB() }); + const routes: Route[] = [ + { + id: "wf-route", + name: "Workflow", + enabled: true, + filters: [{ type: "event", match: "workflow_run" }], + targets: [{ channelId: "111" }], + }, + ]; + + const summary = await dispatchEvent( + { ...baseConfig, routes }, + { + event: "workflow_run", + payload: { + action: "completed", + workflow_run: { + id: 42, + name: "CI", + conclusion: "success", + status: "completed", + html_url: "https://github.com/owner/repo/actions/runs/42", + head_branch: "main", + run_number: 7, + jobs_url: "https://api.github.com/repos/owner/repo/actions/runs/42/jobs", + }, + repository: { full_name: "owner/repo" }, + sender: { login: "octocat" }, + }, + }, + env, + ); + + expect(summary.attempts).toBe(1); + expect(sent.filter((u) => u.includes("/111/"))).toHaveLength(1); + expect(bodies[0]).toContain("```diff"); + expect(bodies[0]).toContain("build ✅ success · 1m 30s"); + }); });