Update claude-review.yml

This commit is contained in:
pacnpal
2024-12-10 15:31:47 -05:00
committed by GitHub
parent dbad4cf822
commit 597894a80a

View File

@@ -22,133 +22,7 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Get PR number # Previous steps remain the same until the post review comment...
id: pr-number
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT
else
echo "number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
fi
- name: Get PR details
id: pr-details
uses: actions/github-script@v7
with:
script: |
try {
const { owner, repo } = context.repo;
const prNumber = ${{ steps.pr-number.outputs.number }};
console.log(`Getting PR #${prNumber} from ${owner}/${repo}`);
const pr = await github.rest.pulls.get({
owner: owner,
repo: repo,
pull_number: prNumber
});
return {
base_sha: pr.data.base.sha,
head_sha: pr.data.head.sha,
base_ref: pr.data.base.ref,
head_ref: pr.data.head.ref
};
} catch (error) {
core.setFailed(`Failed to get PR details: ${error.message}`);
throw error;
}
- name: Generate and analyze diff
id: analysis
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
# Configure git to fetch PR refs
git config --local --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git fetch origin
BASE_SHA="${{ fromJSON(steps.pr-details.outputs.result).base_sha }}"
HEAD_SHA="${{ fromJSON(steps.pr-details.outputs.result).head_sha }}"
echo "Comparing $BASE_SHA..$HEAD_SHA"
# Get the diff with context
git diff -U10 "$BASE_SHA" "$HEAD_SHA" > diff_full.txt
# Filter for relevant files
cat diff_full.txt | \
awk '/^diff --git/{p=0} /\.(js|ts|py|cpp|h|java|cs)$/{p=1} p' | \
grep -v -E '^diff --git .*(package-lock\.json|yarn\.lock|\.md|\.json)' > diff_filtered.txt
if [ ! -s diff_filtered.txt ]; then
echo "No relevant changes found"
echo "diff_size=0" >> $GITHUB_OUTPUT
exit 0
fi
DIFF_SIZE=$(wc -c < diff_filtered.txt)
echo "Found $DIFF_SIZE bytes of relevant changes"
echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT
# Prepare prompt and diff content
PROMPT="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:
\`\`\`
$(cat diff_filtered.txt)
\`\`\`"
# Create the API request using jq to properly escape the content
echo "Creating API request..."
REQUEST=$(jq -n \
--arg prompt "$PROMPT" \
'{
"model": "claude-3-sonnet-20240229",
"max_tokens": 4096,
"temperature": 0.7,
"messages": [{
"role": "user",
"content": $prompt
}]
}')
# Make the API request
echo "Sending request to Claude API..."
RESPONSE=$(curl -s https://api.anthropic.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d "$REQUEST")
if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then
# Save the review to a file to preserve formatting
echo "$RESPONSE" | jq -r '.content[0].text' > review.txt
echo 'review<<EOF' >> $GITHUB_OUTPUT
cat review.txt >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
else
echo "Error in Claude API response: $RESPONSE"
echo "Request was: $REQUEST"
exit 1
fi
- name: Post review comment - name: Post review comment
if: success() && steps.analysis.outputs.diff_size != '0' if: success() && steps.analysis.outputs.diff_size != '0'
@@ -157,12 +31,12 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
script: | script: |
const { owner, repo } = context.repo; const { owner, repo } = context.repo;
const review = `${{ steps.analysis.outputs.review }}`; const review = JSON.parse(JSON.stringify(`${{ 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.replace(/\\([\s\S])|(")/g, '\\$1$2')
}); });