From d6ff7dae2d523499cd1b496d98b0ed16a089852b Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Thu, 14 Aug 2025 10:05:10 -0700 Subject: [PATCH] fix: diff calculations --- .../workflows/continue-detailed-review.yaml | 21 +---- pr-review-action/scripts/annotate-diff.js | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 pr-review-action/scripts/annotate-diff.js diff --git a/.github/workflows/continue-detailed-review.yaml b/.github/workflows/continue-detailed-review.yaml index 275b1cc70..26e2530ae 100644 --- a/.github/workflows/continue-detailed-review.yaml +++ b/.github/workflows/continue-detailed-review.yaml @@ -59,22 +59,8 @@ jobs: # Get PR diff with context gh pr diff $PR_NUMBER --color=never > pr_diff.txt - # Annotate diff with position numbers for easy reference - awk ' - /^@@/ { - pos = 0 - print - next - } - /^diff --git/ || /^index / || /^--- / || /^\+\+\+ / { - print - next - } - { - pos++ - printf "[POS:%d] %s\n", pos, $0 - } - ' pr_diff.txt > pr_diff_annotated.txt + # Annotate diff with GitHub API positions + node ${{ github.workspace }}/pr-review-action/scripts/annotate-diff.js pr_diff.txt > pr_diff_annotated.txt # Create review prompt for inline comments cat > inline_review_prompt.txt << 'EOF' @@ -172,7 +158,7 @@ jobs: echo "Invalid JSON output from Continue CLI" echo "{\"review_summary\": \"Failed to generate review\", \"comments\": []}" > inline_review.json else - # Use the output directly since positions are already correct + # Positions are already correct from our annotation cp inline_review_raw.json inline_review.json echo "Review output:" cat inline_review.json @@ -302,7 +288,6 @@ jobs: inline_review_raw.json inline_review.json inline_review_prompt.txt - pr_files.json pr_diff.txt pr_diff_annotated.txt retention-days: 7 diff --git a/pr-review-action/scripts/annotate-diff.js b/pr-review-action/scripts/annotate-diff.js new file mode 100644 index 000000000..0c3d5f969 --- /dev/null +++ b/pr-review-action/scripts/annotate-diff.js @@ -0,0 +1,83 @@ +#!/usr/bin/env node + +/** + * Annotates a git diff with GitHub API positions + * + * Takes a raw diff and adds [POS:N] markers with the correct + * GitHub API positions (cumulative, with +1 between hunks) + */ + +const fs = require('fs'); + +function annotateDiff(diffContent) { + const lines = diffContent.split('\n'); + const output = []; + let githubPos = 0; + let inHunk = false; + + for (const line of lines) { + // Pass through git headers unchanged + if (line.startsWith('diff --git') || + line.startsWith('index ') || + line.startsWith('---') || + line.startsWith('+++') || + line.startsWith('rename ') || + line.startsWith('similarity ') || + line.startsWith('new file') || + line.startsWith('deleted file') || + line.startsWith('old mode') || + line.startsWith('new mode')) { + output.push(line); + continue; + } + + // Handle hunk headers + if (line.startsWith('@@')) { + if (inHunk) { + githubPos++; // GitHub's +1 between hunks + } + inHunk = true; + output.push(line); + continue; + } + + // Skip "no newline" marker + if (line.startsWith('\\ No newline')) { + output.push(line); + continue; + } + + // Annotate actual diff lines with GitHub positions + if (inHunk) { + githubPos++; + output.push(`[POS:${githubPos}] ${line}`); + } else { + output.push(line); + } + } + + return output.join('\n'); +} + +// Main +function main() { + // Read diff from stdin or file + let input = ''; + + if (process.argv.length > 2) { + // Read from file + input = fs.readFileSync(process.argv[2], 'utf8'); + } else { + // Read from stdin + input = fs.readFileSync(0, 'utf8'); + } + + const annotated = annotateDiff(input); + console.log(annotated); +} + +if (require.main === module) { + main(); +} + +module.exports = { annotateDiff }; \ No newline at end of file