| 222 | } |
| 223 | |
| 224 | async function webhookHandler(event: HandlerEvent<"HTTP">, logger: Logger) { |
| 225 | logger.debug("[inside github integration] Handling github repo event"); |
| 226 | |
| 227 | const { rawEvent: request, source } = event; |
| 228 | |
| 229 | if (!request.body) { |
| 230 | logger.debug("[inside github integration] No body found"); |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | const rawBody = await request.text(); |
| 236 | |
| 237 | const deliveryId = request.headers.get("x-github-delivery"); |
| 238 | const hookId = request.headers.get("x-github-hook-id"); |
| 239 | const signature = request.headers.get("x-hub-signature-256"); |
| 240 | |
| 241 | if (source.secret && signature) { |
| 242 | const githubWebhooks = new Webhooks({ |
| 243 | secret: source.secret, |
| 244 | }); |
| 245 | |
| 246 | if (!githubWebhooks.verify(rawBody, signature)) { |
| 247 | logger.debug("[inside github integration] Unable to verify the signature of the rawBody", { |
| 248 | signature, |
| 249 | secret: source.secret, |
| 250 | }); |
| 251 | |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | const name = request.headers.get("x-github-event") ?? "unknown"; |
| 257 | const allHeaders = Object.fromEntries(request.headers.entries()); |
| 258 | |
| 259 | const context = omit(allHeaders, [ |
| 260 | "x-github-event", |
| 261 | "x-github-delivery", |
| 262 | "x-hub-signature-256", |
| 263 | "x-hub-signature", |
| 264 | "content-type", |
| 265 | "content-length", |
| 266 | "accept", |
| 267 | "accept-encoding", |
| 268 | "x-forwarded-proto", |
| 269 | ]); |
| 270 | |
| 271 | const payload = parseBody(rawBody); |
| 272 | |
| 273 | if (!payload) { |
| 274 | logger.debug("[inside github integration] Unable to parse the rawBody"); |
| 275 | |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | logger.debug("[inside github integration] Returning an event for the webhook!", { |
| 280 | name, |
| 281 | payload, |