Skip to content

Commit

Permalink
start implementation of github event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thatkookooguy committed Oct 24, 2024
1 parent e1203e6 commit 94c6696
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
12 changes: 8 additions & 4 deletions server/src/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Post, UseGuards, Headers } from '@nestjs/common';
import { ApiBody, ApiExcludeEndpoint, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';

import { configService } from '@kb-config';
import { configService, Logger } from '@kb-config';
import { DisableInProduction } from '@kb-decorators';
import { BitbucketWebhookGuard, GitHubWebhookGuard, GitLabWebhookGuard } from '@kb-guards';

Expand All @@ -10,6 +10,8 @@ import { WebhooksService } from './webhooks.service';
@Controller('api/webhooks')
@ApiTags('Webhooks')
export class WebhooksController {
private readonly logger = new Logger(WebhooksController.name);

constructor(
private readonly webhooksService: WebhooksService
) {}
Expand Down Expand Up @@ -39,9 +41,11 @@ export class WebhooksController {
}
})
github(
@Body() body: any
@Body() body: any,
@Headers('X-GitHub-Event') eventType: string
) {
return this.webhooksService.handleGitHubWebhook(body);
// event type: https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads
return this.webhooksService.handleGitHubWebhook(eventType, body);
}

@Post('gitlab')
Expand Down
68 changes: 58 additions & 10 deletions server/src/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as crypto from 'crypto';

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

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

@Injectable()
export class WebhooksService {
private readonly logger = new Logger(WebhooksService.name);

generateBitBucketWebhookApiToken(body: any) {
const bodyString = JSON.stringify(body);

Expand All @@ -31,24 +33,70 @@ export class WebhooksService {
}

handleBitBucketWebhook(body: any) {
console.log('BitBucket webhook received');

return {
message: 'Webhook received'
};
}

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

return {
message: 'Webhook received'
};
if (eventType === 'installation' && body.action === 'deleted') {
this.logger.debug('GitHub App installation deleted');
// return this.webhooksService.handleGitHubAppInstallationDeleted(body);
return;
}

if (eventType === 'installation_repositories' && body.action === 'added') {
this.logger.debug('GitHub App installation repositories added');
// return this.webhooksService.handleGitHubAppInstallationRepositories(body);
return;
}

if (eventType === 'installation_repositories' && body.action === 'removed') {
this.logger.debug('GitHub App installation repositories removed');
// return this.webhooksService.handleGitHubAppInstallationRepositoriesRemoved(body);
return;
}

if (eventType === 'push') {
this.logger.debug('GitHub push event');
// return this.webhooksService.handleGitHubPush(body);
return;
}

if (eventType === 'pull_request') {
this.logger.debug('GitHub pull request event');
// return this.webhooksService.handleGitHubPullRequest(body);
return;
}

if (eventType === 'pull_request_review') {
this.logger.debug('GitHub pull request review event');
// return this.webhooksService.handleGitHubPullRequestReview(body);
return;
}

if (eventType === 'pull_request_review_comment') {
this.logger.debug('GitHub pull request review comment event');
// return this.webhooksService.handleGitHubPullRequestReviewComment(body);
return;
}

this.logger.debug('GitHub event not handled', {
eventType,
action: body.action
});
}

handleGitLabWebhook(body: any) {
console.log('GitLab webhook received');

return {
message: 'Webhook received'
};
Expand Down

0 comments on commit 94c6696

Please sign in to comment.