Skip to content

Commit 94c6696

Browse files
committed
start implementation of github event handling
1 parent e1203e6 commit 94c6696

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

server/src/webhooks/webhooks.controller.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
1+
import { Body, Controller, Post, UseGuards, Headers } from '@nestjs/common';
22
import { ApiBody, ApiExcludeEndpoint, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
33

4-
import { configService } from '@kb-config';
4+
import { configService, Logger } from '@kb-config';
55
import { DisableInProduction } from '@kb-decorators';
66
import { BitbucketWebhookGuard, GitHubWebhookGuard, GitLabWebhookGuard } from '@kb-guards';
77

@@ -10,6 +10,8 @@ import { WebhooksService } from './webhooks.service';
1010
@Controller('api/webhooks')
1111
@ApiTags('Webhooks')
1212
export class WebhooksController {
13+
private readonly logger = new Logger(WebhooksController.name);
14+
1315
constructor(
1416
private readonly webhooksService: WebhooksService
1517
) {}
@@ -39,9 +41,11 @@ export class WebhooksController {
3941
}
4042
})
4143
github(
42-
@Body() body: any
44+
@Body() body: any,
45+
@Headers('X-GitHub-Event') eventType: string
4346
) {
44-
return this.webhooksService.handleGitHubWebhook(body);
47+
// event type: https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads
48+
return this.webhooksService.handleGitHubWebhook(eventType, body);
4549
}
4650

4751
@Post('gitlab')

server/src/webhooks/webhooks.service.ts

+58-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as crypto from 'crypto';
22

33
import { Injectable } from '@nestjs/common';
44

5-
import { configService } from '@kb-config';
5+
import { configService, Logger } from '@kb-config';
66

77
@Injectable()
88
export class WebhooksService {
9+
private readonly logger = new Logger(WebhooksService.name);
10+
911
generateBitBucketWebhookApiToken(body: any) {
1012
const bodyString = JSON.stringify(body);
1113

@@ -31,24 +33,70 @@ export class WebhooksService {
3133
}
3234

3335
handleBitBucketWebhook(body: any) {
34-
console.log('BitBucket webhook received');
35-
3636
return {
3737
message: 'Webhook received'
3838
};
3939
}
4040

41-
handleGitHubWebhook(body: any) {
42-
console.log('GitHub webhook received');
41+
handleGitHubWebhook(
42+
eventType: string,
43+
body: Record<string, any>
44+
) {
45+
// event type: https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads
46+
if (eventType === 'installation' && body.action === 'created') {
47+
this.logger.debug('GitHub App installation created');
48+
return;
49+
}
4350

44-
return {
45-
message: 'Webhook received'
46-
};
51+
if (eventType === 'installation' && body.action === 'deleted') {
52+
this.logger.debug('GitHub App installation deleted');
53+
// return this.webhooksService.handleGitHubAppInstallationDeleted(body);
54+
return;
55+
}
56+
57+
if (eventType === 'installation_repositories' && body.action === 'added') {
58+
this.logger.debug('GitHub App installation repositories added');
59+
// return this.webhooksService.handleGitHubAppInstallationRepositories(body);
60+
return;
61+
}
62+
63+
if (eventType === 'installation_repositories' && body.action === 'removed') {
64+
this.logger.debug('GitHub App installation repositories removed');
65+
// return this.webhooksService.handleGitHubAppInstallationRepositoriesRemoved(body);
66+
return;
67+
}
68+
69+
if (eventType === 'push') {
70+
this.logger.debug('GitHub push event');
71+
// return this.webhooksService.handleGitHubPush(body);
72+
return;
73+
}
74+
75+
if (eventType === 'pull_request') {
76+
this.logger.debug('GitHub pull request event');
77+
// return this.webhooksService.handleGitHubPullRequest(body);
78+
return;
79+
}
80+
81+
if (eventType === 'pull_request_review') {
82+
this.logger.debug('GitHub pull request review event');
83+
// return this.webhooksService.handleGitHubPullRequestReview(body);
84+
return;
85+
}
86+
87+
if (eventType === 'pull_request_review_comment') {
88+
this.logger.debug('GitHub pull request review comment event');
89+
// return this.webhooksService.handleGitHubPullRequestReviewComment(body);
90+
return;
91+
}
92+
93+
this.logger.debug('GitHub event not handled', {
94+
eventType,
95+
action: body.action
96+
});
4797
}
4898

4999
handleGitLabWebhook(body: any) {
50-
console.log('GitLab webhook received');
51-
52100
return {
53101
message: 'Webhook received'
54102
};

0 commit comments

Comments
 (0)