Update claude-review.yml

This commit is contained in:
pacnpal
2024-12-10 16:38:21 -05:00
committed by GitHub
parent 7eecd72130
commit 554bf7a00d

View File

@@ -18,8 +18,7 @@ jobs:
code-review: code-review:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout repository - uses: actions/checkout@v4
uses: actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
@@ -60,8 +59,10 @@ jobs:
throw error; throw error;
} }
- name: Generate diff - name: Generate diff and analyze
id: diff id: analysis
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: | run: |
# Configure git to fetch PR refs # Configure git to fetch PR refs
git config --local --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" git config --local --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
@@ -90,71 +91,82 @@ jobs:
echo "Found $DIFF_SIZE bytes of relevant changes" echo "Found $DIFF_SIZE bytes of relevant changes"
echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT
# Base64 encode the diff to preserve formatting # Prepare the diff content
ENCODED_DIFF=$(base64 -w 0 diff_filtered.txt) DIFF_CONTENT=$(cat diff_filtered.txt)
echo "diff_content=$ENCODED_DIFF" >> $GITHUB_OUTPUT
- name: Analyze with Claude
if: steps.diff.outputs.diff_size != '0'
id: analysis
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
# Decode the diff content
DIFF_CONTENT=$(echo "${{ steps.diff.outputs.diff_content }}" | base64 -d)
# Create request JSON using jq # Create the JSON payload
REQUEST=$(jq -n \ read -r -d '' PROMPT << EOM
--arg diff "$DIFF_CONTENT" \ You are performing a code review. Please analyze this code diff and provide a thorough review that covers:
1. Potential conflicts with existing codebase
2. Code correctness and potential bugs
3. Security vulnerabilities or risks
4. Performance implications
5. Maintainability and readability issues
6. Adherence to best practices and coding standards
7. Suggestions for improvements
For each issue found:
- Explain the problem clearly
- Rate the severity (Critical/High/Medium/Low)
- Provide specific recommendations for fixes
- Include code examples where helpful
If no issues are found in a particular area, explicitly state that.
Here is the code diff to review:
\`\`\`
${DIFF_CONTENT}
\`\`\`
EOM
# Create API request JSON
REQUEST_JSON=$(jq -n \
--arg prompt "$PROMPT" \
'{ '{
model: "claude-3-sonnet-20240229", "model": "claude-3-sonnet-20240229",
max_tokens: 4096, "max_tokens": 4096,
temperature: 0.7, "temperature": 0.7,
messages: [{ "messages": [
role: "user", {
content: "You are performing a code review. Please analyze this code diff and provide a thorough review that covers:\n\n1. Potential conflicts with existing codebase\n2. Code correctness and potential bugs\n3. Security vulnerabilities or risks\n4. Performance implications\n5. Maintainability and readability issues\n6. Adherence to best practices and coding standards\n7. Suggestions for improvements\n\nFor each issue found:\n- Explain the problem clearly\n- Rate the severity (Critical/High/Medium/Low)\n- Provide specific recommendations for fixes\n- Include code examples where helpful\n\nIf no issues are found in a particular area, explicitly state that.\n\nHere is the code diff to review:\n\n```\n" + $diff + "\n```" "role": "user",
}] "content": $prompt
}
]
}') }')
# Make the API request # Make API request and capture response in a file to preserve formatting
echo "Sending request to Claude API..." echo "Sending request to Claude API..."
RESPONSE=$(curl -s https://api.anthropic.com/v1/messages \ RESPONSE=$(curl -s https://api.anthropic.com/v1/messages \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \ -H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \ -H "anthropic-version: 2023-06-01" \
-d "$REQUEST") -d "$REQUEST_JSON")
if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then
# Save the review text echo 'review<<EOF' >> $GITHUB_OUTPUT
REVIEW=$(echo "$RESPONSE" | jq -r '.content[0].text') echo "$RESPONSE" | jq -r '.content[0].text' >> $GITHUB_OUTPUT
{ echo 'EOF' >> $GITHUB_OUTPUT
echo 'review<<EOF'
echo "$REVIEW"
echo 'EOF'
} >> $GITHUB_OUTPUT
else else
echo "Error in Claude API response: $RESPONSE" echo "Error in Claude API response: $RESPONSE"
echo "Request was: $REQUEST_JSON"
exit 1 exit 1
fi fi
- name: Post review comment - name: Post review comment
if: success() && steps.diff.outputs.diff_size != '0' if: success() && steps.analysis.outputs.diff_size != '0'
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
try { const { owner, repo } = context.repo;
const { owner, repo } = context.repo; const review = `${{ steps.analysis.outputs.review }}`;
const review = `${{ steps.analysis.outputs.review }}`; const prNumber = ${{ steps.pr-number.outputs.number }};
const prNumber = ${{ steps.pr-number.outputs.number }};
await github.rest.issues.createComment({
await github.rest.issues.createComment({ owner,
owner, repo,
repo, issue_number: prNumber,
issue_number: prNumber, body: "# Claude Code Review\n\n" + review
body: "# Claude Code Review\n\n" + review });
});
} catch (error) {
core.setFailed(`Failed to post review: ${error.message}`);
throw error;
}