feat(github): fetch workflow_run jobs via jobs_url enrichment

The workflow_run webhook payload carries no jobs array, only jobs_url,
so the Jobs field never populated for real GitHub events. Fetch the job
list from the GitHub API once per delivery (lazy, memoized across
routes) and inject it into the payload before formatting.

- app-token.ts: shared getInstallationToken (App JWT -> installation
  token) + GITHUB_API_VERSION; check-run.ts now reuses it
- workflow-jobs.ts: getWorkflowRunJobs, App token when configured else
  unauthenticated (public repos), per_page=100, best-effort
- docs: GitHub App needs Actions: read; note API enrichment in the
  message-format and supported-events guides (en + zh)
This commit is contained in:
RhenCloud committed 2026-09-26 15:33:06 +08:00
1 parent ae36c22fda
commit 04352c3a19
12 files changed
+223 -23

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
import { describe, it, expect, afterEach } from "bun:test";
import { getWorkflowRunJobs } from "../server/lib/github/workflow-jobs";
function bufToPem(buf: ArrayBuffer): string {
const bytes = new Uint8Array(buf);
let s = "";
for (const b of bytes) s += String.fromCharCode(b);
const b64 = btoa(s);
return `-----BEGIN PRIVATE KEY-----\n${b64.replace(/(.{64})/g, "$1\n")}\n-----END PRIVATE KEY-----`;
}
async function makeAppKeyPair(): Promise<{ appId: string; pem: string }> {
const pair = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"],
);
const pkcs8 = await crypto.subtle.exportKey("pkcs8", pair.privateKey);
return { appId: "12345", pem: bufToPem(pkcs8) };
}
const JOBS_URL = "https://api.github.com/repos/acme/widget/actions/runs/42/jobs";
describe("getWorkflowRunJobs", () => {
const restoredFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = restoredFetch;
});
function mockFetch(
handler: (url: string, init?: RequestInit) => Response | Promise<Response>,
): string[] {
const urls: string[] = [];
globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
urls.push(url);
return Promise.resolve(handler(url, init));
}) as typeof fetch;
return urls;
}
it("mints an installation token then lists the jobs", async () => {
const { appId, pem } = await makeAppKeyPair();
const jobs = [{ name: "build", status: "completed", conclusion: "success" }];
const urls = mockFetch((url) => {
if (url.includes("/access_tokens")) {
return new Response(JSON.stringify({ token: "ghs_test" }), { status: 201 });
}
return new Response(JSON.stringify({ jobs }), { status: 200 });
});
const result = await getWorkflowRunJobs(JOBS_URL, appId, pem, 555);
expect(result).toEqual(jobs);
expect(urls[0]).toContain("/app/installations/555/access_tokens");
expect(urls[1]).toContain(JOBS_URL);
expect(urls[1]).toContain("per_page=100");
});
it("falls back to an unauthenticated request without App credentials", async () => {
const jobs = [{ name: "test", status: "in_progress" }];
let sawAuth: string | undefined;
mockFetch((_url, init) => {
sawAuth = (init?.headers as Record<string, string> | undefined)?.Authorization;
return new Response(JSON.stringify({ jobs }), { status: 200 });
});
const result = await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined);
expect(result).toEqual(jobs);
expect(sawAuth).toBeUndefined();
});
it("returns undefined when jobs_url is missing", async () => {
let called = false;
mockFetch(() => {
called = true;
return new Response("{}", { status: 200 });
});
expect(await getWorkflowRunJobs(undefined, undefined, undefined, undefined)).toBeUndefined();
expect(called).toBe(false);
});
it("returns undefined when the API responds with an error", async () => {
mockFetch(() => new Response("{}", { status: 403 }));
expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined();
});
it("returns undefined for an empty jobs list", async () => {
mockFetch(() => new Response(JSON.stringify({ jobs: [] }), { status: 200 }));
expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined();
});
it("returns undefined when the request throws", async () => {
globalThis.fetch = (() => Promise.reject(new Error("network"))) as typeof fetch;
expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined();
});
});