Skip to content

Commit 33f8819

Browse files
committed
refactor: move isGitHubEvent to util
1 parent c8cc9f7 commit 33f8819

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/filter/index.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
import { WebhookEvent, WebhookEventMap, WebhookEventName } from "@octokit/webhooks-types";
1+
import { WebhookEvent } from "@octokit/webhooks-types";
22

33
import { getAndIncrementKV } from "./kv.ts";
44
import { getRequestLog } from "../server/context.ts";
55
import { UrlConfig } from "../types.d.ts";
6-
import { wildcardMatch } from "../util.ts";
6+
import { isGitHubEvent, wildcardMatch } from "../util.ts";
77

88
const COMMON_CI_BOTS = ["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]", "dependabot[bot]"];
99

10-
function isGitHubEvent<T extends WebhookEventName>(
11-
_data: WebhookEvent,
12-
eventType: string,
13-
expected: T,
14-
): _data is WebhookEventMap[T] {
15-
return eventType === expected;
16-
}
17-
1810
export default async function filter(
19-
headers: Record<string, string>,
2011
json: WebhookEvent,
12+
event: string,
2113
config: UrlConfig,
2214
): Promise<string | null> {
23-
const event = headers["x-github-event"] || "unknown";
24-
2515
if (!("sender" in json)) {
2616
// Discord always requires a sender (most events have this, but a handful of them don't)
2717
return "missing sender";

src/handler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ export default async function handle(
1717
): Promise<[Response, Record<string, string>]> {
1818
const urlConfig = getUrlConfig(queryParams);
1919

20+
const eventType = headers["x-github-event"];
21+
if (!eventType) {
22+
throw new HTTPException(418, { message: `Missing x-github-event` });
23+
}
24+
2025
// do the thing
21-
const filterReason = await filterWebhook(headers, json, urlConfig);
26+
const filterReason = await filterWebhook(json, eventType, urlConfig);
2227
if (filterReason !== null) {
2328
const reqLog = getRequestLog();
2429
reqLog.debug(`handler: ignored due to '${filterReason}'`);

src/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Context } from "@hono/hono";
2+
import { WebhookEvent, WebhookEventMap, WebhookEventName } from "@octokit/webhooks-types";
23

34
export function parseBool(s: string, strict: false): boolean | undefined;
45
export function parseBool(s: string, strict?: true): boolean;
@@ -54,3 +55,11 @@ export type RequestLog = ReturnType<typeof requestLog>;
5455
export function setMetaHeader(c: Context, key: string, value: string): void {
5556
c.res.headers.set(`x-webhook-filter-${key}`, value);
5657
}
58+
59+
export function isGitHubEvent<T extends WebhookEventName>(
60+
_data: WebhookEvent,
61+
eventType: string,
62+
expected: T,
63+
): _data is WebhookEventMap[T] {
64+
return eventType === expected;
65+
}

0 commit comments

Comments
 (0)