|
11 | 11 | import { WebhookEvent, WebhookEvents } from "@octokit/webhooks-types"; |
12 | 12 | import Br from "./components/Br"; |
13 | 13 | import { TextDisplay } from "./components/TextDisplay"; |
14 | | -import handleCreate from "./handlers/handleCreate"; |
15 | | -import handleDelete from "./handlers/handleDelete"; |
16 | | -import handleIssues from "./handlers/handleIssues"; |
17 | | -import handlePullRequests from "./handlers/handlePullRequests"; |
18 | | -import handlePush from "./handlers/handlePush"; |
19 | | -import handleStar from "./handlers/handleStar"; |
| 14 | +import handleCreate from "./handlers/create"; |
| 15 | +import handleDelete from "./handlers/delete"; |
| 16 | +import handleIssues from "./handlers/issues"; |
| 17 | +import handlePullRequests from "./handlers/pullRequests"; |
| 18 | +import handlePush from "./handlers/push"; |
| 19 | +import handleStar from "./handlers/star"; |
20 | 20 | import { WebhookBody, WebhookBodyWithHeaders } from "./types"; |
21 | 21 | import { send } from "./utils/discord"; |
22 | 22 |
|
23 | 23 | export interface Env {} |
24 | 24 |
|
25 | 25 | export const DEFAULT_USER = { |
26 | | - username: "GitCordHook [META]", |
27 | | - avatar_url: |
28 | | - "https://media.discordapp.net/attachments/1202351390484353046/1372294496456413294/443813695-80bf856e-a6e5-42fd-9e41-d0f5d346b4de.png?ex=68264057&is=6824eed7&hm=6e3f2a0ef679ba093c693255a03d182da1872f2f119a7bc9bea9a575b80eaf16&=&format=webp&quality=lossless", |
| 26 | + username: "GitCordHook [META]", |
| 27 | + avatar_url: |
| 28 | + "https://media.discordapp.net/attachments/1202351390484353046/1372294496456413294/443813695-80bf856e-a6e5-42fd-9e41-d0f5d346b4de.png?ex=68264057&is=6824eed7&hm=6e3f2a0ef679ba093c693255a03d182da1872f2f119a7bc9bea9a575b80eaf16&=&format=webp&quality=lossless", |
29 | 29 | }; |
30 | 30 |
|
31 | 31 | function runHandler( |
32 | | - githubEvent: WebhookEvents[0] | "ping", |
33 | | - data: WebhookEvent |
| 32 | + githubEvent: WebhookEvents[0] | "ping", |
| 33 | + data: WebhookEvent |
34 | 34 | ): WebhookBody { |
35 | | - switch (githubEvent) { |
36 | | - case "issues": { |
37 | | - return handleIssues(data as any); |
38 | | - } |
39 | | - |
40 | | - case "pull_request": { |
41 | | - return handlePullRequests(data as any); |
42 | | - } |
43 | | - |
44 | | - case "create": { |
45 | | - return handleCreate(data as any); |
46 | | - } |
47 | | - |
48 | | - case "delete": { |
49 | | - return handleDelete(data as any); |
50 | | - } |
51 | | - |
52 | | - case "push": { |
53 | | - return handlePush(data as any); |
54 | | - } |
55 | | - |
56 | | - case "star": { |
57 | | - return handleStar(data as any); |
58 | | - } |
59 | | - |
60 | | - case "ping": { |
61 | | - return { |
62 | | - components: ( |
63 | | - <> |
64 | | - <TextDisplay> |
65 | | - # Webhook Ping! |
66 | | - <Br /> |
67 | | - GitHub webhook setup successfully, using Webhook proxy by |
68 | | - https://github.com/surgedevs/gitcordhook |
69 | | - </TextDisplay> |
70 | | - </> |
71 | | - ), |
72 | | - }; |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - return { default: data }; |
| 35 | + switch (githubEvent) { |
| 36 | + case "issues": { |
| 37 | + return handleIssues(data as any); |
| 38 | + } |
| 39 | + |
| 40 | + case "pull_request": { |
| 41 | + return handlePullRequests(data as any); |
| 42 | + } |
| 43 | + |
| 44 | + case "create": { |
| 45 | + return handleCreate(data as any); |
| 46 | + } |
| 47 | + |
| 48 | + case "delete": { |
| 49 | + return handleDelete(data as any); |
| 50 | + } |
| 51 | + |
| 52 | + case "push": { |
| 53 | + return handlePush(data as any); |
| 54 | + } |
| 55 | + |
| 56 | + case "star": { |
| 57 | + return handleStar(data as any); |
| 58 | + } |
| 59 | + |
| 60 | + case "ping": { |
| 61 | + return { |
| 62 | + components: ( |
| 63 | + <> |
| 64 | + <TextDisplay> |
| 65 | + # Webhook Ping! |
| 66 | + <Br /> |
| 67 | + GitHub webhook setup successfully, using Webhook |
| 68 | + proxy by https://github.com/surgedevs/gitcordhook |
| 69 | + </TextDisplay> |
| 70 | + </> |
| 71 | + ), |
| 72 | + }; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return { default: data }; |
77 | 77 | } |
78 | 78 |
|
79 | 79 | export default { |
80 | | - async fetch(request: Request, env: Env, ctx: ExecutionContext) { |
81 | | - if (request.method == "GET") { |
82 | | - return new Response( |
83 | | - "Hey you aren't a webhook! This page is only for webhooks! See https://github.com/surgedevs/gitcordhook for more info :D" |
84 | | - ); |
85 | | - } |
86 | | - |
87 | | - const webhookUrl = new URL(request.url); |
88 | | - const discordWebhook = webhookUrl.pathname.replace( |
89 | | - /^\/https:\/\//, |
90 | | - "https://" |
91 | | - ); |
92 | | - |
93 | | - let json = null; |
94 | | - |
95 | | - try { |
96 | | - json = await request.json(); |
97 | | - } catch (exception) { |
98 | | - await send(discordWebhook, { |
99 | | - content: |
100 | | - "You must select application/json inside the Github webhook settings!", |
101 | | - }); |
102 | | - } |
103 | | - |
104 | | - const eventType = request.headers.get("X-GitHub-Event"); |
105 | | - const webhookBody = runHandler(eventType ?? ("" as any), json as any); |
106 | | - |
107 | | - if ("default" in webhookBody) { |
108 | | - webhookBody.headers = { |
109 | | - "X-GitHub-Delivery": request.headers.get("X-GitHub-Delivery"), |
110 | | - "X-GitHub-Event": request.headers.get("X-GitHub-Event"), |
111 | | - "X-GitHub-Hook-ID": request.headers.get("X-GitHub-Hook-ID"), |
112 | | - "X-GitHub-Hook-Installation-Target-ID": request.headers.get( |
113 | | - "X-GitHub-Hook-Installation-Target-ID" |
114 | | - ), |
115 | | - "X-GitHub-Hook-Installation-Target-Type": request.headers.get( |
116 | | - "X-GitHub-Hook-Installation-Target-Type" |
117 | | - ), |
118 | | - }; |
119 | | - } |
120 | | - |
121 | | - const response = await send( |
122 | | - discordWebhook, |
123 | | - webhookBody as WebhookBodyWithHeaders |
124 | | - ); |
125 | | - |
126 | | - if (response.success) { |
127 | | - return new Response( |
128 | | - `Discord Webhook Execute Success; response body = ${ |
129 | | - response.body |
130 | | - }, sent body = ${JSON.stringify(webhookBody)}` |
131 | | - ); |
132 | | - } |
133 | | - |
134 | | - return new Response( |
135 | | - `Discord Webhook Execute Failed; response body = ${ |
136 | | - response.body |
137 | | - }, sent body = ${JSON.stringify(webhookBody)}`, |
138 | | - { |
139 | | - status: 500, |
140 | | - } |
141 | | - ); |
142 | | - }, |
| 80 | + async fetch(request: Request, env: Env, ctx: ExecutionContext) { |
| 81 | + if (request.method == "GET") { |
| 82 | + return new Response( |
| 83 | + "Hey you aren't a webhook! This page is only for webhooks! See https://github.com/surgedevs/gitcordhook for more info :D" |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + const webhookUrl = new URL(request.url); |
| 88 | + const discordWebhook = webhookUrl.pathname.replace( |
| 89 | + /^\/https:\/\//, |
| 90 | + "https://" |
| 91 | + ); |
| 92 | + |
| 93 | + let json = null; |
| 94 | + |
| 95 | + try { |
| 96 | + json = await request.json(); |
| 97 | + } catch (exception) { |
| 98 | + await send(discordWebhook, { |
| 99 | + content: |
| 100 | + "You must select application/json inside the Github webhook settings!", |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + const eventType = request.headers.get("X-GitHub-Event"); |
| 105 | + const webhookBody = runHandler(eventType ?? ("" as any), json as any); |
| 106 | + |
| 107 | + if ("none" in webhookBody) { |
| 108 | + return new Response( |
| 109 | + `Discord Webhook Execute Success; Event intentionally skipped! Reason: ${ |
| 110 | + webhookBody.reason ? webhookBody.reason : "None provided" |
| 111 | + }` |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + if ("default" in webhookBody) { |
| 116 | + webhookBody.headers = { |
| 117 | + "X-GitHub-Delivery": request.headers.get("X-GitHub-Delivery"), |
| 118 | + "X-GitHub-Event": request.headers.get("X-GitHub-Event"), |
| 119 | + "X-GitHub-Hook-ID": request.headers.get("X-GitHub-Hook-ID"), |
| 120 | + "X-GitHub-Hook-Installation-Target-ID": request.headers.get( |
| 121 | + "X-GitHub-Hook-Installation-Target-ID" |
| 122 | + ), |
| 123 | + "X-GitHub-Hook-Installation-Target-Type": request.headers.get( |
| 124 | + "X-GitHub-Hook-Installation-Target-Type" |
| 125 | + ), |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + const response = await send( |
| 130 | + discordWebhook, |
| 131 | + webhookBody as WebhookBodyWithHeaders |
| 132 | + ); |
| 133 | + |
| 134 | + if (response.success) { |
| 135 | + return new Response( |
| 136 | + `Discord Webhook Execute Success; response body = ${ |
| 137 | + response.body |
| 138 | + }, sent body = ${JSON.stringify(webhookBody)}` |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + return new Response( |
| 143 | + `Discord Webhook Execute Failed; response body = ${ |
| 144 | + response.body |
| 145 | + }, sent body = ${JSON.stringify(webhookBody)}`, |
| 146 | + { |
| 147 | + status: 500, |
| 148 | + } |
| 149 | + ); |
| 150 | + }, |
143 | 151 | }; |
0 commit comments