diff --git a/server/lib/github/app-token.ts b/server/lib/github/app-token.ts index f473ad2..fc1ccd7 100644 --- a/server/lib/github/app-token.ts +++ b/server/lib/github/app-token.ts @@ -1,6 +1,7 @@ import { createAppJwt } from "./oauth"; export const GITHUB_API_VERSION = "2022-11-28"; +export const GITHUB_USER_AGENT = "WebHooker (https://github.com/ReCloudStudio/WebHooker)"; /** * Mint a short-lived installation access token for a GitHub App installation. @@ -23,6 +24,7 @@ export async function getInstallationToken( Authorization: `Bearer ${jwt}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION, + "User-Agent": GITHUB_USER_AGENT, }, }, ); diff --git a/server/lib/github/check-run.ts b/server/lib/github/check-run.ts index 8b1cdba..bca6210 100644 --- a/server/lib/github/check-run.ts +++ b/server/lib/github/check-run.ts @@ -1,4 +1,4 @@ -import { getInstallationToken, GITHUB_API_VERSION } from "./app-token"; +import { getInstallationToken, GITHUB_API_VERSION, GITHUB_USER_AGENT } from "./app-token"; const CLOUDFLARE_SLUG = "cloudflare-workers-and-pages"; @@ -28,6 +28,7 @@ export async function getCheckSuiteBuildLogUrl( Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION, + "User-Agent": GITHUB_USER_AGENT, }, }); if (!runsRes.ok) return undefined; diff --git a/server/lib/github/workflow-jobs.ts b/server/lib/github/workflow-jobs.ts index 87a01ad..a1f4ba3 100644 --- a/server/lib/github/workflow-jobs.ts +++ b/server/lib/github/workflow-jobs.ts @@ -1,4 +1,4 @@ -import { getInstallationToken, GITHUB_API_VERSION } from "./app-token"; +import { getInstallationToken, GITHUB_API_VERSION, GITHUB_USER_AGENT } from "./app-token"; export interface WorkflowJob { name?: string; @@ -32,6 +32,7 @@ export async function getWorkflowRunJobs( const headers: Record = { 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 }); diff --git a/tests/github-workflow-jobs.test.ts b/tests/github-workflow-jobs.test.ts index d80bcf3..69a5e99 100644 --- a/tests/github-workflow-jobs.test.ts +++ b/tests/github-workflow-jobs.test.ts @@ -99,4 +99,27 @@ describe("getWorkflowRunJobs", () => { globalThis.fetch = (() => Promise.reject(new Error("network"))) as typeof fetch; expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined(); }); + + it("always sends a User-Agent header (GitHub rejects requests without one)", async () => { + let sawUA: string | undefined; + let tokenUA: string | undefined; + const { appId, pem } = await makeAppKeyPair(); + mockFetch((url, init) => { + const headers = (init?.headers ?? {}) as Record; + if (url.includes("/access_tokens")) tokenUA = headers["User-Agent"]; + else sawUA = headers["User-Agent"]; + return new Response( + JSON.stringify( + url.includes("/access_tokens") + ? { token: "ghs_test" } + : { jobs: [{ name: "build" }] }, + ), + { status: 200 }, + ); + }); + + expect(await getWorkflowRunJobs(JOBS_URL, appId, pem, 555)).toBeDefined(); + expect(tokenUA).toBeTruthy(); + expect(sawUA).toBeTruthy(); + }); });