mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
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.
This commit is contained in:
2 files changed
+79
-17
No files matched your search
+18
-17
@@ -68,6 +68,24 @@ export async function dispatchEvent(
|
||||
|
||||
const attempts: DispatchAttempt[] = [];
|
||||
const sendLogs: SendRecord[] = [];
|
||||
|
||||
let workflowJobsPromise: Promise<void> | undefined;
|
||||
function ensureWorkflowJobs(): Promise<void> {
|
||||
if (event.event !== "workflow_run") return Promise.resolve();
|
||||
workflowJobsPromise ??= (async (): Promise<void> => {
|
||||
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<void>[] = [];
|
||||
for (const route of config.routes) {
|
||||
if (!accepted(route)) continue;
|
||||
@@ -164,23 +182,6 @@ export async function dispatchEvent(
|
||||
}
|
||||
}
|
||||
|
||||
let workflowJobsPromise: Promise<void> | undefined;
|
||||
function ensureWorkflowJobs(): Promise<void> {
|
||||
if (event.event !== "workflow_run") return Promise.resolve();
|
||||
workflowJobsPromise ??= (async (): Promise<void> => {
|
||||
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<void> {
|
||||
const targets = route.targets && route.targets.length > 0 ? route.targets : [];
|
||||
if (targets.length === 0) return;
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user