-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (62 loc) ยท 2.15 KB
/
index.js
File metadata and controls
75 lines (62 loc) ยท 2.15 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
72
73
74
75
/**
* Cloudflare Worker: DaleStudy GitHub App
*
* DaleStudy ์กฐ์ง์ ์๋ํ ์์
์ ์ฒ๋ฆฌํ๋ ํตํฉ GitHub App
*/
import { checkWeeks } from "./handlers/check-weeks.js";
import { handleWebhook } from "./handlers/webhooks.js";
import { approvePrs } from "./handlers/approve_prs.js";
import { mergePrs } from "./handlers/merge_prs.js";
import { preflightResponse, corsResponse, errorResponse } from "./utils/cors.js";
import { verifyWebhookSignature } from "./utils/webhook.js";
export default {
async fetch(request, env) {
// Handle CORS preflight
if (request.method === "OPTIONS") {
return preflightResponse();
}
if (request.method !== "POST") {
return corsResponse({ error: "Method not allowed" }, 405);
}
const url = new URL(request.url);
// GitHub Webhook ์์
if (url.pathname === "/webhooks") {
// Webhook signature ๊ฒ์ฆ
const signature = request.headers.get("X-Hub-Signature-256");
const rawBody = await request.text();
// Secret์ด ์ค์ ๋์ด ์์ผ๋ฉด ๊ฒ์ฆ
if (env.WEBHOOK_SECRET) {
const isValid = await verifyWebhookSignature(
rawBody,
signature,
env.WEBHOOK_SECRET
);
if (!isValid) {
console.error("Invalid webhook signature");
return errorResponse("Invalid signature", 401);
}
}
// Request ๊ฐ์ฒด ์ฌ์์ฑ (body๋ฅผ ๋ค์ ์ฝ์ ์ ์๋๋ก)
const newRequest = new Request(request.url, {
method: request.method,
headers: request.headers,
body: rawBody,
});
return handleWebhook(newRequest, env);
}
// PR Week ์ค์ ๊ฒ์ฌ (์๋ ํธ์ถ์ฉ)
if (url.pathname === "/check-weeks") {
return checkWeeks(request, env);
}
// Bulk approve open PRs
if (url.pathname === "/approve-prs" || url.pathname === "/approve_prs") {
return approvePrs(request, env);
}
// Bulk merge open PRs
if (url.pathname === "/merge-prs" || url.pathname === "/merge_prs") {
return mergePrs(request, env);
}
// ์ง์ํ์ง ์๋ ์๋ํฌ์ธํธ
return corsResponse({ error: "Not found" }, 404);
},
};