-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.service.ts
284 lines (251 loc) · 7.55 KB
/
github.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// src/github/github.service.ts
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';
import axios from 'axios';
import * as fs from 'fs';
import * as path from 'path';
import { AppConfigService } from 'src/config/config.service';
import { Project } from 'src/project/project.model';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@Injectable()
export class GitHubService {
private readonly logger = new Logger(GitHubService.name);
private readonly appId: string;
private privateKey: string;
private ignored = ['node_modules', '.git', '.gitignore', '.env'];
constructor(
private configService: AppConfigService,
@InjectRepository(Project)
private projectsRepository: Repository<Project>,
) {
const githubEnabled = this.configService.githubEnabled;
if (!githubEnabled) {
this.logger.warn('GitHub service integration is disabled');
return;
}
this.appId = this.configService.githubAppId;
const privateKeyPath = this.configService.githubPrivateKeyPath;
if (!privateKeyPath) {
throw new Error(
'GitHub private key path is not set in environment variables',
);
}
this.logger.log(`Reading GitHub private key from: ${privateKeyPath}`);
this.privateKey = fs.readFileSync(privateKeyPath, 'utf8');
if (!this.privateKey) {
throw new Error('GitHub private key is missing!');
}
}
/**
* 1) Generate a JWT for your GitHub App using the private key.
* 2) Use that JWT to get an installation access token.
* This token allows you to act on behalf of a particular installation (user/org).
*/
async getInstallationToken(installationId: string): Promise<string> {
// 1) Create a JWT (valid for ~10 minutes)
const now = Math.floor(Date.now() / 1000);
const payload = {
iat: now, // Issued at time
exp: now + 600, // JWT expiration (10 minute maximum)
iss: this.appId, // Your GitHub App's App ID
};
const gitHubAppJwt = jwt.sign(payload, this.privateKey, {
algorithm: 'RS256',
});
// 2) Exchange JWT for an installation token
const tokenUrl = `https://api.github.com/app/installations/${installationId}/access_tokens`;
const response = await axios.post(
tokenUrl,
{},
{
headers: {
Authorization: `Bearer ${gitHubAppJwt}`,
Accept: 'application/vnd.github.v3+json',
},
},
);
const token = response.data.token;
return token;
}
async exchangeOAuthCodeForToken(code: string): Promise<string> {
const clientId = this.configService.githubClientId;
const clientSecret = this.configService.githubClientSecret;
console.log('Exchanging OAuth Code:', {
code,
clientId,
clientSecretExists: !!clientSecret,
});
try {
const response = await axios.post(
'https://github.com/login/oauth/access_token',
{
client_id: clientId,
client_secret: clientSecret,
code: code,
},
{
headers: {
Accept: 'application/json',
},
},
);
console.log('GitHub Token Exchange Response:', response.data);
if (response.data.error) {
console.error('GitHub OAuth error:', response.data);
throw new BadRequestException(
`GitHub OAuth error: ${response.data.error_description}`,
);
}
const accessToken = response.data.access_token;
if (!accessToken) {
throw new Error(
'GitHub token exchange failed: No access token returned.',
);
}
return accessToken;
} catch (error: any) {
console.error(
'OAuth exchange failed:',
error.response?.data || error.message,
);
// throw new Error(`GitHub OAuth exchange failed: ${error.response?.data?.error_description || error.message}`);
}
}
/**
* Create a new repository under the *user's* account.
* If you need an org-level repo, use POST /orgs/{org}/repos.
*/
async createUserRepo(
repoName: string,
isPublic: boolean,
userOAuthToken: string,
): Promise<{
owner: string;
repo: string;
htmlUrl: string;
}> {
const url = `https://api.github.com/user/repos`;
const response = await axios.post(
url,
{
name: repoName,
private: !isPublic, // false => public, true => private
},
{
headers: {
Authorization: `token ${userOAuthToken}`,
Accept: 'application/vnd.github.v3+json',
},
},
);
// The response will have data about the new repo
const data = response.data;
return {
owner: data.owner.login, // e.g. "octocat"
repo: data.name, // e.g. "my-new-repo"
htmlUrl: data.html_url, // e.g. "https://github.com/octocat/my-new-repo"
};
}
async pushMultipleFiles(
installationToken: string,
owner: string,
repo: string,
files: string[],
) {
for (const file of files) {
const fileName = path.basename(file);
await this.pushFileContent(
installationToken,
owner,
repo,
file,
`myFolder/${fileName}`,
'Initial commit of file ' + fileName,
);
}
}
/**
* Push a single file to the given path in the repo using GitHub Contents API.
*
* @param relativePathInRepo e.g. "backend/index.js" or "frontend/package.json"
*/
async pushFileContent(
installationToken: string,
owner: string,
repo: string,
localFilePath: string,
relativePathInRepo: string,
commitMessage: string,
) {
const fileBuffer = fs.readFileSync(localFilePath);
const base64Content = fileBuffer.toString('base64');
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${relativePathInRepo}`;
await axios.put(
url,
{
message: commitMessage,
content: base64Content,
},
{
headers: {
Authorization: `token ${installationToken}`,
Accept: 'application/vnd.github.v3+json',
},
},
);
this.logger.log(
`Pushed file: ${relativePathInRepo} -> https://github.com/${owner}/${repo}`,
);
}
/**
* Recursively push all files in a local folder to the repo.
* Skips .git, node_modules, etc. (configurable)
*/
async pushFolderContent(
installationToken: string,
owner: string,
repo: string,
folderPath: string,
basePathInRepo: string, // e.g. "" or "backend"
) {
const entries = fs.readdirSync(folderPath, { withFileTypes: true });
for (const entry of entries) {
// Skip unwanted files
if (this.ignored.includes(entry.name)) {
continue;
}
const entryPath = path.join(folderPath, entry.name);
if (entry.isDirectory()) {
// Skip unwanted directories
if (entry.name === '.git' || entry.name === 'node_modules') {
continue;
}
// Recurse into subdirectory
const subDirInRepo = path
.join(basePathInRepo, entry.name)
.replace(/\\/g, '/');
await this.pushFolderContent(
installationToken,
owner,
repo,
entryPath,
subDirInRepo,
);
} else {
// It's a file; push it
const fileInRepo = path
.join(basePathInRepo, entry.name)
.replace(/\\/g, '/');
await this.pushFileContent(
installationToken,
owner,
repo,
entryPath,
fileInRepo,
`Add file: ${fileInRepo}`,
);
}
}
}
}