forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-webhooks.controller.ts
More file actions
38 lines (33 loc) · 1.12 KB
/
Copy pathgithub-webhooks.controller.ts
File metadata and controls
38 lines (33 loc) · 1.12 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
import { Controller, Headers, HttpCode, Post, Req } from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
import type { Request } from 'express';
import { GithubWebhooksService } from './github-webhooks.service';
interface RawBodyRequest extends Request {
rawBody?: Buffer;
}
@ApiExcludeController()
@Controller('github/webhooks')
export class GithubWebhooksController {
constructor(private readonly webhooksService: GithubWebhooksService) {}
@Post()
@HttpCode(202)
async handle(
@Req() req: RawBodyRequest,
@Headers('x-github-event') eventType: string,
@Headers('x-github-delivery') deliveryId: string,
@Headers('x-hub-signature-256') signature: string,
) {
const rawBody = req.rawBody ?? Buffer.from(JSON.stringify(req.body ?? {}));
const signatureValid = this.webhooksService.verifySignature(
rawBody,
signature,
);
const event = await this.webhooksService.handleEvent(
eventType,
deliveryId,
req.body as Record<string, unknown>,
signatureValid,
);
return { received: true, eventId: event.id, status: event.status };
}
}