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

+22
View File
@@ -18,6 +18,7 @@ import { getDriver } from "../drivers";
import type { SendResult } from "../drivers/types";
import type { DispatchFailure, DispatchSummary } from "../queue/delivery";
import { getCheckSuiteBuildLogUrl } from "../github/check-run";
import { getWorkflowRunJobs } from "../github/workflow-jobs";
/** One dispatch attempt (route × target), collected for the group webhook log. */
interface DispatchAttempt {
@@ -163,10 +164,31 @@ 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;
await ensureWorkflowJobs();
const group = route.groupId ? groupById.get(route.groupId) : undefined;
const tr = trMap.get(group?.lang ?? "en")!;
const showEmoji = group?.emoji !== false;
+35
View File
@@ -0,0 +1,35 @@
import { createAppJwt } from "./oauth";
export const GITHUB_API_VERSION = "2022-11-28";
/**
* Mint a short-lived installation access token for a GitHub App installation.
* Returns undefined when the App credentials are missing or the exchange
* fails. Best-effort: callers must not depend on it.
*/
export async function getInstallationToken(
appId: string | undefined,
privateKey: string | undefined,
installationId: number | undefined,
): Promise<string | undefined> {
if (!appId || !privateKey || !installationId) return undefined;
try {
const jwt = await createAppJwt(appId, privateKey);
const res = await fetch(
`https://api.github.com/app/installations/${installationId}/access_tokens`,
{
method: "POST",
headers: {
Authorization: `Bearer ${jwt}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
},
);
if (!res.ok) return undefined;
const { token } = (await res.json()) as { token?: string };
return token ?? undefined;
} catch {
return undefined;
}
}
+3 -17
View File
@@ -1,6 +1,5 @@
import { createAppJwt } from "./oauth";
import { getInstallationToken, GITHUB_API_VERSION } from "./app-token";
const API_VERSION = "2022-11-28";
const CLOUDFLARE_SLUG = "cloudflare-workers-and-pages";
/**
@@ -21,27 +20,14 @@ export async function getCheckSuiteBuildLogUrl(
): Promise<string | undefined> {
if (!checkRunsUrl || !appId || !privateKey || !installationId) return undefined;
try {
const jwt = await createAppJwt(appId, privateKey);
const tokRes = await fetch(
`https://api.github.com/app/installations/${installationId}/access_tokens`,
{
method: "POST",
headers: {
Authorization: `Bearer ${jwt}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": API_VERSION,
},
},
);
if (!tokRes.ok) return undefined;
const { token } = (await tokRes.json()) as { token?: string };
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": API_VERSION,
"X-GitHub-Api-Version": GITHUB_API_VERSION,
},
});
if (!runsRes.ok) return undefined;
+44
View File
@@ -0,0 +1,44 @@
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;
}
}