diff --git a/.github/workflows/claude-review.yml b/.github/workflows/claude-review.yml index 446e03e..0df36fc 100644 --- a/.github/workflows/claude-review.yml +++ b/.github/workflows/claude-review.yml @@ -102,8 +102,9 @@ jobs: echo "Found $DIFF_SIZE bytes of relevant changes" echo "diff_size=$DIFF_SIZE" >> $GITHUB_OUTPUT - # Create prompt text - PROMPT_TEXT="You are performing a code review. Please analyze this code diff and provide a thorough review that covers: + # Create prompt text in a file for better handling + cat > prompt.txt << '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 @@ -123,20 +124,22 @@ jobs: Here is the code diff to review: - \`\`\` - $(cat filtered_diff.txt) - \`\`\`" + ``` + EOL + + cat filtered_diff.txt >> prompt.txt + echo '```' >> prompt.txt - # Create API request with proper string handling - REQUEST=$(jq -n \ - --arg prompt "$PROMPT_TEXT" \ + # Create API request + REQUEST_JSON=$(jq -n \ + --arg content "$(cat prompt.txt)" \ '{ model: "claude-3-sonnet-20240229", max_tokens: 4096, temperature: 0.7, messages: [{ role: "user", - content: $prompt + content: $content }] }') @@ -146,7 +149,7 @@ jobs: -H "Content-Type: application/json" \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ - -d "$REQUEST") + -d "$REQUEST_JSON") # Process the response if echo "$RESPONSE" | jq -e '.content[0].text' > /dev/null; then @@ -157,10 +160,28 @@ jobs: } >> $GITHUB_OUTPUT else echo "Error in Claude API response: $RESPONSE" - echo "Request was: $REQUEST" + echo "Request was: $REQUEST_JSON" exit 1 fi + - name: Process review + if: success() && steps.analysis.outputs.diff_size != '0' + id: process-review + uses: actions/github-script@v7 + with: + script: | + const review = `${{ steps.analysis.outputs.review }}`; + + // Process review text to properly escape code blocks + const processedReview = review + .replace(/```/g, '\\`\\`\\`') + .replace(/`([^`]+)`/g, '\\`$1\\`') + .replace(/\${/g, '\\${'); + + return { + text: processedReview + }; + - name: Post review comment if: success() && steps.analysis.outputs.diff_size != '0' uses: actions/github-script@v7 @@ -168,8 +189,8 @@ jobs: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const { owner, repo } = context.repo; - const review = `${{ steps.analysis.outputs.review }}`; const prNumber = ${{ steps.pr-number.outputs.number }}; + const review = ${{ steps.process-review.outputs.result }}.text; await github.rest.issues.createComment({ owner,