Files
WebHooker/server/lib/github/workflow-jobs.ts
T
RhenCloud 04352c3a19 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)
2026-09-26 15:33:06 +08:00

45 lines
1.5 KiB
TypeScript

import { getInstallationToken, GITHUB_API_VERSION } from "./app-token";
export interface WorkflowJob {
name?: string;
status?: string;
conclusion?: string | null;
started_at?: string;
completed_at?: string;
}
/**
* Fetch the jobs of a workflow run from its `jobs_url`. The `workflow_run`
* webhook payload only carries `jobs_url`, never the jobs themselves, so this
* enrichment is required for the formatter's Jobs field.
*
* Uses an App installation token when the credentials are available (required
* for private repos); otherwise falls back to an unauthenticated request,
* which works for public repositories at a lower rate limit. Best-effort:
* returns undefined on any failure.
*/
export async function getWorkflowRunJobs(
jobsUrl: string | undefined,
appId: string | undefined,
privateKey: string | undefined,
installationId: number | undefined,
): Promise<WorkflowJob[] | undefined> {
if (!jobsUrl) return undefined;
try {
const token = await getInstallationToken(appId, privateKey, installationId);
const url = new URL(jobsUrl);
url.searchParams.set("per_page", "100");
const headers: Record<string, string> = {
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
};
if (token) headers.Authorization = `Bearer ${token}`;
const res = await fetch(url.toString(), { headers });
if (!res.ok) return undefined;
const data = (await res.json()) as { jobs?: WorkflowJob[] };
return data.jobs?.length ? data.jobs : undefined;
} catch {
return undefined;
}
}