|
| 1 | +// src/github/github-webhook.controller.ts |
| 2 | + |
| 3 | +import { Body, Controller, Post, Req, Res } from '@nestjs/common'; |
| 4 | +import { Request, Response } from 'express'; |
| 5 | +import { createNodeMiddleware } from '@octokit/webhooks'; |
| 6 | +import { GitHubAppService } from './githubApp.service'; |
| 7 | +import { GetUserIdFromToken } from 'src/decorator/get-auth-token.decorator'; |
| 8 | +import { UserService } from 'src/user/user.service'; |
| 9 | + |
| 10 | +@Controller('github') |
| 11 | +export class GitHuController { |
| 12 | + private readonly webhookMiddleware; |
| 13 | + |
| 14 | + constructor(private readonly gitHubAppService: GitHubAppService, private readonly userService: UserService) { |
| 15 | + // Get the App instance from the service |
| 16 | + const app = this.gitHubAppService.getApp(); |
| 17 | + |
| 18 | + // Create the Express-style middleware from @octokit/webhooks |
| 19 | + this.webhookMiddleware = createNodeMiddleware(app.webhooks, { |
| 20 | + path: '/github/webhook', |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + @Post('webhook') |
| 25 | + async handleWebhook(@Req() req: Request, @Res() res: Response) { |
| 26 | + console.log('📩 Received POST /github/webhook'); |
| 27 | + |
| 28 | + return this.webhookMiddleware(req, res, (error?: any) => { |
| 29 | + if (error) { |
| 30 | + console.error('Webhook middleware error:', error); |
| 31 | + return res.status(500).send('Internal Server Error'); |
| 32 | + } else { |
| 33 | + console.log('Middleware processed request'); |
| 34 | + return res.sendStatus(200); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + @Post('storeInstallation') |
| 40 | + async storeInstallation( |
| 41 | + @Body() body: { installationId: string, githubCode: string }, |
| 42 | + @GetUserIdFromToken() userId: string, |
| 43 | + ) { |
| 44 | + await this.userService.bindUserIdAndInstallId(userId, body.installationId, body.githubCode); |
| 45 | + return { success: true }; |
| 46 | + } |
| 47 | +} |
0 commit comments