generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ubiquity/whilefoo/event-handler
- Loading branch information
Showing
6 changed files
with
69 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters