diff --git a/bun.lockb b/bun.lockb index 2fd9eaf..c1d8480 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 99ff304..b8faff9 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "smee-client": "^2.0.0" }, "devDependencies": { - "@cloudflare/workers-types": "^4.20231121.0", + "@cloudflare/workers-types": "^4.20240117.0", "@commitlint/cli": "^18.4.3", "@commitlint/config-conventional": "^18.4.3", "@types/node": "^20.11.1", @@ -53,7 +53,7 @@ "prettier": "^3.1.0", "tsx": "^4.6.2", "typescript": "^5.0.4", - "wrangler": "^3.22.3" + "wrangler": "^3.23.0" }, "lint-staged": { "*.ts": [ diff --git a/src/types/env.ts b/src/types/env.ts new file mode 100644 index 0000000..3da7b37 --- /dev/null +++ b/src/types/env.ts @@ -0,0 +1,4 @@ +import { Type as T, type Static } from "@sinclair/typebox"; + +export const envSchema = T.Object({ WEBHOOK_SECRET: T.String() }); +export type Env = Static; diff --git a/src/webhooks.ts b/src/webhooks.ts deleted file mode 100644 index 9de661e..0000000 --- a/src/webhooks.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { EmitterWebhookEvent } from "@octokit/webhooks"; -import { makeEventKey } from "./webhooks/make-event-key"; - -export async function handleEvent(event: EmitterWebhookEvent) { - const eventKey = makeEventKey(event); - - console.log(eventKey); - console.log(); - console.log(event); - console.log(); - console.log(eventKey); - - switch (eventKey) { - case "issues.opened": - return notImplemented(event); - case "issues.closed": - return notImplemented(event); - case "issues.reopened": - return notImplemented(event); - case "pull_request.opened": - return notImplemented(event); - default: - return notImplemented(event); - } -} - -function notImplemented(event: EmitterWebhookEvent) { - console.log(`Not implemented: ${makeEventKey(event)}`); -} diff --git a/src/worker.ts b/src/worker.ts index b76ec67..2fcef8c 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,11 +1,9 @@ import { EmitterWebhookEventName as WebhookEventName, emitterEventNames } from "@octokit/webhooks"; -import { Type as T, type Static } from "@sinclair/typebox"; import { Value } from "@sinclair/typebox/value"; - import { EventHandler } from "./event-handler"; import { bindHandlers } from "./handlers"; -const envSchema = T.Object({ WEBHOOK_SECRET: T.String() }); -type Env = Static; +import { envSchema, Env } from "./types/env"; + export default { async fetch(request: Request, env: Env): Promise { if (!Value.Check(envSchema, env)) { @@ -16,6 +14,7 @@ export default { headers: { "content-type": "application/json" }, }); } + let pathname; try { pathname = new URL(request.url, "http://localhost").pathname; @@ -25,18 +24,21 @@ export default { headers: { "content-type": "application/json" }, }); } + if (pathname !== "/events" || request.method !== "POST") { return new Response(JSON.stringify({ error: `Unknown route: ${request.method} ${request.url}` }), { status: 400, headers: { "content-type": "application/json" }, }); } + if (!request.headers.get("content-type") || !request.headers.get("content-type")?.startsWith("application/json")) { return new Response(JSON.stringify({ error: `Unsupported "Content-Type" header value. Must be "application/json"` }), { status: 415, headers: { "content-type": "application/json" }, }); } + const eventName = request.headers.get("x-github-event"); if (!eventName) { return new Response(JSON.stringify({ error: `Missing "x-github-event" header` }), { @@ -44,6 +46,7 @@ export default { headers: { "content-type": "application/json" }, }); } + const signatureSHA256 = request.headers.get("x-hub-signature-256"); if (!signatureSHA256) { return new Response(JSON.stringify({ error: `Missing "x-hub-signature-256" header` }), { @@ -51,6 +54,7 @@ export default { headers: { "content-type": "application/json" }, }); } + const id = request.headers.get("x-github-delivery"); if (!id) { return new Response(JSON.stringify({ error: `Missing "x-github-delivery" header` }), { @@ -58,6 +62,7 @@ export default { headers: { "content-type": "application/json" }, }); } + if (!emitterEventNames.includes(eventName as WebhookEventName)) { return new Response(JSON.stringify({ error: `Unsupported "x-github-event" header value: ${eventName}` }), { status: 400,