Merge pull request #7106 from continuedev/tomasz/con-3426
Create detailed review bot using 'cn'
This commit is contained in:
commit
bdab2cb85e
3 changed files with 317 additions and 7 deletions
2
.github/pull_request_template.md
vendored
2
.github/pull_request_template.md
vendored
|
|
@ -5,7 +5,7 @@
|
|||
## AI Code Review
|
||||
|
||||
- **Team members only**: AI review runs automatically when PR is opened or marked ready for review
|
||||
- Team members can also trigger a review by commenting `@continue-review`
|
||||
- Team members can also trigger a review by commenting `@continue-general-review` or `@continue-detailed-review`
|
||||
|
||||
## Checklist
|
||||
|
||||
|
|
|
|||
310
.github/workflows/continue-detailed-review.yaml
vendored
Normal file
310
.github/workflows/continue-detailed-review.yaml
vendored
Normal file
|
|
@ -0,0 +1,310 @@
|
|||
name: Continue Detailed Review
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, ready_for_review]
|
||||
issue_comment:
|
||||
types: [created]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: write
|
||||
issues: write
|
||||
|
||||
jobs:
|
||||
detailed-code-review:
|
||||
name: Continue Detailed Review
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
# Only run if:
|
||||
# - It's a PR event from a team member (with write/admin permissions)
|
||||
# - OR it's a comment with @continue-detailed-review on a PR from a team member
|
||||
if: >
|
||||
(github.event_name == 'pull_request' &&
|
||||
(github.event.pull_request.author_association == 'OWNER' ||
|
||||
github.event.pull_request.author_association == 'MEMBER' ||
|
||||
github.event.pull_request.author_association == 'COLLABORATOR')) ||
|
||||
(github.event_name == 'issue_comment' &&
|
||||
github.event.issue.pull_request &&
|
||||
contains(github.event.comment.body, '@continue-detailed-review') &&
|
||||
(github.event.comment.author_association == 'OWNER' ||
|
||||
github.event.comment.author_association == 'MEMBER' ||
|
||||
github.event.comment.author_association == 'COLLABORATOR'))
|
||||
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
fetch-depth: 0 # Fetch full history for better context
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20
|
||||
|
||||
- name: Install Continue CLI
|
||||
run: npm install -g @continuedev/cli@1.4.25
|
||||
|
||||
- name: Build Inline Review Prompt
|
||||
run: |
|
||||
# Get PR number based on event type
|
||||
if [ "${{ github.event_name }}" = "pull_request" ]; then
|
||||
PR_NUMBER="${{ github.event.number }}"
|
||||
else
|
||||
# For issue_comment event on a PR
|
||||
PR_NUMBER="${{ github.event.issue.number }}"
|
||||
fi
|
||||
|
||||
# Get PR files with line numbers
|
||||
gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/files > pr_files.json
|
||||
|
||||
# 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
|
||||
|
||||
# Create review prompt for inline comments
|
||||
cat > inline_review_prompt.txt << 'EOF'
|
||||
You are conducting a detailed inline code review for a pull request.
|
||||
|
||||
Your task is to analyze the code changes and generate specific, actionable review comments for individual lines or small sections of code.
|
||||
|
||||
IMPORTANT: The diff below has been annotated with [POS:N] markers showing the exact position for each line.
|
||||
|
||||
Example of how to use the position markers:
|
||||
```
|
||||
diff --git a/src/app.js b/src/app.js
|
||||
index abc123..def456 100644
|
||||
--- a/src/app.js
|
||||
+++ b/src/app.js
|
||||
@@ -10,4 +10,6 @@ function example() {
|
||||
[POS:1] console.log("context");
|
||||
[POS:2] - const old = "remove this";
|
||||
[POS:3] + const new = "add this";
|
||||
[POS:4] + const risky = eval(userInput); <-- To comment here, use position: 4
|
||||
[POS:5] return result;
|
||||
[POS:6] + console.log("done");
|
||||
@@ -20,3 +22,4 @@ function another() {
|
||||
[POS:1] let x = 1;
|
||||
[POS:2] + let y = 2; <-- To comment here, use position: 2
|
||||
```
|
||||
|
||||
To comment on a line, simply use the number from its [POS:N] marker as the position value.
|
||||
For example: {"path": "src/app.js", "position": 4, "body": "Avoid using eval with user input"}
|
||||
|
||||
Below is the annotated PR diff:
|
||||
|
||||
EOF
|
||||
|
||||
cat pr_diff_annotated.txt >> inline_review_prompt.txt
|
||||
|
||||
cat >> inline_review_prompt.txt << 'EOF'
|
||||
|
||||
Please analyze these changes and provide inline review comments in the following JSON format:
|
||||
|
||||
{
|
||||
"review_summary": "Overall assessment of the PR",
|
||||
"comments": [
|
||||
{
|
||||
"path": "path/to/file.js",
|
||||
"position": 4,
|
||||
"body": "Brief, specific comment about this line or section"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Focus on:
|
||||
1. **Critical Issues**: Security vulnerabilities, bugs that will break functionality
|
||||
2. **High Priority**: Performance problems, incorrect logic, missing error handling
|
||||
3. **Medium Priority**: Code style violations, potential edge cases, unclear code
|
||||
4. **Low Priority**: Suggestions for improvement, refactoring opportunities
|
||||
|
||||
Guidelines for comments:
|
||||
- Be specific and actionable
|
||||
- Use the [POS:N] number shown in the diff as the position value
|
||||
- Keep comments concise but informative
|
||||
- Suggest concrete improvements when pointing out issues
|
||||
- Only comment on actual problems or important improvements
|
||||
|
||||
Important:
|
||||
- Output ONLY valid JSON, no additional text
|
||||
- Use the exact number from [POS:N] markers as the position
|
||||
- Only comment on changed lines (lines with + or - prefix)
|
||||
- Comment on the most important issues first
|
||||
- Limit to maximum 20 most relevant comments
|
||||
EOF
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
|
||||
- name: Run Continue CLI Inline Review
|
||||
run: |
|
||||
echo "Running Continue CLI for inline review..."
|
||||
|
||||
# Log the prompt for debugging
|
||||
echo "===== START REVIEW PROMPT ====="
|
||||
head -n 50 inline_review_prompt.txt
|
||||
echo "... [diff content] ..."
|
||||
tail -n 20 inline_review_prompt.txt
|
||||
echo "===== END REVIEW PROMPT ====="
|
||||
|
||||
# Run the CLI and capture JSON output
|
||||
cat inline_review_prompt.txt | cn --readonly --format json --org continuedev --config continuedev/review-bot -p > inline_review_raw.json
|
||||
|
||||
# Log the output for debugging
|
||||
echo "Initial Continue CLI output:"
|
||||
cat inline_review_raw.json
|
||||
|
||||
# Validate JSON output
|
||||
if ! jq empty inline_review_raw.json 2>/dev/null; then
|
||||
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
|
||||
cp inline_review_raw.json inline_review.json
|
||||
echo "Review output:"
|
||||
cat inline_review.json
|
||||
fi
|
||||
env:
|
||||
CONTINUE_API_KEY: ${{ secrets.CONTINUE_API_KEY }}
|
||||
|
||||
- name: Post Inline Review Comments
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
|
||||
try {
|
||||
// Read the review output
|
||||
let reviewData;
|
||||
try {
|
||||
const reviewContent = fs.readFileSync('inline_review.json', 'utf8');
|
||||
reviewData = JSON.parse(reviewContent);
|
||||
} catch (parseError) {
|
||||
console.log('Failed to parse review JSON:', parseError);
|
||||
reviewData = {
|
||||
review_summary: 'AI review completed but failed to parse output',
|
||||
comments: []
|
||||
};
|
||||
}
|
||||
|
||||
// Get PR number
|
||||
let prNumber;
|
||||
if (context.eventName === 'pull_request') {
|
||||
prNumber = context.payload.pull_request.number;
|
||||
} else {
|
||||
prNumber = context.payload.issue.number;
|
||||
}
|
||||
|
||||
// Get the latest commit SHA
|
||||
const { data: pr } = await github.rest.pulls.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: prNumber
|
||||
});
|
||||
|
||||
const commitSha = pr.head.sha;
|
||||
|
||||
// Prepare review comments - filter out comments without required fields
|
||||
const allComments = reviewData.comments || [];
|
||||
const validComments = allComments.filter(comment =>
|
||||
comment.position &&
|
||||
comment.path &&
|
||||
comment.body &&
|
||||
typeof comment.position === 'number' &&
|
||||
comment.position > 0
|
||||
);
|
||||
|
||||
if (validComments.length < allComments.length) {
|
||||
console.log(`Filtered out ${allComments.length - validComments.length} comments due to missing/invalid position, path, or body`);
|
||||
const invalidComments = allComments.filter(c => !validComments.includes(c));
|
||||
console.log('Invalid comments:', JSON.stringify(invalidComments, null, 2));
|
||||
}
|
||||
|
||||
const comments = validComments.map(comment => ({
|
||||
path: comment.path,
|
||||
position: comment.position,
|
||||
body: comment.body
|
||||
}));
|
||||
|
||||
// Create the review
|
||||
if (comments.length > 0) {
|
||||
try {
|
||||
await github.rest.pulls.createReview({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: prNumber,
|
||||
commit_id: commitSha,
|
||||
body: reviewData.review_summary || 'AI Code Review',
|
||||
event: 'COMMENT',
|
||||
comments: comments
|
||||
});
|
||||
console.log(`Successfully created review with ${comments.length} inline comments`);
|
||||
} catch (reviewError) {
|
||||
console.log('Failed to create review:', reviewError.message);
|
||||
|
||||
// Fallback: post as a regular comment if review creation fails
|
||||
const fallbackBody = `## AI Code Review\n\n${reviewData.review_summary}\n\n` +
|
||||
`*Failed to post ${comments.length} inline comments. Please review the changes manually.*`;
|
||||
|
||||
await github.rest.issues.createComment({
|
||||
issue_number: prNumber,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: fallbackBody
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// No inline comments, just post the summary
|
||||
await github.rest.issues.createComment({
|
||||
issue_number: prNumber,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: `## AI Code Review\n\n${reviewData.review_summary}\n\n*No specific line comments generated.*`
|
||||
});
|
||||
}
|
||||
|
||||
// Add triggered by note if from comment
|
||||
if (context.eventName === 'issue_comment') {
|
||||
await github.rest.issues.createComment({
|
||||
issue_number: prNumber,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: `*Inline review triggered by @${context.payload.comment.user.login}*`
|
||||
});
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log('Failed to post review:', error.message);
|
||||
console.log('Error details:', error);
|
||||
}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
|
||||
- name: Upload Review Artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
with:
|
||||
name: inline-review-results
|
||||
path: |
|
||||
inline_review_raw.json
|
||||
inline_review.json
|
||||
inline_review_prompt.txt
|
||||
pr_files.json
|
||||
pr_diff.txt
|
||||
pr_diff_annotated.txt
|
||||
retention-days: 7
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
name: Continue CLI Code Review
|
||||
name: Continue General Review
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
|
@ -12,13 +12,13 @@ permissions:
|
|||
issues: write
|
||||
|
||||
jobs:
|
||||
code-review:
|
||||
name: AI Code Review
|
||||
general-code-review:
|
||||
name: Continue General Review
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
# Only run if:
|
||||
# - It's a PR event from a team member (with write/admin permissions)
|
||||
# - OR it's a comment with @continue-review on a PR from a team member
|
||||
# - OR it's a comment with @continue-general-review on a PR from a team member
|
||||
if: |
|
||||
(github.event_name == 'pull_request' &&
|
||||
(github.event.pull_request.author_association == 'OWNER' ||
|
||||
|
|
@ -26,14 +26,14 @@ jobs:
|
|||
github.event.pull_request.author_association == 'COLLABORATOR')) ||
|
||||
(github.event_name == 'issue_comment' &&
|
||||
github.event.issue.pull_request &&
|
||||
contains(github.event.comment.body, '@continue-review') &&
|
||||
contains(github.event.comment.body, '@continue-general-review') &&
|
||||
(github.event.comment.author_association == 'OWNER' ||
|
||||
github.event.comment.author_association == 'MEMBER' ||
|
||||
github.event.comment.author_association == 'COLLABORATOR'))
|
||||
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v5
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
fetch-depth: 0 # Fetch full history for better context
|
||||
|
||||
Loading…
Add table
Reference in a new issue