Update claude-review.yml

This commit is contained in:
pacnpal
2024-12-10 16:43:21 -05:00
committed by GitHub
parent f6b3929932
commit 2039026133

View File

@@ -73,60 +73,50 @@ jobs:
echo "Comparing $BASE_SHA..$HEAD_SHA" echo "Comparing $BASE_SHA..$HEAD_SHA"
# Get the diff # Generate the full diff
DIFF_CONTENT=$(git diff -U10 "$BASE_SHA" "$HEAD_SHA" | \ git diff -U10 "$BASE_SHA" "$HEAD_SHA" > full_diff.txt
grep -v -E '^diff --git .*(package-lock\.json|yarn\.lock|\.md|\.json)' | \
grep -E '\.(js|ts|py|cpp|h|java|cs)$')
if [ -z "$DIFF_CONTENT" ]; then # Use awk to properly capture full context of relevant files
awk '
BEGIN { found=0; buffer="" }
/^diff --git/ {
if (found) { print buffer }
found=0; buffer=""
if ($0 ~ /\.(js|ts|py|cpp|h|java|cs)$/ && $0 !~ /(package-lock\.json|yarn\.lock|\.md|\.json)/) {
found=1
}
}
{ if (found) buffer = buffer $0 "\n" }
END { if (found) print buffer }
' full_diff.txt > filtered_diff.txt
# Check if we have any relevant changes
if [ ! -s filtered_diff.txt ]; then
echo "No relevant changes found" echo "No relevant changes found"
echo "diff_size=0" >> $GITHUB_OUTPUT echo "diff_size=0" >> $GITHUB_OUTPUT
exit 0 exit 0
fi fi
DIFF_SIZE=${#DIFF_CONTENT} # Get file size and content
DIFF_SIZE=$(wc -c < filtered_diff.txt)
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
# Create prompt # Create the API request
PROMPT="You are performing a code review. Please analyze this code diff and provide a thorough review that covers: DIFF_CONTENT=$(cat filtered_diff.txt)
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
\`\`\`"
# Create API request
REQUEST=$(jq -n \ REQUEST=$(jq -n \
--arg prompt "$PROMPT" \ --arg diff "$DIFF_CONTENT" \
'{ '{
"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", "role": "user",
"content": $prompt "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```"
}] }]
}') }')
# Call API # Call the API
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" \
@@ -134,7 +124,7 @@ jobs:
-H "anthropic-version: 2023-06-01" \ -H "anthropic-version: 2023-06-01" \
-d "$REQUEST") -d "$REQUEST")
# Process response # Process the response
if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then
{ {
echo 'review<<EOF' echo 'review<<EOF'
@@ -143,6 +133,7 @@ jobs:
} >> $GITHUB_OUTPUT } >> $GITHUB_OUTPUT
else else
echo "Error in Claude API response: $RESPONSE" echo "Error in Claude API response: $RESPONSE"
echo "Request was: $REQUEST"
exit 1 exit 1
fi fi
@@ -153,8 +144,8 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
const { owner, repo } = context.repo; const { owner, repo } = context.repo;
const prNumber = ${{ steps.pr-number.outputs.number }};
const review = `${{ steps.analysis.outputs.review }}`; const review = `${{ steps.analysis.outputs.review }}`;
const prNumber = ${{ steps.pr-number.outputs.number }};
await github.rest.issues.createComment({ await github.rest.issues.createComment({
owner, owner,