Skip to content

Commit e6f8a0f

Browse files
committed
update now fully work
1 parent db85055 commit e6f8a0f

7 files changed

Lines changed: 212 additions & 110 deletions

File tree

backend/src/github/githubWebhook.controller.ts renamed to backend/src/github/github.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { GetUserIdFromToken } from 'src/decorator/get-auth-token.decorator';
88
import { UserService } from 'src/user/user.service';
99

1010
@Controller('github')
11-
export class GitHubWebhookController {
11+
export class GitHuController {
1212
private readonly webhookMiddleware;
1313

1414
constructor(private readonly gitHubAppService: GitHubAppService, private readonly userService: UserService) {
@@ -27,10 +27,10 @@ export class GitHubWebhookController {
2727

2828
return this.webhookMiddleware(req, res, (error?: any) => {
2929
if (error) {
30-
console.error('⚠️ Webhook middleware error:', error);
30+
console.error('Webhook middleware error:', error);
3131
return res.status(500).send('Internal Server Error');
3232
} else {
33-
console.log('Middleware processed request');
33+
console.log('Middleware processed request');
3434
return res.sendStatus(200);
3535
}
3636
});

backend/src/github/github.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { GitHubAppService } from './githubApp.service';
1111
import { GitHubService } from './github.service';
1212
import { Project } from 'src/project/project.model';
1313
import { ProjectPackages } from 'src/project/project-packages.model';
14-
import { GitHubWebhookController } from './githubWebhook.controller';
14+
import { GitHuController } from './github.controller';
1515
import { ProjectService } from 'src/project/project.service';
1616
import { ConfigModule, ConfigService } from '@nestjs/config';
1717
import { UserService } from 'src/user/user.service';
@@ -24,7 +24,7 @@ import { UserService } from 'src/user/user.service';
2424
UploadModule,
2525
ConfigModule
2626
],
27-
controllers: [GitHubWebhookController],
27+
controllers: [GitHuController],
2828
providers: [ProjectService, ProjectGuard, GitHubAppService, GitHubService, ConfigService, ChatService, UserService],
2929
exports: [GitHubService],
3030
})

backend/src/github/github.service.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@ import axios from 'axios';
66
import * as fs from 'fs';
77
import * as path from 'path';
88
import { ConfigService } from '@nestjs/config';
9+
import { Project } from 'src/project/project.model';
10+
import { InjectRepository } from '@nestjs/typeorm';
11+
import { Repository } from 'typeorm';
912

1013
@Injectable()
1114
export class GitHubService {
1215
private readonly logger = new Logger(GitHubService.name);
13-
16+
1417
private readonly appId: string;
1518
private privateKey: string;
19+
private ignored = ['node_modules', '.git', '.gitignore', '.env'];
20+
21+
constructor(
22+
private configService: ConfigService,
23+
@InjectRepository(Project)
24+
private projectsRepository: Repository<Project>,)
25+
{
1626

17-
constructor(private configService: ConfigService) {
1827
this.appId = this.configService.get<string>('GITHUB_APP_ID');
1928

2029
const privateKeyPath = this.configService.get<string>('GITHUB_PRIVATE_KEY_PATH');
@@ -102,19 +111,16 @@ export class GitHubService {
102111
* If you need an org-level repo, use POST /orgs/{org}/repos.
103112
*/
104113
async createUserRepo(
105-
installationToken: string,
106114
repoName: string,
107115
isPublic: boolean,
108-
githubCode: string,
116+
userOAuthToken: string,
109117
): Promise<{
110118
owner: string;
111119
repo: string;
112120
htmlUrl: string;
113121
}> {
114122
const url = `https://api.github.com/user/repos`;
115123

116-
const userOAuthToken = await this.exchangeOAuthCodeForToken(githubCode);
117-
118124
const response = await axios.post(
119125
url,
120126
{
@@ -128,6 +134,7 @@ export class GitHubService {
128134
},
129135
},
130136
);
137+
131138
// The response will have data about the new repo
132139
const data = response.data;
133140
return {
@@ -151,7 +158,7 @@ export class GitHubService {
151158
}
152159
}
153160

154-
/**
161+
/**
155162
* Push a single file to the given path in the repo using GitHub Contents API.
156163
*
157164
* @param relativePathInRepo e.g. "backend/index.js" or "frontend/package.json"
@@ -200,6 +207,12 @@ export class GitHubService {
200207
const entries = fs.readdirSync(folderPath, { withFileTypes: true });
201208

202209
for (const entry of entries) {
210+
211+
// Skip unwanted files
212+
if (this.ignored.includes(entry.name)) {
213+
continue;
214+
}
215+
203216
const entryPath = path.join(folderPath, entry.name);
204217
if (entry.isDirectory()) {
205218
// Skip unwanted directories

backend/src/github/githubApp.service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ export class GitHubAppService {
6363

6464
this.logger.log(`uninstall Created: installationId=${installationId}, GitHub Login=`);
6565

66+
// remove user github code and installationId
6667
await this.userRepo.update(
6768
{ githubInstallationId: installationId },
6869
{ githubInstallationId: null,
6970
githubCode: null
7071
}
7172
);
72-
this.logger.log(`Cleared installationId for user: ${installationId}`);
73-
7473

75-
// e.g. clear the installation ID in your DB
74+
this.logger.log(`Cleared installationId for user: ${installationId}`);
7675
});
7776

7877
// Handle errors
@@ -84,11 +83,10 @@ export class GitHubAppService {
8483
}
8584
});
8685

86+
// only for webhooks debugging
8787
this.app.webhooks.onAny(async (event) => {
88-
this.logger.log(`🔥 onAny: Received event='${event.name}' action='${event.payload}'`);
88+
this.logger.log(`onAny: Received event='${event.name}' action='${event.payload}'`);
8989
});
90-
91-
9290
}
9391

9492
/**

backend/src/project/project.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,19 +808,21 @@ export class ProjectService {
808808
user.githubInstallationId,
809809
);
810810

811+
const githubCode = user.githubCode;
812+
const userOAuthToken = await this.gitHubService.exchangeOAuthCodeForToken(githubCode);
813+
811814
// 4) Create the repo if the project doesn’t have it yet
812815
if (!project.githubRepoName || !project.githubOwner) {
813816
// Use project.projectName or generate a safe name
814817
const repoName = project.projectName
815818
.replace(/\s+/g, '-')
816819
.toLowerCase() // e.g. "my-project"
817-
+ '-' + Date.now(); // to make it unique if needed
820+
+ '-' + project.githubOwner; // to make it unique if needed
818821

819822
const { owner, repo, htmlUrl } = await this.gitHubService.createUserRepo(
820-
installationToken,
821823
repoName,
822824
isPublic,
823-
user.githubCode
825+
userOAuthToken
824826
);
825827

826828
project.githubRepoName = repo;
@@ -832,6 +834,8 @@ export class ProjectService {
832834
// If your projectPath is something like "/path/to/myProject",
833835
// we'll just push everything inside it, ignoring .git, node_modules, etc.
834836
const projectPath = getProjectPath(project.projectPath);
837+
838+
// delete await for now, To make it background running
835839
await this.gitHubService.pushFolderContent(
836840
installationToken,
837841
project.githubOwner,

0 commit comments

Comments
 (0)