From 94c669613966b55a7eae7902ab56d3cd0b8dadaa Mon Sep 17 00:00:00 2001 From: Neil Kalman Date: Thu, 24 Oct 2024 16:45:33 +0000 Subject: [PATCH] start implementation of github event handling --- server/src/webhooks/webhooks.controller.ts | 12 ++-- server/src/webhooks/webhooks.service.ts | 68 ++++++++++++++++++---- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/server/src/webhooks/webhooks.controller.ts b/server/src/webhooks/webhooks.controller.ts index 0ad0ef1..08b4e9a 100644 --- a/server/src/webhooks/webhooks.controller.ts +++ b/server/src/webhooks/webhooks.controller.ts @@ -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'; @@ -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 ) {} @@ -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') diff --git a/server/src/webhooks/webhooks.service.ts b/server/src/webhooks/webhooks.service.ts index 8c8eaee..bd59786 100644 --- a/server/src/webhooks/webhooks.service.ts +++ b/server/src/webhooks/webhooks.service.ts @@ -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); @@ -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 + ) { + // 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' };