-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathgithub.ts
More file actions
375 lines (322 loc) · 10.6 KB
/
github.ts
File metadata and controls
375 lines (322 loc) · 10.6 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { Octokit } from "@octokit/rest";
import type { GitHubFile, GitHubFileContent, PullRequestInfo } from "./types";
import type { Ctx } from "./mod";
import { getConfig } from "../../config";
/**
* Push changes to GitHub and refresh file tree
*/
export async function pushChangesToGitHub(c: Ctx, commitMessage: string) {
// Skip if no files were modified
if (Object.keys(c.state.code.modifiedFiles).length === 0) {
console.log('[GITHUB] No files modified, skipping push to GitHub');
return true;
}
console.log(`[GITHUB] Pushing changes to GitHub: ${Object.keys(c.state.code.modifiedFiles).length} files modified`);
console.log(`[GITHUB] Branch: ${c.state.github.branchName}, Commit message: ${commitMessage}`);
// Push changes to GitHub
const result = await commitChanges(
c,
c.state.code.modifiedFiles,
c.state.github.branchName,
commitMessage
);
if (result) {
console.log(`[GITHUB] Successfully pushed changes to branch: ${c.state.github.branchName}`);
// Successfully pushed changes, clear modified files
c.state.code.modifiedFiles = {};
// Refresh file tree
c.state.code.fileTree = await getFileTree(c, c.state.github.branchName);
} else {
console.error(`[GITHUB] Failed to push changes to branch: ${c.state.github.branchName}`);
}
return result;
}
/**
* Create a new branch from the specified base branch
*/
export async function createBranch(c: Ctx, branchName: string, baseBranch: string): Promise<boolean> {
try {
console.log(`[GITHUB] Creating new branch: ${branchName} from ${baseBranch}`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
// Get the SHA of the base branch
const { data: refData } = await octokit.git.getRef({
owner: c.state.github.owner,
repo: c.state.github.repo,
ref: `heads/${baseBranch}`,
});
console.log(`[GITHUB] Retrieved base branch reference: ${baseBranch} (${refData.object.sha})`);
// Create a new branch
await octokit.git.createRef({
owner: c.state.github.owner,
repo: c.state.github.repo,
ref: `refs/heads/${branchName}`,
sha: refData.object.sha,
});
console.log(`[GITHUB] Branch created successfully: ${branchName}`);
return true;
} catch (error) {
console.error(`[GITHUB] Failed to create branch ${branchName}:`, error);
return false;
}
}
/**
* Get the file tree for the specified branch
*/
export async function getFileTree(c: Ctx, branch: string): Promise<GitHubFile[]> {
try {
console.log(`[GITHUB] Fetching file tree for ${c.state.github.repo} (${branch})`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
// Get the latest commit on the branch
const { data: refData } = await octokit.git.getRef({
owner: c.state.github.owner,
repo: c.state.github.repo,
ref: `heads/${branch}`,
});
// Get the tree of that commit
const { data: treeData } = await octokit.git.getTree({
owner: c.state.github.owner,
repo: c.state.github.repo,
tree_sha: refData.object.sha,
recursive: "1",
});
// Map to our GitHubFile type
const files = treeData.tree.map((item) => ({
path: item.path || "",
type: item.type === "blob" ? "file" : "directory" as "file" | "directory",
sha: item.sha,
}));
console.log(`[GITHUB] File tree fetched successfully: ${files.length} files`);
return files;
} catch (error) {
console.error(`[GITHUB] Failed to get file tree for branch ${branch}:`, error);
return [];
}
}
/**
* Read the contents of a file from the specified branch
*/
export async function readFile(c: Ctx, path: string, branch: string): Promise<GitHubFileContent | null> {
try {
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
const { data } = await octokit.repos.getContent({
owner: c.state.github.owner,
repo: c.state.github.repo,
path,
ref: branch,
});
// Handle case when data is an array (directory) instead of a file
if (Array.isArray(data)) {
throw new Error(`Path ${path} is a directory, not a file`);
}
// Handle case where content might be undefined
if (!("content" in data)) {
throw new Error(`No content found for ${path}`);
}
// Decode base64 content
const content = Buffer.from(data.content, "base64").toString();
return {
content,
sha: data.sha,
path,
};
} catch (error) {
console.error(`[GITHUB] Failed to read file ${path}:`, error);
return null;
}
}
/**
* Read multiple files at once
*/
export async function readFiles(c: Ctx, paths: string[], branch: string): Promise<Record<string, GitHubFileContent | null>> {
const results: Record<string, GitHubFileContent | null> = {};
await Promise.all(
paths.map(async (path) => {
results[path] = await readFile(c, path, branch);
}),
);
return results;
}
/**
* Commit changes to files
*/
export async function commitChanges(
c: Ctx,
files: Record<string, string>,
branch: string,
message: string,
): Promise<boolean> {
try {
console.log(`[GITHUB] Committing changes to ${branch}: ${Object.keys(files).length} files modified`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
// First get the current commit to use as parent
const { data: refData } = await octokit.git.getRef({
owner: c.state.github.owner,
repo: c.state.github.repo,
ref: `heads/${branch}`,
});
const commitSha = refData.object.sha;
// Get the current tree
const { data: commitData } = await octokit.git.getCommit({
owner: c.state.github.owner,
repo: c.state.github.repo,
commit_sha: commitSha,
});
const treeSha = commitData.tree.sha;
// Create blobs for each file
const blobPromises = Object.entries(files).map(
async ([path, content]) => {
const { data } = await octokit.git.createBlob({
owner: c.state.github.owner,
repo: c.state.github.repo,
content,
encoding: "utf-8",
});
return {
path,
mode: "100644" as const, // Regular file
type: "blob" as const,
sha: data.sha,
};
},
);
const blobs = await Promise.all(blobPromises);
// Create a new tree
const { data: newTree } = await octokit.git.createTree({
owner: c.state.github.owner,
repo: c.state.github.repo,
base_tree: treeSha,
tree: blobs,
});
// Create a new commit
const { data: newCommit } = await octokit.git.createCommit({
owner: c.state.github.owner,
repo: c.state.github.repo,
message,
tree: newTree.sha,
parents: [commitSha],
});
// Update the reference
await octokit.git.updateRef({
owner: c.state.github.owner,
repo: c.state.github.repo,
ref: `heads/${branch}`,
sha: newCommit.sha,
});
console.log(`[GITHUB] Changes committed successfully to ${branch}`);
return true;
} catch (error) {
console.error(`[GITHUB] Failed to commit changes to ${branch}:`, error);
return false;
}
}
/**
* Create a pull request
*/
export async function createPullRequest(
c: Ctx,
title: string,
body: string,
head: string,
base: string,
): Promise<PullRequestInfo | null> {
try {
console.log(`[GITHUB] Creating pull request: ${head} → ${base}`);
console.log(`[GITHUB] PR title: ${title}`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
// First check if there are any commits between branches
try {
const { data: comparison } = await octokit.repos.compareCommits({
owner: c.state.github.owner,
repo: c.state.github.repo,
base,
head,
});
if (comparison.total_commits === 0) {
console.error(`[GITHUB] Cannot create PR: No commits between ${base} and ${head}`);
return {
id: 0,
number: 0,
url: `https://github.com/${c.state.github.owner}/${c.state.github.repo}/tree/${head}`,
noDiff: true
};
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.warn(`[GITHUB] Could not compare branches: ${errorMessage}`);
// Continue anyway and let the PR creation attempt fail if needed
}
const { data } = await octokit.pulls.create({
owner: c.state.github.owner,
repo: c.state.github.repo,
title,
body,
head,
base,
});
console.log(`[GITHUB] Pull request created successfully: #${data.number} (${data.html_url})`);
return {
id: data.id,
number: data.number,
url: data.html_url,
};
} catch (error) {
if (error instanceof Error && error.message && error.message.includes("No commits between")) {
console.error(`[GITHUB] Failed to create PR: No commits between branches`);
return {
id: 0,
number: 0,
url: `https://github.com/${c.state.github.owner}/${c.state.github.repo}/tree/${head}`,
noDiff: true
};
}
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(`[GITHUB] Failed to create pull request:`, errorMessage);
return null;
}
}
/**
* Merge a pull request
*/
export async function mergePullRequest(c: Ctx, prNumber: number): Promise<boolean> {
try {
console.log(`[GITHUB] Merging pull request #${prNumber}`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
await octokit.pulls.merge({
owner: c.state.github.owner,
repo: c.state.github.repo,
pull_number: prNumber,
});
console.log(`[GITHUB] Pull request #${prNumber} merged successfully`);
return true;
} catch (error) {
console.error(`[GITHUB] Failed to merge PR #${prNumber}:`, error);
return false;
}
}
/**
* Close a pull request
*/
export async function closePullRequest(c: Ctx, prNumber: number): Promise<boolean> {
try {
console.log(`[GITHUB] Closing pull request #${prNumber}`);
const config = getConfig();
const octokit = new Octokit({ auth: config.githubToken });
await octokit.pulls.update({
owner: c.state.github.owner,
repo: c.state.github.repo,
pull_number: prNumber,
state: "closed",
});
console.log(`[GITHUB] Pull request #${prNumber} closed successfully`);
return true;
} catch (error) {
console.error(`[GITHUB] Failed to close PR #${prNumber}:`, error);
return false;
}
}