Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ MAIL_FROM=noreply@example.com
MAIL_PORT=587
MAIL_DOMAIN=your_net

# github app
GITHUB_APP_ENABLED=true
GITHUB_APP_ID="GITHUB_APP_ID"
GITHUB_PRIVATE_KEY_PATH="YOUR_PATH"
GITHUB_CLIENT_ID="GITHUB_CLIENT_ID"
GITHUB_CLIENT_SECRET="GITHUB_CLIENT_SECRET"
GITHUB_WEBHOOK_SECRET="GITHUB_WEBHOOK_SECRET"
CALLBACK="CALLBACK"
5 changes: 5 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.2",
"@octokit/auth-app": "^7.1.5",
"@octokit/webhooks": "^13.7.5",
"@types/bcrypt": "^5.0.2",
"@types/fs-extra": "^11.0.4",
"@types/normalize-path": "^3.0.2",
"@types/toposort": "^2.0.7",
"archiver": "^7.0.1",
"axios": "^1.7.7",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
Expand All @@ -62,10 +65,12 @@
"markdown-to-txt": "^2.0.1",
"nodemailer": "^6.10.0",
"normalize-path": "^3.0.0",
"octokit": "^4.1.2",
"openai": "^4.77.0",
"p-queue-es5": "^6.0.2",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"simple-git": "^3.27.0",
"sqlite3": "^5.1.7",
"subscriptions-transport-ws": "^0.11.0",
"toposort": "^2.0.2",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from 'src/interceptor/LoggingInterceptor';
import { PromptToolModule } from './prompt-tool/prompt-tool.module';
import { MailModule } from './mail/mail.module';
import { GitHubModule } from './github/github.module';

// TODO(Sma1lboy): move to a separate file
function isProduction(): boolean {
Expand Down Expand Up @@ -50,6 +51,7 @@ function isProduction(): boolean {
PromptToolModule,
MailModule,
TypeOrmModule.forFeature([User]),
GitHubModule
],
providers: [
AppResolver,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { UserService } from 'src/user/user.service';
import { PubSub } from 'graphql-subscriptions';
import { JwtCacheModule } from 'src/jwt-cache/jwt-cache.module';
import { UploadModule } from 'src/upload/upload.module';
import { GitHubModule } from 'src/github/github.module';

@Module({
imports: [
TypeOrmModule.forFeature([Chat, User]),
AuthModule,
JwtCacheModule,
UploadModule,
GitHubModule,
],
controllers: [ChatController],
providers: [
Expand Down
47 changes: 47 additions & 0 deletions backend/src/github/github.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// src/github/github-webhook.controller.ts

import { Body, Controller, Post, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';
import { createNodeMiddleware } from '@octokit/webhooks';
import { GitHubAppService } from './githubApp.service';
import { GetUserIdFromToken } from 'src/decorator/get-auth-token.decorator';
import { UserService } from 'src/user/user.service';

@Controller('github')
export class GitHuController {
private readonly webhookMiddleware;

constructor(private readonly gitHubAppService: GitHubAppService, private readonly userService: UserService) {
// Get the App instance from the service
const app = this.gitHubAppService.getApp();

// Create the Express-style middleware from @octokit/webhooks
this.webhookMiddleware = createNodeMiddleware(app.webhooks, {
path: '/github/webhook',
});
}

@Post('webhook')
async handleWebhook(@Req() req: Request, @Res() res: Response) {
console.log('📩 Received POST /github/webhook');

return this.webhookMiddleware(req, res, (error?: any) => {
if (error) {
console.error('Webhook middleware error:', error);
return res.status(500).send('Internal Server Error');
} else {
console.log('Middleware processed request');
return res.sendStatus(200);
}
});
}

@Post('storeInstallation')
async storeInstallation(
@Body() body: { installationId: string, githubCode: string },
@GetUserIdFromToken() userId: string,
) {
await this.userService.bindUserIdAndInstallId(userId, body.installationId, body.githubCode);
return { success: true };
}
}
32 changes: 32 additions & 0 deletions backend/src/github/github.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { forwardRef, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '../auth/auth.module';
import { ProjectGuard } from '../guard/project.guard';
import { ChatService } from 'src/chat/chat.service';
import { User } from 'src/user/user.model';
import { Chat } from 'src/chat/chat.model';
import { AppConfigModule } from 'src/config/config.module';
import { UploadModule } from 'src/upload/upload.module';
import { GitHubAppService } from './githubApp.service';
import { GitHubService } from './github.service';
import { Project } from 'src/project/project.model';
import { ProjectPackages } from 'src/project/project-packages.model';
import { GitHuController } from './github.controller';
import { ProjectService } from 'src/project/project.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { UserModule } from 'src/user/user.module';

@Module({
imports: [
TypeOrmModule.forFeature([Project, Chat, User, ProjectPackages]),
AuthModule,
AppConfigModule,
UploadModule,
ConfigModule,
forwardRef(() => UserModule),
],
controllers: [GitHuController],
providers: [ProjectService, ProjectGuard, GitHubAppService, GitHubService, ConfigService, ChatService],
exports: [GitHubService],
})
export class GitHubModule {}
Loading
Loading