mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(github): send User-Agent header on GitHub API requests
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.
This commit is contained in:
4 files changed
+29
-2
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
import { createAppJwt } from "./oauth";
|
import { createAppJwt } from "./oauth";
|
||||||
|
|
||||||
export const GITHUB_API_VERSION = "2022-11-28";
|
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.
|
* Mint a short-lived installation access token for a GitHub App installation.
|
||||||
@@ -23,6 +24,7 @@ export async function getInstallationToken(
|
|||||||
Authorization: `Bearer ${jwt}`,
|
Authorization: `Bearer ${jwt}`,
|
||||||
Accept: "application/vnd.github+json",
|
Accept: "application/vnd.github+json",
|
||||||
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
||||||
|
"User-Agent": GITHUB_USER_AGENT,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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";
|
const CLOUDFLARE_SLUG = "cloudflare-workers-and-pages";
|
||||||
|
|
||||||
@@ -28,6 +28,7 @@ export async function getCheckSuiteBuildLogUrl(
|
|||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
Accept: "application/vnd.github+json",
|
Accept: "application/vnd.github+json",
|
||||||
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
||||||
|
"User-Agent": GITHUB_USER_AGENT,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!runsRes.ok) return undefined;
|
if (!runsRes.ok) return undefined;
|
||||||
|
|||||||
@@ -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 {
|
export interface WorkflowJob {
|
||||||
name?: string;
|
name?: string;
|
||||||
@@ -32,6 +32,7 @@ export async function getWorkflowRunJobs(
|
|||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
Accept: "application/vnd.github+json",
|
Accept: "application/vnd.github+json",
|
||||||
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
"X-GitHub-Api-Version": GITHUB_API_VERSION,
|
||||||
|
"User-Agent": GITHUB_USER_AGENT,
|
||||||
};
|
};
|
||||||
if (token) headers.Authorization = `Bearer ${token}`;
|
if (token) headers.Authorization = `Bearer ${token}`;
|
||||||
const res = await fetch(url.toString(), { headers });
|
const res = await fetch(url.toString(), { headers });
|
||||||
|
|||||||
@@ -99,4 +99,27 @@ describe("getWorkflowRunJobs", () => {
|
|||||||
globalThis.fetch = (() => Promise.reject(new Error("network"))) as typeof fetch;
|
globalThis.fetch = (() => Promise.reject(new Error("network"))) as typeof fetch;
|
||||||
expect(await getWorkflowRunJobs(JOBS_URL, undefined, undefined, undefined)).toBeUndefined();
|
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<string, string>;
|
||||||
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user