Skip to content

Commit

Permalink
Merge pull request #11 from ubiquity/whilefoo/event-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Jan 18, 2024
2 parents 1060e38 + 1056cac commit 28c8e73
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 254 deletions.
21 changes: 21 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks";

export class Context<T extends WebhookEventName = WebhookEventName> {
public key: WebhookEventName;
public name: WebhookEventName;
public id: string;
public payload: WebhookEvent<T>["payload"];

constructor(event: WebhookEvent<T>) {
this.name = event.name;
this.id = event.id;
this.payload = event.payload;
if ("action" in this.payload) {
this.key = `${this.name}.${this.payload.action}` as WebhookEventName;
} else {
this.key = this.name;
}
}
}

export type SimplifiedContext = Omit<Context, keyof WebhookEventName>;
28 changes: 28 additions & 0 deletions src/event-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Webhooks } from "@octokit/webhooks";
import { Context, SimplifiedContext } from "./context";

export class EventHandler {
public webhooks: Webhooks<SimplifiedContext>;
public on: Webhooks<SimplifiedContext>["on"];
public onAny: Webhooks<SimplifiedContext>["onAny"];
public onError: Webhooks<SimplifiedContext>["onError"];

constructor(secret: string) {
this.webhooks = new Webhooks<SimplifiedContext>({
secret,
transform: (event) => {
return new Context(event);
},
});
this.on = this.webhooks.on;
this.onAny = this.webhooks.onAny;
this.onError = this.webhooks.onError;

this.onAny((event) => {
console.log(`Event ${event.name} received (id: ${event.id})`);
});
this.onError((error) => {
console.error(error);
});
}
}
6 changes: 6 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EventHandler } from "../event-handler";
import { handleIssueCommentCreated } from "./issue/comment_created";

export function bindHandlers(webhooks: EventHandler) {
webhooks.on("issue_comment.created", handleIssueCommentCreated);
}
5 changes: 5 additions & 0 deletions src/handlers/issue/comment_created.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Context } from "../../context";

export async function handleIssueCommentCreated(event: Context<"issue_comment.created">) {
console.log(event);
}
242 changes: 0 additions & 242 deletions src/types/github-events.ts

This file was deleted.

21 changes: 9 additions & 12 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EmitterWebhookEventName, Webhooks, emitterEventNames } from "@octokit/webhooks";
import { EmitterWebhookEventName as WebhookEventName, emitterEventNames } from "@octokit/webhooks";
import { Type as T, type Static } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { GitHubEvent } from "./types/github-events";
import { handleEvent } from "./webhooks";

import { EventHandler } from "./event-handler";
import { bindHandlers } from "./handlers";

const envSchema = T.Object({
WEBHOOK_SECRET: T.String(),
Expand Down Expand Up @@ -81,7 +82,7 @@ export default {
);
}

if (!emitterEventNames.includes(eventName as EmitterWebhookEventName)) {
if (!emitterEventNames.includes(eventName as WebhookEventName)) {
return new Response(
JSON.stringify({
error: `Unsupported "x-github-event" header value: ${eventName}`,
Expand All @@ -90,17 +91,13 @@ export default {
);
}

const webhooks = new Webhooks({
secret: env.WEBHOOK_SECRET,
});

webhooks.on(Object.values(GitHubEvent), handleEvent);
const eventHandler = new EventHandler(env.WEBHOOK_SECRET);
bindHandlers(eventHandler);

console.debug(`${eventName} event received (id: ${id})`);
try {
await webhooks.verifyAndReceive({
await eventHandler.webhooks.verifyAndReceive({
id,
name: eventName as EmitterWebhookEventName,
name: eventName as WebhookEventName,
payload: await request.text(),
signature: signatureSHA256,
});
Expand Down

0 comments on commit 28c8e73

Please sign in to comment.