From d1541cda773693cf66284bf38ff3eb26a8121de2 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:00:58 -0500 Subject: [PATCH] Update claude-review.yml --- .github/workflows/claude-review.yml | 171 ++-------------------------- 1 file changed, 8 insertions(+), 163 deletions(-) diff --git a/.github/workflows/claude-review.yml b/.github/workflows/claude-review.yml index be98726..ddaaa83 100644 --- a/.github/workflows/claude-review.yml +++ b/.github/workflows/claude-review.yml @@ -5,8 +5,11 @@ permissions: pull-requests: write on: + # Run on new/updated PRs pull_request: types: [opened, reopened, synchronize] + + # Allow manual triggers for existing PRs workflow_dispatch: inputs: pr_number: @@ -17,173 +20,15 @@ on: jobs: code-review: runs-on: ubuntu-latest + steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - - - name: Get PR number - 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 diff and analyze - 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" - - # Generate the full diff - git diff -U10 "$BASE_SHA" "$HEAD_SHA" > full_diff.txt - - # 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 "diff_size=0" >> $GITHUB_OUTPUT - exit 0 - fi - - # Get file size and content - DIFF_SIZE=$(wc -c < filtered_diff.txt) - echo "Found $DIFF_SIZE bytes of relevant changes" - echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT - - # Prepare content for API request - DIFF_CONTENT=$(cat filtered_diff.txt) - - # Create prompt in a way that preserves formatting - PROMPT=$(cat << 'EOL' - 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: - - ``` - EOL - ) - - # Create API request - REQUEST=$(jq -n \ - --arg prompt "$PROMPT" \ - --arg diff "$DIFF_CONTENT" \ - '{ - model: "claude-3-sonnet-20240229", - max_tokens: 4096, - temperature: 0.7, - messages: [{ - role: "user", - content: ($prompt + "\n" + $diff + "\n```") - }] - }') - - # Call the API - 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") - - # Process the response - if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then - { - echo 'review<> $GITHUB_OUTPUT - else - echo "Error in Claude API response: $RESPONSE" - echo "Request was: $REQUEST" - exit 1 - fi - - - name: Post review comment - if: success() && steps.analysis.outputs.diff_size != '0' - uses: actions/github-script@v7 + - name: Run Claude Review + uses: pacnpal/claude-code-review@v1.0.6 with: github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const { owner, repo } = context.repo; - const prNumber = ${{ steps.pr-number.outputs.number }}; - - // Prepare review text - const reviewText = `${{ steps.analysis.outputs.review }}` - .replace(/(?<=[\s\n])`([^`]+)`(?=[\s\n])/g, '\\`$1\\`') - .replace(/```/g, '\\`\\`\\`') - .replace(/\${/g, '\\${'); - - await github.rest.issues.createComment({ - owner, - repo, - issue_number: prNumber, - body: "# Claude Code Review\n\n" + reviewText - }); + anthropic-key: ${{ secrets.ANTHROPIC_API_KEY }} + pr-number: ${{ github.event.pull_request.number || inputs.pr_number }}