-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgithub.ts
More file actions
633 lines (559 loc) · 21.9 KB
/
github.ts
File metadata and controls
633 lines (559 loc) · 21.9 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import * as core from '@actions/core';
import * as github from '@actions/github';
import { execaSync } from 'execa';
import * as fs from 'fs';
import { genContentsString } from './contents.js';
import { Octokit } from 'octokit';
// --- Type Definitions ---
export type AgentEvent =
| { type: 'issuesOpened', github: GitHubEventIssuesOpened }
| { type: 'issueCommentCreated', github: GitHubEventIssueCommentCreated }
| { type: 'pullRequestCommentCreated', github: GitHubEventPullRequestCommentCreated }
| { type: 'pullRequestReviewCommentCreated', github: GitHubEventPullRequestReviewCommentCreated }
;
export type GitHubEvent =
| GitHubEventIssuesOpened
| GitHubEventIssueCommentCreated
| GitHubEventPullRequestCommentCreated
| GitHubEventPullRequestReviewCommentCreated;
export type GitHubEventIssuesOpened = {
action: 'opened';
issue: GitHubIssue;
}
export type GitHubEventIssueCommentCreated = {
action: 'created';
issue: GitHubIssue;
comment: GithubComment;
}
export type GitHubEventPullRequestCommentCreated = {
action: 'created';
issue: GitHubPullRequest;
comment: GithubComment;
}
export type GitHubEventPullRequestReviewCommentCreated = {
action: 'created';
pull_request: {
number: number;
title?: string;
body?: string;
};
comment: {
id: number;
body: string;
path: string;
in_reply_to_id?: number;
position?: number;
line?: number;
};
}
export type GithubComment = {
id: number;
body: string;
}
export type GitHubIssue = {
number: number;
title: string;
body: string;
pull_request: null;
}
export type GitHubPullRequest = {
number: number;
title: string;
body: string;
pull_request: {
url: string;
};
}
export type GithubContentsData = {
content: { number: number; title: string; body: string; login: string };
comments: { body: string; login: string }[];
};
type RepoContext = { owner: string; repo: string };
// --- Functions ---
/**
* Clones the repository based on the event type.
*/
export async function cloneRepository(
workspace: string,
githubToken: string,
repo: RepoContext,
context: typeof github.context,
octokit: Octokit,
event: AgentEvent
): Promise<void> {
const cloneUrl = context.payload.repository?.clone_url;
if (!cloneUrl) {
throw new Error('Repository clone URL not found');
}
// Determine branch to clone
let branchToClone: string;
if (event.type === 'pullRequestCommentCreated' || event.type === 'pullRequestReviewCommentCreated') {
// For PR comments, clone the PR's head branch
const prNumber = event.type === 'pullRequestCommentCreated' ? event.github.issue.number : event.github.pull_request.number;
try {
const prData = await octokit.rest.pulls.get({ ...repo, pull_number: prNumber });
branchToClone = prData.data.head.ref;
core.info(`Cloning PR branch: ${branchToClone}`);
} catch (e) {
throw new Error(`Could not get PR branch from API: ${e}`);
}
} else {
// For issues or other events, clone the default branch
branchToClone = context.payload.repository?.default_branch;
if (!branchToClone) {
throw new Error('Default branch not found');
}
core.info(`Cloning default branch: ${branchToClone}`);
}
// Clone the repository
core.info(`Cloning repository ${cloneUrl} branch ${branchToClone} into ${workspace}`);
try {
// Ensure the workspace directory exists and is empty or doesn't exist
if (fs.existsSync(workspace)) {
fs.rmSync(workspace, { recursive: true, force: true });
}
fs.mkdirSync(workspace, { recursive: true });
// Use token for authentication with clone URL
const authenticatedCloneUrl = cloneUrl.replace('https://', `https://x-access-token:${githubToken}@`);
execaSync('git', ['clone', '--depth', '1', '--branch', branchToClone, authenticatedCloneUrl, '.'], { cwd: workspace, stdio: 'inherit' });
core.info('Repository cloned successfully.');
} catch (error) {
throw new Error(`Failed to clone repository: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Determines the type of GitHub event.
*/
export function getEventType(payload: any): AgentEvent | null {
if (payload.action === 'opened' && payload.issue && !payload.issue.pull_request) {
return { type: 'issuesOpened', github: payload };
}
if (payload.action === 'created' && payload.issue && !payload.issue.pull_request && payload.comment) {
return { type: 'issueCommentCreated', github: payload };
}
// Check if payload.issue exists before accessing its properties
if (payload.action === 'created' && payload.issue && payload.issue.pull_request && payload.comment) {
return { type: 'pullRequestCommentCreated', github: payload };
}
// Check for Pull Request Review Comment (comment on a specific line of code)
if (payload.action === 'created' && payload.pull_request && payload.comment && payload.comment.path) {
return { type: 'pullRequestReviewCommentCreated', github: payload };
}
return null;
}
/**
* Adds an 'eyes' reaction to the event source (issue or comment).
*/
export async function addEyeReaction(
octokit: Octokit,
repo: RepoContext,
event: GitHubEvent
): Promise<void> {
try {
if (event.action === 'opened' && 'issue' in event) {
// Add eye reaction to issue
await octokit.rest.reactions.createForIssue({
...repo,
issue_number: event.issue.number,
content: 'eyes'
});
core.info(`Added eye reaction to issue #${event.issue.number}`);
} else if (event.action === 'created' && 'comment' in event && 'issue' in event) {
// Add eye reaction to comment on issue or PR conversation
await octokit.rest.reactions.createForIssueComment({
...repo,
comment_id: event.comment.id,
content: 'eyes'
});
core.info(`Added eye reaction to comment on issue/PR #${event.issue.number}`);
} else if (event.action === 'created' && 'comment' in event && 'pull_request' in event) {
// Add eye reaction to PR review comment
await octokit.rest.reactions.createForPullRequestReviewComment({
...repo,
comment_id: event.comment.id,
content: 'eyes'
});
core.info(`Added eye reaction to review comment on PR #${event.pull_request.number}`);
}
} catch (error) {
core.warning(`Failed to add reaction: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Extracts the relevant text (body or comment) from the event payload.
*/
export function extractText(event: GitHubEvent): string | null {
if (event.action === 'opened' && 'issue' in event) {
return event.issue.body;
}
// Ensure 'comment' exists before accessing 'body' for issue/PR comments
if (event.action === 'created' && 'comment' in event && event.comment) {
return event.comment.body;
}
return null;
}
/**
* Creates a Pull Request with the changes.
*/
export async function createPullRequest(
workspace: string,
octokit: Octokit,
repo: RepoContext,
event: GitHubEventIssuesOpened | GitHubEventIssueCommentCreated,
commitMessage: string,
output: string
): Promise<void> {
const issueNumber = event.issue.number;
let branchName = `code-agent-changes-${issueNumber}`;
if (event.action == "created") {
branchName = `code-agent-changes-${issueNumber}-${event.comment.id}`;
}
const baseBranch = github.context.payload.repository?.default_branch; // Get default branch for base
if (!baseBranch) {
throw new Error('Could not determine the default branch to use as base for the PR.');
}
try {
// Set up Git and create a new branch
core.info('Configuring Git user identity locally...');
execaSync('git', ['config', 'user.name', 'github-actions[bot]'], { cwd: workspace, stdio: 'inherit' });
execaSync('git', ['config', 'user.email', 'github-actions[bot]@users.noreply.github.com'], { cwd: workspace, stdio: 'inherit' });
core.info(`Creating new branch: ${branchName}`);
execaSync('git', ['checkout', '-b', branchName], { cwd: workspace, stdio: 'inherit' });
core.info('Adding changed files to Git...');
execaSync('git', ['add', '-A'], { cwd: workspace, stdio: 'inherit' });
core.info('Committing changes...');
execaSync('git', ['commit', '-m', commitMessage], { cwd: workspace, stdio: 'inherit' });
core.info(`Pushing changes to origin/${branchName}...`);
execaSync('git', ['push', 'origin', branchName, '--force'], { cwd: workspace, stdio: 'inherit' }); // Use force push for simplicity in case branch exists
core.info('Creating Pull Request...');
const pr = await octokit.rest.pulls.create({
...repo,
title: `Code Agent changes for #${issueNumber}: ${commitMessage}`,
head: branchName,
base: baseBranch, // Use the default branch as base
body: `Applied changes based on Issue #${issueNumber}.\n\n${output}`,
maintainer_can_modify: true,
});
core.info(`Pull Request created: ${pr.data.html_url}`);
// Optionally, post a comment linking to the PR in the original issue
await octokit.rest.issues.createComment({
...repo,
issue_number: issueNumber,
body: `Created Pull Request #${pr.data.number}`,
});
} catch (error) {
core.error(`Error creating Pull Request: ${error}`);
throw new Error(`Failed to create Pull Request: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Commits and pushes changes to the existing PR branch.
*/
export async function commitAndPush(
workspace: string,
octokit: Octokit,
repo: RepoContext,
event: GitHubEventPullRequestCommentCreated | GitHubEventPullRequestReviewCommentCreated,
commitMessage: string,
output: string
): Promise<void> {
// Get PR number from the event - different location based on event type
const prNumber = 'issue' in event ? event.issue.number : event.pull_request.number;
try {
// Get current branch name from the PR context
let currentBranch: string;
try {
const prData = await octokit.rest.pulls.get({ ...repo, pull_number: prNumber });
currentBranch = prData.data.head.ref;
core.info(`Checked out PR branch: ${currentBranch}`);
// Ensure we are on the correct branch
execaSync('git', ['checkout', currentBranch], { cwd: workspace, stdio: 'inherit' });
} catch (e) {
// Fallback if PR data fetch fails (should ideally not happen in this context)
core.warning(`Could not get PR branch from API, attempting to use current branch: ${e}`);
const branchResult = execaSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: workspace });
currentBranch = branchResult.stdout.trim();
core.info(`Using current branch from git: ${currentBranch}`);
// Ensure we are on the correct branch if the checkout happened before the action ran
execaSync('git', ['checkout', currentBranch], { cwd: workspace, stdio: 'inherit' });
}
core.info('Configuring Git user identity locally...');
execaSync('git', ['config', 'user.name', 'github-actions[bot]'], { cwd: workspace, stdio: 'inherit' });
execaSync('git', ['config', 'user.email', 'github-actions[bot]@users.noreply.github.com'], { cwd: workspace, stdio: 'inherit' });
core.info('Adding changed files to Git...');
// Add all changed files (including deleted ones)
execaSync('git', ['add', '-A'], { cwd: workspace, stdio: 'inherit' });
// Check if there are changes to commit
const statusResult = execaSync('git', ['status', '--porcelain'], { cwd: workspace });
if (!statusResult.stdout.trim()) {
core.info('No changes to commit.');
// Post a comment indicating no changes were made or output if relevant
await postComment(octokit, repo, event, `${output}`);
return; // Exit early if no changes
}
core.info('Committing changes...');
execaSync('git', ['commit', '-m', commitMessage], { cwd: workspace, stdio: 'inherit' });
core.info(`Pushing changes to origin/${currentBranch}...`);
execaSync('git', ['push', 'origin', currentBranch], { cwd: workspace, stdio: 'inherit' });
core.info('Changes committed and pushed.');
// Post a comment confirming the changes
await postComment(octokit, repo, event, `${output}`);
} catch (error) {
core.error(`Error committing and pushing changes: ${error}`);
// Attempt to post an error comment
try {
await postComment(octokit, repo, event, `Failed to apply changes to this PR: ${error instanceof Error ? error.message : String(error)}`);
} catch (commentError) {
core.error(`Failed to post error comment: ${commentError}`);
}
throw new Error(`Failed to commit and push changes: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Posts a comment to the issue or PR.
*/
export async function postComment(
octokit: Octokit,
repo: RepoContext,
event: GitHubEvent,
body: string
): Promise<void> {
try {
if ('issue' in event) {
// For regular issues and PR conversation comments
const issueNumber = event.issue.number;
await octokit.rest.issues.createComment({
...repo,
issue_number: issueNumber,
body: body,
});
core.info(`Comment posted to Issue/PR #${issueNumber}`);
} else if ('pull_request' in event) {
// For PR review comments
const prNumber = event.pull_request.number;
const commentId = event.comment.id;
const inReplyTo = event.comment.in_reply_to_id;
try {
await octokit.rest.pulls.createReplyForReviewComment({
...repo,
pull_number: prNumber,
comment_id: inReplyTo ?? commentId, // Use the original comment ID if no reply
body: body,
});
core.info(`Comment posted to PR #${prNumber} Reply to comment #${commentId}`);
} catch (commentError) {
// If we can't determine if it's a top-level comment, fall back to creating a regular PR comment
core.warning(`Failed to check if comment is top-level: ${commentError instanceof Error ? commentError.message : commentError}`);
core.info(`Falling back to creating a regular PR comment instead of a reply`);
await octokit.rest.issues.createComment({
...repo,
issue_number: prNumber,
body: body,
});
core.info(`Regular comment posted to PR #${prNumber}`);
}
}
} catch (error) {
core.error(`Failed to post comment: ${error instanceof Error ? error.message : error}`);
// Don't re-throw here, as posting a comment failure might not be critical
}
}
export async function generatePrompt(
octokit: Octokit,
repo: RepoContext,
event: AgentEvent,
userPrompt: string
): Promise<string> {
if (event.type === 'issuesOpened') {
return userPrompt;
}
const contents = await getContentsData(octokit, repo, event);
let prFiles: string[] = [];
let contextInfo: string = '';
if (event.type === 'pullRequestCommentCreated' || event.type === 'pullRequestReviewCommentCreated') {
// Get the changed files in the PR
prFiles = await getChangedFiles(octokit, repo, event);
}
// For PR review comments, add information about the file path and line
if (event.type === 'pullRequestReviewCommentCreated') {
const comment = event.github.comment;
contextInfo = `Comment on file: ${comment.path}`;
if (comment.line) {
contextInfo += `, line: ${comment.line}`;
}
}
let historyPropmt = genContentsString(contents.content, userPrompt);
for (const comment of contents.comments) {
historyPropmt += genContentsString(comment, userPrompt);
}
let prompt = "";
if (historyPropmt) {
prompt += `[History]\n${historyPropmt}\n\n`;
}
if (contextInfo) {
prompt += `[Context]\n${contextInfo}\n\n`;
}
if (prFiles.length > 0) {
prompt += `[Changed Files]\n${prFiles.join('\n')}\n\n`;
}
if (prompt) {
prompt += `---\n\n${userPrompt}`;
} else {
prompt = userPrompt;
}
return prompt;
}
export async function getChangedFiles(
octokit: Octokit,
repo: RepoContext,
event: AgentEvent
): Promise<string[]> {
let prNumber: number;
if (event.type === 'pullRequestCommentCreated') {
prNumber = event.github.issue.number;
} else if (event.type === 'pullRequestReviewCommentCreated') {
prNumber = event.github.pull_request.number;
} else {
throw new Error(`Cannot get changed files for event type: ${event.type}`);
}
const prFilesResponse = await octokit.rest.pulls.listFiles({
...repo,
pull_number: prNumber,
});
return prFilesResponse.data.map(file => file.filename);
}
export async function getContentsData(
octokit: Octokit,
repo: RepoContext,
event: AgentEvent
): Promise<GithubContentsData> {
if (event.type === 'issuesOpened' || event.type === 'issueCommentCreated') {
return await getIssueData(octokit, repo, event.github.issue.number);
} else if (event.type === 'pullRequestCommentCreated') {
return await getPullRequestData(octokit, repo, event.github.issue.number);
} else if (event.type === 'pullRequestReviewCommentCreated') {
return await getPullRequestReviewCommentsData(octokit, repo, event.github.pull_request.number, event.github.comment.in_reply_to_id ?? event.github.comment.id);
}
throw new Error('Invalid event type for data retrieval');
}
/**
* Retrieves the body and all comment bodies for a specific issue.
*/
async function getIssueData(
octokit: Octokit,
repo: RepoContext,
issueNumber: number
): Promise<GithubContentsData> {
core.info(`Fetching data for issue #${issueNumber}...`);
try {
// Get issue body
const issueResponse = await octokit.rest.issues.get({
...repo,
issue_number: issueNumber,
});
const content = {
number: issueResponse.data.number,
title: issueResponse.data.title,
body: issueResponse.data.body ?? '',
login: issueResponse.data.user?.login ?? 'anonymous'
};
// Get all issue comments by using paginate
const commentsData = await octokit.paginate(octokit.rest.issues.listComments, {
...repo,
issue_number: issueNumber,
per_page: 100, // Fetch 100 per page for efficiency
});
const comments = commentsData.map(comment => ({
body: comment.body ?? '',
login: comment.user?.login ?? 'anonymous'
})); // Extract comment bodies and authors
core.info(`Fetched ${commentsData.length} comments for issue #${issueNumber}.`);
return { content, comments };
} catch (error) {
core.error(`Failed to get data for issue #${issueNumber}: ${error}`);
throw new Error(`Could not retrieve data for issue #${issueNumber}: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Retrieves the body and all review comment bodies for a specific pull request.
* Note: PR review comments are fetched via the pulls API endpoint.
*/
async function getPullRequestReviewCommentsData(
octokit: Octokit,
repo: RepoContext,
pullNumber: number,
targetCommentId: number
): Promise<GithubContentsData> {
core.info(`Fetching data for pull request review comments #${pullNumber}...`);
try {
// Get PR body
const prResponse = await octokit.rest.pulls.get({
...repo,
pull_number: pullNumber,
});
const content = {
number: prResponse.data.number,
title: prResponse.data.title,
body: prResponse.data.body ?? '',
login: prResponse.data.user?.login ?? 'anonymous'
};
// Get PR review comments
const commentsData = await octokit.paginate(octokit.rest.pulls.listReviewComments, {
...repo,
pull_number: pullNumber,
per_page: 100, // Fetch 100 per page for efficiency
});
// Filter comments to include only those related to the target comment ID
const comments = commentsData.filter(comment => comment.id === targetCommentId || comment.in_reply_to_id === targetCommentId).map(comment => ({
body: comment.body ?? '',
login: comment.user?.login ?? 'anonymous'
}));
core.info(`Fetched ${commentsData.length} review comments for PR #${pullNumber}.`);
return { content, comments };
} catch (error) {
core.error(`Failed to get data for pull request review comments #${pullNumber}: ${error}`);
throw new Error(`Could not retrieve data for pull request review comments #${pullNumber}: ${error instanceof Error ? error.message : error}`);
}
}
/**
* Retrieves the body and all comment bodies for a specific pull request.
* Note: PR comments are fetched via the issues API endpoint.
*/
async function getPullRequestData(
octokit: Octokit,
repo: RepoContext,
pullNumber: number
): Promise<GithubContentsData> {
core.info(`Fetching data for pull request #${pullNumber}...`);
try {
// Get PR body
const prResponse = await octokit.rest.pulls.get({
...repo,
pull_number: pullNumber,
});
const content = {
number: prResponse.data.number,
title: prResponse.data.title,
body: prResponse.data.body ?? '',
login: prResponse.data.user?.login ?? 'anonymous'
};
// Get all PR comments by using paginate (using the issues API endpoint for the corresponding issue number)
const commentsData = await octokit.paginate(octokit.rest.issues.listComments, {
...repo,
issue_number: pullNumber, // Use pullNumber as issue_number for comments
per_page: 100, // Fetch 100 per page for efficiency
});
const comments = commentsData.map(comment => ({
body: comment.body ?? '',
login: comment.user?.login ?? 'unknown'
}));
core.info(`Fetched ${commentsData.length} comments for PR #${pullNumber}.`);
// Note: This fetches *issue comments* on the PR. To get *review comments* (comments on specific lines of code),
// you would use `octokit.paginate(octokit.rest.pulls.listReviewComments, { ... })`.
// The current request asks for "all comments written on the PR", which typically refers to the main conversation thread (issue comments).
return { content, comments };
} catch (error) {
core.error(`Failed to get data for pull request #${pullNumber}: ${error}`);
throw new Error(`Could not retrieve data for pull request #${pullNumber}: ${error instanceof Error ? error.message : error}`);
}
}