Repository navigation
Expand file tree
/
Copy pathhandler.ts
More file actions
71 lines (64 loc) · 2.32 KB
/
Copy pathhandler.ts
File metadata and controls
71 lines (64 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { HTTPException } from "@hono/hono/http-exception";
import { WebhookEvent } from "@octokit/webhooks-types";
import fixupEmbeds from "./discord/formatter.ts";
import { sendWebhook } from "./discord/webhook.ts";
import filterWebhook from "./filter/index.ts";
import { getRequestLog } from "./server/context.ts";
import { UrlConfig } from "./types.d.ts";
import { parseBool } from "./util.ts";
export default async function handle(
json: WebhookEvent,
headers: Record<string, string>,
queryParams: Record<string, string>,
id: string,
token: string,
): Promise<[Response, Record<string, string>]> {
const urlConfig = getUrlConfig(queryParams);
const eventType = headers["x-github-event"];
if (!eventType) {
throw new HTTPException(418, { message: `Missing x-github-event` });
}
// do the thing
const filterReason = await filterWebhook(json, eventType, urlConfig);
if (filterReason !== null) {
const reqLog = getRequestLog();
reqLog.debug(`handler: ignored due to '${filterReason}'`);
return [
new Response(`Ignored by webhook filter (reason: ${filterReason})`, { status: 203 }),
{},
];
}
// mutates `json` in-place (fixing codeblocks etc.)
json = fixupEmbeds(json);
return await sendWebhook(id, token, headers, json);
}
function getUrlConfig(params: Record<string, string>): UrlConfig {
const config: UrlConfig = {};
for (const [key, value] of Object.entries(params)) {
switch (key) {
case "sig":
continue;
case "allowBranches":
config.allowBranches = value;
break;
case "allowBots": {
const bool = parseBool(value, false);
if (bool === undefined) {
config.allowBots = value;
} else {
config.allowBots = bool ? ".*" : "";
}
break;
}
case "hideTags":
config.hideTags = parseBool(value);
break;
case "commentBurstLimit":
config.commentBurstLimit = parseInt(value);
break;
default:
throw new HTTPException(418, { message: `Unknown config option: ${key}` });
}
}
return config;
}