Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(ci): use Cloudflare-compatible GitHub requests

Tin9740bf8402d7
diff --git a/scripts/ui-review-bot/tests/webhook.test.mjs b/scripts/ui-review-bot/tests/webhook.test.mjs
index 63f0c07ea..202fdec17 100644
--- a/scripts/ui-review-bot/tests/webhook.test.mjs
+++ b/scripts/ui-review-bot/tests/webhook.test.mjs
@@ -174,3 +174,50 @@ test("Actions rechecks dispatched comment authors and rejects spoofed dispatcher
false,
);
});
+
+test("default Worker path signs an App JWT and uses an edge-compatible redirect policy", async (t) => {
+ const s = setup();
+ const keys = await crypto.subtle.generateKey(
+ {
+ name: "RSASSA-PKCS1-v1_5",
+ modulusLength: 2048,
+ publicExponent: new Uint8Array([1, 0, 1]),
+ hash: "SHA-256",
+ },
+ true,
+ ["sign", "verify"],
+ );
+ const der = await crypto.subtle.exportKey("pkcs8", keys.privateKey);
+ s.env.APP_PRIVATE_KEY = `-----BEGIN PRIVATE KEY-----\n${Buffer.from(der).toString("base64")}\n-----END PRIVATE KEY-----`;
+ s.env.APP_CLIENT_ID = "test-app";
+ t.mock.method(globalThis, "fetch", async (url, options) => {
+ assert.equal(options.redirect, "manual");
+ const endpoint = String(url).replace("https://api.github.com/", "");
+ if (endpoint.endsWith("/access_tokens")) {
+ const jwt = options.headers.Authorization.slice(7).split(".");
+ assert.equal(
+ await crypto.subtle.verify(
+ "RSASSA-PKCS1-v1_5",
+ keys.publicKey,
+ Buffer.from(jwt[2], "base64url"),
+ Buffer.from(jwt.slice(0, 2).join(".")),
+ ),
+ true,
+ );
+ return Response.json({ token: "test-installation-token" });
+ }
+ assert.equal(
+ options.headers.Authorization,
+ "Bearer test-installation-token",
+ );
+ const result = await s.dependencies.api(
+ endpoint,
+ options.body ? JSON.parse(options.body) : undefined,
+ );
+ return result === null
+ ? new Response(null, { status: 204 })
+ : Response.json(result);
+ });
+ assert.equal((await handleWebhook(request(), s.env)).status, 200);
+ assert.equal(s.writes.length, 1);
+});
diff --git a/scripts/ui-review-bot/worker/index.mjs b/scripts/ui-review-bot/worker/index.mjs
index 7674c031b..8ef0c79f3 100644
--- a/scripts/ui-review-bot/worker/index.mjs
+++ b/scripts/ui-review-bot/worker/index.mjs
@@ -79,7 +79,7 @@ async function github(endpoint, token, signal, body) {
const result = await fetch(`https://api.github.com/${endpoint}`, {
method: body ? "POST" : "GET",
signal,
- redirect: "error",
+ redirect: "manual",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
@@ -139,6 +139,7 @@ export async function handleWebhook(request, env, dependencies = {}) {
return response(403, "wrong_installation");
const signal = AbortSignal.timeout(8_000);
let claimed = false;
+ let stage = "token";
try {
const token = await (
dependencies.token ||
@@ -158,8 +159,10 @@ export async function handleWebhook(request, env, dependencies = {}) {
const api =
dependencies.api ||
((endpoint, body) => github(endpoint, token, signal, body));
+ stage = "authorize";
const decision = await authorizeRequest(eventName, event, api);
if (!decision.allowed) return response(200, "ignored");
+ stage = "claim";
const claim = await env.DB.prepare(
"INSERT OR IGNORE INTO commands (comment_id, created_at) VALUES (?, ?)",
)
@@ -167,6 +170,7 @@ export async function handleWebhook(request, env, dependencies = {}) {
.run();
if (!claim.meta.changes) return response(200, "duplicate");
claimed = true;
+ stage = "dispatch";
await api(`repos/${REPO}/dispatches`, {
event_type: "peekareq",
client_payload: { pr: decision.pr, comment_id: event.comment.id },
@@ -177,6 +181,8 @@ export async function handleWebhook(request, env, dependencies = {}) {
console.error(
JSON.stringify({
event: "webhook_failed",
+ stage,
+ error_type: error.name,
comment_id: event.comment.id,
claimed,
code: /^github_\d+$/.test(error.message)
@@ -188,4 +194,4 @@ export async function handleWebhook(request, env, dependencies = {}) {
}
}
-export default { fetch: handleWebhook };
+export default { fetch: (request, env) => handleWebhook(request, env) };