|
1 | 1 | import { getConfig } from "../config.js"; |
2 | 2 | import { reviewDiff } from "../review/reviewer.js"; |
3 | 3 | import { GitHubClient } from "./client.js"; |
4 | | -import { CHECK_STATUS, CHECK_CONCLUSION, PRContext, GitHubPullRequestEvent } from "./types.js"; |
| 4 | +import { CHECK_STATUS, CHECK_CONCLUSION, GitHubPullRequestEvent } from "./types.js"; |
5 | 5 |
|
6 | 6 | export async function processReview( |
7 | 7 | jobId: string, |
@@ -78,75 +78,70 @@ export async function processReview( |
78 | 78 |
|
79 | 79 | console.log(`Retrieved diff content (${diffContent.length} chars)`); |
80 | 80 |
|
81 | | - // Create structured PR context object |
82 | | - const prContext: PRContext = { |
83 | | - owner, |
84 | | - repo, |
85 | | - pr_number: prNumber, |
86 | | - repository_id: repositoryId, |
87 | | - commit_sha: commitSha, |
88 | | - pr_url: prUrl, |
89 | | - repository_full_name: payload.repository.full_name, |
90 | | - }; |
| 81 | + // Context for agent to find PR |
| 82 | + const prContent = `Repository: ${payload.repository.full_name}\nPR Number: ${prNumber}\nCommit SHA: ${commitSha}\nPR URL: ${prUrl}`; |
91 | 83 |
|
92 | 84 | console.log(`Calling reviewDiff() for job ${jobId}`); |
93 | | - const reviewResult = await reviewDiff(diffContent, prContext, installationId); |
| 85 | + const reviewResult = await reviewDiff(diffContent, prContent, installationId.toString(), { |
| 86 | + GITHUB_INSTALLATION_ID: installationId.toString(), |
| 87 | + GITHUB_OWNER: owner, |
| 88 | + GITHUB_REPO: repo, |
| 89 | + GITHUB_PR_NUMBER: prNumber.toString(), |
| 90 | + GITHUB_APP_ID: process.env.GITHUB_APP_ID || '', |
| 91 | + GITHUB_APP_PRIVATE_KEY_PATH: process.env.GITHUB_APP_PRIVATE_KEY_PATH || '', |
| 92 | + GITHUB_APP_CWD: process.env.GITHUB_APP_CWD || '', |
| 93 | + }); |
94 | 94 | console.log(`Review completed for job ${jobId}`); |
95 | 95 |
|
96 | 96 | // Read collected comments from file |
97 | 97 | const fs = await import('fs'); |
98 | | - const commentsFilePath = reviewResult.commentsFilePath; |
| 98 | + const commentsContent = reviewResult.commentsContent; |
99 | 99 |
|
100 | | - if (fs.existsSync(commentsFilePath)) { |
| 100 | + if (commentsContent) { |
101 | 101 | try { |
102 | | - const fileContent = fs.readFileSync(commentsFilePath, 'utf8').trim(); |
103 | | - console.log(`Found comments file with ${fileContent.length} characters`); |
104 | | - |
105 | | - if (fileContent) { |
106 | | - const commentLines = fileContent.split('\n'); |
107 | | - const comments = commentLines.map(line => JSON.parse(line)); |
108 | | - |
109 | | - const inlineComments = comments.filter(c => c.type === 'inline'); |
110 | | - const generalComments = comments.filter(c => c.type === 'general'); |
111 | | - |
112 | | - // TODO(sayans): GitHub API allows <= 30 comments per review, so we need to add splitting logic if there are > 30 comments |
113 | | - |
114 | | - console.log(`Collected ${inlineComments.length} inline comments and ${generalComments.length} general comments`); |
115 | | - |
116 | | - // Create review summary from general comments |
117 | | - let reviewSummary = generalComments.length > 0 |
118 | | - ? generalComments.map(c => c.message).join('\n\n') |
119 | | - : 'Code review completed.'; |
120 | | - |
121 | | - // Append Amp thread URL if available |
122 | | - if (reviewResult.threadId && config.amp.server_url) { |
123 | | - const threadUrl = `${config.amp.server_url}/threads/${reviewResult.threadId}`; |
124 | | - reviewSummary += `\n\n[View this review on Amp](${threadUrl})`; |
125 | | - } |
126 | | - |
127 | | - // Post aggregated review |
128 | | - console.log('Posting aggregated PR review...'); |
129 | | - await githubClient.createPRReview( |
130 | | - owner, |
131 | | - repo, |
132 | | - prNumber, |
133 | | - reviewSummary, |
134 | | - 'COMMENT', |
135 | | - inlineComments.map(comment => ({ |
136 | | - path: comment.path, |
137 | | - line: comment.line, |
138 | | - body: comment.suggested_fix |
139 | | - ? `${comment.message}\n\n\`\`\`suggestion\n${comment.suggested_fix}\n\`\`\`` |
140 | | - : comment.message |
141 | | - })) |
142 | | - ); |
143 | | - console.log('PR review posted successfully'); |
144 | | - } else { |
145 | | - console.log('No comments collected, skipping review creation'); |
| 102 | + console.log(`Found comments file with ${commentsContent.length} characters`); |
| 103 | + |
| 104 | + const commentLines = commentsContent.split('\n'); |
| 105 | + const comments = commentLines.map(line => JSON.parse(line)); |
| 106 | + |
| 107 | + const inlineComments = comments.filter(c => c.type === 'inline'); |
| 108 | + const generalComments = comments.filter(c => c.type === 'general'); |
| 109 | + |
| 110 | + // TODO(sayans): GitHub API allows <= 30 comments per review, so we need to add splitting logic if there are > 30 comments |
| 111 | + |
| 112 | + console.log(`Collected ${inlineComments.length} inline comments and ${generalComments.length} general comments`); |
| 113 | + |
| 114 | + // Create review summary from general comments |
| 115 | + let reviewSummary = generalComments.length > 0 |
| 116 | + ? generalComments.map(c => c.message).join('\n\n') |
| 117 | + : 'Code review completed.'; |
| 118 | + |
| 119 | + // Append Amp thread URL if available |
| 120 | + if (reviewResult.threadId && config.amp.server_url) { |
| 121 | + const threadUrl = `${config.amp.server_url}/threads/${reviewResult.threadId}`; |
| 122 | + reviewSummary += `\n\n[View this review on Amp](${threadUrl})`; |
146 | 123 | } |
147 | 124 |
|
| 125 | + // Post aggregated review |
| 126 | + console.log('Posting aggregated PR review...'); |
| 127 | + await githubClient.createPRReview( |
| 128 | + owner, |
| 129 | + repo, |
| 130 | + prNumber, |
| 131 | + reviewSummary, |
| 132 | + 'COMMENT', |
| 133 | + inlineComments.map(comment => ({ |
| 134 | + path: comment.path, |
| 135 | + line: comment.line, |
| 136 | + body: comment.suggested_fix |
| 137 | + ? `${comment.message}\n\n\`\`\`suggestion\n${comment.suggested_fix}\n\`\`\`` |
| 138 | + : comment.message |
| 139 | + })) |
| 140 | + ); |
| 141 | + console.log('PR review posted successfully'); |
| 142 | + |
148 | 143 | // Clean up the comments file |
149 | | - fs.unlinkSync(commentsFilePath); |
| 144 | + fs.unlinkSync(reviewResult.commentsFilePath); |
150 | 145 | console.log('Cleaned up comments file'); |
151 | 146 |
|
152 | 147 | } catch (error) { |
|
0 commit comments