-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
87 lines (80 loc) · 2.48 KB
/
main.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// webhooks.mcbe.wtf
/// Copyright (C) 2024 Minecraft Moderators
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
import {
createEventHandler,
EmitterWebhookEvent,
} from "https://esm.sh/@octokit/webhooks@13.3.0";
const secret = Deno.env.get("GITHUB_WEBHOOK_SECRET");
const refToWatch = Deno.env.get("GITHUB_WEBHOOK_REF");
const discordUrl = Deno.env.get("DISCORD_WEBHOOK_URL");
async function sendMessage(content: string) {
if (!secret || !refToWatch || !discordUrl) {
throw new Error("Missing configuration");
}
const res = await fetch(discordUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content,
}),
});
if (res.ok) return;
const err = await res.text();
console.error("Error when sending message to discord", err);
}
const eventHandler = createEventHandler({ secret });
eventHandler.on("push", async ({ payload }) => {
if (payload.ref === `refs/heads/${refToWatch}`) {
const content = `${payload.pusher.name} updated the rules
${
payload.commits.map((c) =>
`[${
c.id.substring(0, 11)
}](<${payload.repository.url}/commit/${c.id}>): ${c.message}`
).join("\n")
}
[Compare changes](<${payload.repository.url}/compare/${payload.before}...${payload.after}>)`;
await sendMessage(content);
}
});
Deno.serve(async (req) => {
if (req.url.endsWith("/github")) {
const id = req.headers.get(
"x-github-delivery",
) as EmitterWebhookEvent["id"];
const name = req.headers.get(
"x-github-event",
) as EmitterWebhookEvent["name"];
if (!id || !name) {
return Response.json({
message: "id or name is missing",
}, { status: 400 });
}
const payload = (await req.json()) as EmitterWebhookEvent["payload"];
eventHandler.receive({
id,
name,
// @ts-expect-error wtf?
payload,
});
return Response.json({
message: "Triggered webhook",
});
}
return Response.json({ status: "OK" });
});