@@ -211,44 +211,103 @@ jobs:
211
211
console.log(`💬 Posting ${results.inlineComments.length} inline comments...`);
212
212
213
213
try {
214
- // Get the PR files and their diff hunks for proper positioning
215
- const prFiles = await github.rest.pulls.listFiles ({
214
+ // Get the PR info first for commit SHA
215
+ const prInfo = await github.rest.pulls.get ({
216
216
owner: context.repo.owner,
217
217
repo: context.repo.repo,
218
218
pull_number: parseInt(prNumber)
219
219
});
220
220
221
- // Get the latest commit SHA for the PR
222
- const prData = await github.rest.pulls.get({
221
+ const commitSha = prInfo.data.head.sha;
222
+ console.log(`📍 Using commit SHA: ${commitSha}`);
223
+
224
+ // Get the PR diff separately
225
+ const prDiff = await github.rest.pulls.get({
223
226
owner: context.repo.owner,
224
227
repo: context.repo.repo,
225
- pull_number: parseInt(prNumber)
228
+ pull_number: parseInt(prNumber),
229
+ mediaType: {
230
+ format: 'diff'
231
+ }
226
232
});
227
233
228
- const commitSha = prData.data.head.sha;
234
+ const diffContent = prDiff.data; // This is the raw diff content
235
+ console.log(`📄 Diff content length: ${diffContent.length} chars`);
236
+
237
+ // Helper function to calculate diff position from raw diff content
238
+ const calculateDiffPositionFromRawDiff = function(diffContent, targetPath, targetLine) {
239
+ const lines = diffContent.split('\n');
240
+ let currentFile = null;
241
+ let position = 0;
242
+ let currentNewLine = 0;
243
+ let inTargetFile = false;
244
+ let inHunk = false;
245
+
246
+ for (const line of lines) {
247
+ if (line.startsWith('diff --git')) {
248
+ // Check if this is our target file
249
+ inTargetFile = line.includes(targetPath);
250
+ position = 0;
251
+ currentNewLine = 0;
252
+ inHunk = false;
253
+ continue;
254
+ }
255
+
256
+ if (!inTargetFile) continue;
257
+
258
+ // Skip file headers (---, +++)
259
+ if (line.startsWith('---') || line.startsWith('+++')) {
260
+ continue;
261
+ }
262
+
263
+ if (line.startsWith('@@')) {
264
+ // Parse hunk header: @@ -oldStart,oldCount +newStart,newCount @@
265
+ const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
266
+ if (match) {
267
+ currentNewLine = parseInt(match[1]) - 1; // Start one before the first line
268
+ inHunk = true;
269
+ position = 0; // Reset position counter for this hunk
270
+ }
271
+ continue;
272
+ }
273
+
274
+ if (!inHunk) continue;
275
+
276
+ // Increment position for every line in the hunk
277
+ position++;
278
+
279
+ if (line.startsWith('+')) {
280
+ // This is an added line
281
+ currentNewLine++;
282
+ if (currentNewLine === targetLine) {
283
+ return position;
284
+ }
285
+ } else if (line.startsWith(' ')) {
286
+ // This is a context line (unchanged)
287
+ currentNewLine++;
288
+ }
289
+ // Lines starting with '-' don't increment currentNewLine (they're deleted)
290
+ }
291
+
292
+ return -1; // Line not found in diff
293
+ };
229
294
230
295
// Process inline comments to ensure they have valid positions
231
296
const validInlineComments = [];
232
297
233
298
for (const comment of results.inlineComments) {
234
- // Find the corresponding file in the PR
235
- const prFile = prFiles.data.find(file => file.filename === comment.path );
299
+ // Calculate the position in the diff for this line using the raw diff
300
+ const position = calculateDiffPositionFromRawDiff(diffContent, comment.path, comment.line );
236
301
237
- if (prFile && prFile.patch) {
238
- // Calculate the position in the diff for this line
239
- const position = this.calculateDiffPosition(prFile.patch, comment.line);
240
-
241
- if (position > 0) {
242
- validInlineComments.push({
243
- path: comment.path,
244
- position: position, // Use position instead of line for diff-based comments
245
- body: comment.body
246
- });
247
- } else {
248
- console.log(`⚠️ Could not find valid diff position for ${comment.path}:${comment.line}`);
249
- }
302
+ if (position > 0) {
303
+ validInlineComments.push({
304
+ path: comment.path,
305
+ position: position,
306
+ body: comment.body
307
+ });
308
+ console.log(`✅ Valid position found for ${comment.path}:${comment.line} -> position ${position}`);
250
309
} else {
251
- console.log(`⚠️ File ${comment.path} not found in PR files or has no patch `);
310
+ console.log(`⚠️ Could not find valid diff position for ${comment.path}:${comment.line} `);
252
311
}
253
312
}
254
313
@@ -274,7 +333,7 @@ jobs:
274
333
if (results.hasMoreIssues) {
275
334
const truncationComment = `## 💙 More suggestions available!\n\n` +
276
335
`I found ${results.remainingIssues} additional item${results.remainingIssues > 1 ? 's' : ''} to review. ` +
277
- `Once you address the current suggestions, I'll be happy to review again and share the remaining feedback!\n\n` +
336
+ `Once you address the current suggestions, I'll review again and share the remaining feedback!\n\n` +
278
337
`This approach helps keep the review manageable and focused. Thanks for your patience! 😊`;
279
338
280
339
await github.rest.issues.createComment({
@@ -287,6 +346,7 @@ jobs:
287
346
288
347
} catch (error) {
289
348
console.error('Failed to post inline comments:', error.message);
349
+ console.error('Error details:', error);
290
350
291
351
// Fallback: post inline comments as a single comment
292
352
let fallbackComment = '## 📝 Detailed Suggestions\n\n*Unable to post inline comments, here are the specific suggestions:*\n\n';
@@ -306,51 +366,6 @@ jobs:
306
366
}
307
367
}
308
368
309
- // Helper function to calculate diff position according to GitHub API
310
- this.calculateDiffPosition = function(patch, targetLine) {
311
- const lines = patch.split('\n');
312
- let position = 0;
313
- let currentNewLine = 0;
314
- let inHunk = false;
315
-
316
- for (const line of lines) {
317
- // Skip file headers (---, +++)
318
- if (line.startsWith('---') || line.startsWith('+++')) {
319
- continue;
320
- }
321
-
322
- if (line.startsWith('@@')) {
323
- // Parse hunk header: @@ -oldStart,oldCount +newStart,newCount @@
324
- const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
325
- if (match) {
326
- currentNewLine = parseInt(match[1]) - 1; // Start one before the first line
327
- inHunk = true;
328
- position = 0; // Reset position counter for this hunk
329
- }
330
- continue;
331
- }
332
-
333
- if (!inHunk) continue;
334
-
335
- // Increment position for every line in the hunk
336
- position++;
337
-
338
- if (line.startsWith('+')) {
339
- // This is an added line
340
- currentNewLine++;
341
- if (currentNewLine === targetLine) {
342
- return position;
343
- }
344
- } else if (line.startsWith(' ')) {
345
- // This is a context line (unchanged)
346
- currentNewLine++;
347
- }
348
- // Lines starting with '-' don't increment currentNewLine (they're deleted)
349
- }
350
-
351
- return -1; // Line not found in diff
352
- };
353
-
354
369
// Set appropriate exit code
355
370
if (results.issues.errors.length > 0) {
356
371
core.setFailed(`Found ${results.issues.errors.length} critical errors that must be fixed before merging.`);
0 commit comments