From 06fc88c19cddcf5a75f1ee5b26b58645900e53be Mon Sep 17 00:00:00 2001 From: whilefoo Date: Sun, 21 Jan 2024 16:04:41 +0100 Subject: [PATCH] fix: move env to types, update packages, remove unused functions --- bun.lockb | Bin 264571 -> 264571 bytes package.json | 4 ++-- src/types/env.ts | 4 ++++ src/webhooks.ts | 29 ----------------------------- src/worker.ts | 13 +++++++++---- 5 files changed, 15 insertions(+), 35 deletions(-) create mode 100644 src/types/env.ts delete mode 100644 src/webhooks.ts diff --git a/bun.lockb b/bun.lockb index 2fd9eaf9e9bda1a9192e680a289a7362d1eab9ec..c1d84809f64cffbc0c33148df2c1bf4af0be75fc 100755 GIT binary patch delta 263 zcmV+i0r>v=k`Vin5Rfh)J7m%X5baum;G;+Q!YExX_xnHek)?Lz9a*CkrL?o=u}SCb#t$0eI3t9R)m; zm|7)Re1QeUwNN{ioE_2v=k`Vin5Rfh)^W@zk7MKB}4Z_X7D8-wufxYSuVWQ?jG7Vf+-qu}o@`a&hT`t-y_GgE{wulJ<%BJWyu0AZv`_kF)}f?m^cF}7XvabHMhl20~5Ig NGchtTIG5cB1fC; 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,