mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
GitHub rejects API requests without a User-Agent (403), and Workers' fetch does not set one by default, so workflow job enrichment and check-suite build-log lookups silently failed in production.
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { getInstallationToken, GITHUB_API_VERSION, GITHUB_USER_AGENT } 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,
|
|
"User-Agent": GITHUB_USER_AGENT,
|
|
};
|
|
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;
|
|
}
|
|
}
|