Skip to content

Commit 7a5530e

Browse files
author
Tulga Tsogtgerel
committed
refactor: let Auggie agent create PR instead of action code
- Remove createPRForChanges function - Update invokeAuggie to accept shouldCreatePR parameter - For /pr command, instruct Auggie to create branch, commit, and create PR using its tools - This avoids needing PAT_TOKEN since Auggie has GitHub API access - Simplifies setup and makes it work out of the box
1 parent 7dd2275 commit 7a5530e

File tree

1 file changed

+37
-131
lines changed

1 file changed

+37
-131
lines changed

assistant/src/index.ts

Lines changed: 37 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -250,87 +250,10 @@ Automatically generated by Auggie Assistant`;
250250
core.info('✅ Changes committed and pushed successfully');
251251
}
252252

253-
/**
254-
* Create a new branch, commit changes, and create a PR targeting the original PR
255-
*/
256-
async function createPRForChanges(
257-
commentId: number,
258-
headBranch: string,
259-
githubToken: string,
260-
owner: string,
261-
repo: string,
262-
prNumber: number,
263-
octokit: Octokit
264-
): Promise<{ newBranch: string; newPrNumber: number }> {
265-
core.info('📝 Creating new branch for PR...');
266-
267-
// Stage all changes
268-
await exec.exec('git', ['add', '-A']);
269-
270-
// Check if there are changes to commit using git status
271-
let statusOutput = '';
272-
await exec.exec('git', ['status', '--porcelain'], {
273-
listeners: {
274-
stdout: (data: Buffer) => {
275-
statusOutput += data.toString();
276-
},
277-
},
278-
});
279-
280-
const hasChanges = statusOutput.trim().length > 0;
281-
282-
if (!hasChanges) {
283-
core.info('ℹ️ No changes to commit');
284-
throw new Error('No changes to create PR with');
285-
}
286-
287-
core.info(`📝 Changes detected:\n${statusOutput}`);
288-
289-
// Create a new branch name based on the comment ID
290-
const newBranch = `auggie/pr-${prNumber}-comment-${commentId}`;
291-
core.info(`🌿 Creating new branch: ${newBranch}`);
292-
293-
// Create and checkout new branch
294-
await exec.exec('git', ['checkout', '-b', newBranch]);
295-
296-
// Commit changes
297-
const commitMessage = `feat: implement changes requested in comment #${commentId}
298-
299-
Automatically generated by Auggie Assistant`;
300-
301-
await exec.exec('git', ['commit', '-m', commitMessage]);
302-
303-
// Push new branch
304-
core.info(`🚀 Pushing to ${newBranch}...`);
305-
const remoteUrl = `https://x-access-token:${githubToken}@github.com/${owner}/${repo}.git`;
306-
await exec.exec('git', ['push', remoteUrl, `HEAD:${newBranch}`]);
307-
308-
core.info('✅ Changes committed and pushed to new branch');
309-
310-
// Create PR targeting the original PR's branch
311-
core.info(`📬 Creating PR targeting ${headBranch}...`);
312-
const { data: newPr } = await octokit.rest.pulls.create({
313-
owner,
314-
repo,
315-
title: `Changes from comment #${commentId} on PR #${prNumber}`,
316-
body: `This PR contains changes requested in [comment #${commentId}](https://github.com/${owner}/${repo}/pull/${prNumber}#issuecomment-${commentId}) on PR #${prNumber}.
317-
318-
Automatically generated by Auggie Assistant.
319-
320-
**Target Branch:** \`${headBranch}\` (from PR #${prNumber})`,
321-
head: newBranch,
322-
base: headBranch,
323-
});
324-
325-
core.info(`✅ Created PR #${newPr.number}`);
326-
327-
return { newBranch, newPrNumber: newPr.number };
328-
}
329-
330253
/**
331254
* Invoke Auggie to implement changes
332255
*/
333-
async function invokeAuggie(context: PRContext): Promise<void> {
256+
async function invokeAuggie(context: PRContext, shouldCreatePR: boolean): Promise<void> {
334257
core.info('🤖 Invoking Auggie to implement changes...');
335258

336259
const augmentApiToken = process.env.AUGMENT_API_TOKEN;
@@ -343,7 +266,7 @@ async function invokeAuggie(context: PRContext): Promise<void> {
343266
}
344267

345268
// Build context for Auggie
346-
const instruction = `You are an AI assistant helping to implement code changes in a Pull Request.
269+
const baseInstruction = `You are an AI assistant helping to implement code changes in a Pull Request.
347270
348271
## User Request
349272
${context.commentBody}
@@ -367,7 +290,23 @@ ${context.diff}
367290
368291
## Instructions
369292
You MUST actually implement the requested changes by editing the files. Do NOT just describe what you would do.
370-
Use the str-replace-editor, save-file, or other file editing tools to make the actual changes to the code.
293+
Use the str-replace-editor, save-file, or other file editing tools to make the actual changes to the code.`;
294+
295+
const instruction = shouldCreatePR
296+
? `${baseInstruction}
297+
298+
After making the changes, you MUST:
299+
1. Create a new branch named "auggie/pr-${context.prNumber}-comment-{commentId}" (use the actual comment ID from the context)
300+
2. Commit all your changes to this new branch with message: "feat: implement changes requested in comment #{commentId}"
301+
3. Push the new branch to origin
302+
4. Create a PR using the github-api tool targeting the "${context.headBranch}" branch with:
303+
- Title: "Changes from comment on PR #${context.prNumber}"
304+
- Body: A description of what you implemented
305+
5. Provide the PR number in your response
306+
307+
Use git commands and the github-api tool to accomplish this.`
308+
: `${baseInstruction}
309+
371310
After making the changes, provide a brief summary of what you implemented.`;
372311

373312
core.info('📤 Sending request to Auggie...');
@@ -521,68 +460,35 @@ async function main(): Promise<void> {
521460

522461
// Step 5: Invoke Auggie to implement changes
523462
try {
524-
await invokeAuggie(context);
525-
526-
// Step 6: Commit and push changes (different behavior for /pr vs /commit)
527-
if (isPrCommand) {
528-
// For /pr: create a new branch and PR targeting the original PR
529-
const { newPrNumber } = await createPRForChanges(
530-
commentId,
531-
context.headBranch,
532-
githubToken,
533-
owner,
534-
repo,
535-
prNumber,
536-
octokit
537-
);
538-
539-
// Step 7: Add success comment to original PR
540-
const quotedComment = context.commentBody
541-
.split('\n')
542-
.map(line => `> ${line}`)
543-
.join('\n');
544-
545-
await addPRComment(
546-
octokit,
547-
owner,
548-
repo,
549-
prNumber,
550-
`${quotedComment}
463+
await invokeAuggie(context, isPrCommand);
551464

552-
✅ Successfully implemented the requested changes!
553-
554-
Created PR #${newPrNumber} with the changes targeting \`${context.headBranch}\`.
465+
// Step 6: Commit and push changes (only for /commit, /pr is handled by Auggie)
466+
if (!isPrCommand) {
467+
await commitAndPush(commentId, context.headBranch, githubToken, owner, repo);
468+
}
555469

556-
👉 [View the new PR](https://github.com/${owner}/${repo}/pull/${newPrNumber})`
557-
);
470+
// Step 7: Add success comment to PR
471+
const quotedComment = context.commentBody
472+
.split('\n')
473+
.map(line => `> ${line}`)
474+
.join('\n');
558475

559-
core.info(`✨ PR Assistant completed successfully - created PR #${newPrNumber}`);
560-
} else {
561-
// For /commit: commit directly to the PR branch (existing behavior)
562-
await commitAndPush(commentId, context.headBranch, githubToken, owner, repo);
476+
const successMessage = isPrCommand
477+
? `${quotedComment}
563478
564-
// Step 7: Add success comment to PR
565-
const quotedComment = context.commentBody
566-
.split('\n')
567-
.map(line => `> ${line}`)
568-
.join('\n');
479+
✅ Successfully implemented the requested changes!
569480
570-
await addPRComment(
571-
octokit,
572-
owner,
573-
repo,
574-
prNumber,
575-
`${quotedComment}
481+
The agent has created a new PR with the changes targeting \`${context.headBranch}\`. Check the agent's response above for the PR link.`
482+
: `${quotedComment}
576483
577484
✅ Successfully implemented the requested changes!
578485
579-
The changes have been committed to the \`${context.headBranch}\` branch.`
580-
);
486+
The changes have been committed to the \`${context.headBranch}\` branch.`;
581487

582-
core.info('✨ PR Assistant completed successfully');
583-
}
488+
await addPRComment(octokit, owner, repo, prNumber, successMessage);
584489

585490
core.setOutput('success', 'true');
491+
core.info('✨ PR Assistant completed successfully');
586492
} catch (error) {
587493
// Add failure comment to PR
588494
const quotedComment = context.commentBody

0 commit comments

Comments
 (0)