Files
WebHooker/server/lib/github/check-run.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.7 KiB
TypeScript

import { getInstallationToken, GITHUB_API_VERSION } from "./app-token";
const CLOUDFLARE_SLUG = "cloudflare-workers-and-pages";
/**
* Resolve a check suite's build log URL. The `check_suite` webhook payload
* carries no `details_url` of its own — only the underlying `check_run` does
* (Cloudflare sets it to the build log page). For a failed suite we fetch the
* suite's check runs via the GitHub API and return the first run's
* `details_url`, preferring the Cloudflare run when present.
*
* Returns undefined when the App credentials are missing, the call fails, or
* no run exposes a `details_url`. Best-effort: callers must not depend on it.
*/
export async function getCheckSuiteBuildLogUrl(
checkRunsUrl: string | undefined,
appId: string | undefined,
privateKey: string | undefined,
installationId: number | undefined,
): Promise<string | undefined> {
if (!checkRunsUrl || !appId || !privateKey || !installationId) return undefined;
try {
const token = await getInstallationToken(appId, privateKey, installationId);
if (!token) return undefined;
const runsRes = await fetch(checkRunsUrl, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
});
if (!runsRes.ok) return undefined;
const data = (await runsRes.json()) as {
check_runs?: Array<{ details_url?: string; app?: { slug?: string } }>;
};
const runs = data.check_runs ?? [];
const cloudflare = runs.find((r) => r.app?.slug === CLOUDFLARE_SLUG && r.details_url);
const any = runs.find((r) => r.details_url);
return (cloudflare ?? any)?.details_url;
} catch {
return undefined;
}
}