|
| 1 | +# AI-Powered Code Review Workflow |
| 2 | +# |
| 3 | +# This workflow runs on every pull request to automatically extract code context |
| 4 | +# and prepare it for AI review. You can extend this to automatically post review |
| 5 | +# comments by integrating with an AI API (Claude, GPT, etc.) |
| 6 | +# |
| 7 | +# Setup: |
| 8 | +# 1. Copy this file to your repo's .github/workflows/ directory |
| 9 | +# 2. (Optional) Add ANTHROPIC_API_KEY or OPENAI_API_KEY to repository secrets |
| 10 | +# 3. Customize token budget and review guidelines as needed |
| 11 | + |
| 12 | +name: AI Code Review |
| 13 | + |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + types: [opened, synchronize, reopened] |
| 17 | + # Optionally limit to specific branches |
| 18 | + # branches: [main, develop] |
| 19 | + |
| 20 | + # Allow manual triggers for testing |
| 21 | + workflow_dispatch: |
| 22 | + inputs: |
| 23 | + pr_number: |
| 24 | + description: 'PR number to review' |
| 25 | + required: true |
| 26 | + |
| 27 | +jobs: |
| 28 | + extract-context: |
| 29 | + name: Extract PR Context |
| 30 | + runs-on: ubuntu-latest |
| 31 | + |
| 32 | + steps: |
| 33 | + # Checkout code with full history for proper diffs |
| 34 | + - name: Checkout code |
| 35 | + uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + fetch-depth: 0 # Need full history to compare branches |
| 38 | + ref: ${{ github.event.pull_request.head.sha }} |
| 39 | + |
| 40 | + # Setup Go environment |
| 41 | + - name: Setup Go |
| 42 | + uses: actions/setup-go@v4 |
| 43 | + with: |
| 44 | + go-version: '1.22' |
| 45 | + cache: true |
| 46 | + |
| 47 | + # Install promptext |
| 48 | + - name: Install promptext |
| 49 | + run: | |
| 50 | + go install github.com/1broseidon/promptext/cmd/promptext@latest |
| 51 | +
|
| 52 | + # Extract PR context using the example tool |
| 53 | + - name: Extract PR Context |
| 54 | + env: |
| 55 | + PR_NUMBER: ${{ github.event.pull_request.number || github.event.inputs.pr_number }} |
| 56 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 57 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 58 | + BASE_BRANCH: ${{ github.event.pull_request.base.ref || 'main' }} |
| 59 | + run: | |
| 60 | + # Ensure base branch exists |
| 61 | + git fetch origin $BASE_BRANCH:$BASE_BRANCH || true |
| 62 | +
|
| 63 | + # Run the context extractor |
| 64 | + cd examples/ci-code-review |
| 65 | + go run main.go |
| 66 | +
|
| 67 | + # Show summary |
| 68 | + echo "## 📊 Review Context Summary" >> $GITHUB_STEP_SUMMARY |
| 69 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 70 | + echo "- **Token Count**: $(jq -r '.token_count' pr-review-metadata.json)" >> $GITHUB_STEP_SUMMARY |
| 71 | + echo "- **Files Analyzed**: $(jq -r '.file_count' pr-review-metadata.json)" >> $GITHUB_STEP_SUMMARY |
| 72 | + echo "- **Files Changed**: $(jq -r '.pr_info.changed_files | length' pr-review-metadata.json)" >> $GITHUB_STEP_SUMMARY |
| 73 | +
|
| 74 | + # Upload artifacts for manual review or downstream jobs |
| 75 | + - name: Upload Review Context |
| 76 | + uses: actions/upload-artifact@v4 |
| 77 | + with: |
| 78 | + name: review-context-pr-${{ github.event.pull_request.number || github.event.inputs.pr_number }} |
| 79 | + path: | |
| 80 | + examples/ci-code-review/pr-review-context.ptx |
| 81 | + examples/ci-code-review/review-prompt.txt |
| 82 | + examples/ci-code-review/pr-review-metadata.json |
| 83 | + retention-days: 30 |
| 84 | + |
| 85 | + # Optional: Post summary comment to PR |
| 86 | + - name: Comment PR Summary |
| 87 | + if: github.event_name == 'pull_request' |
| 88 | + uses: actions/github-script@v7 |
| 89 | + with: |
| 90 | + script: | |
| 91 | + const fs = require('fs'); |
| 92 | + const metadata = JSON.parse(fs.readFileSync('examples/ci-code-review/pr-review-metadata.json', 'utf8')); |
| 93 | +
|
| 94 | + const comment = `## 🤖 AI Review Context Ready |
| 95 | +
|
| 96 | + Context has been extracted and is ready for AI review: |
| 97 | +
|
| 98 | + - **Files Analyzed**: ${metadata.file_count} |
| 99 | + - **Token Count**: ${metadata.token_count.toLocaleString()} |
| 100 | + - **Files Changed**: ${metadata.pr_info.changed_files.length} |
| 101 | +
|
| 102 | + ### Changed Files |
| 103 | + ${metadata.pr_info.changed_files.map(f => `- \`${f}\``).join('\n')} |
| 104 | +
|
| 105 | + ### Next Steps |
| 106 | + The review context is available as a workflow artifact. You can: |
| 107 | + 1. Download the artifact and send it to your AI assistant |
| 108 | + 2. Use the provided review prompt for structured feedback |
| 109 | + 3. Post the AI's suggestions as review comments |
| 110 | +
|
| 111 | + _Note: Automated AI review posting is not enabled. See workflow file for integration examples._ |
| 112 | + `; |
| 113 | +
|
| 114 | + github.rest.issues.createComment({ |
| 115 | + issue_number: context.issue.number, |
| 116 | + owner: context.repo.owner, |
| 117 | + repo: context.repo.repo, |
| 118 | + body: comment |
| 119 | + }); |
| 120 | +
|
| 121 | + # Optional: Automated AI Review (requires API key) |
| 122 | + # Uncomment and configure this job to automatically post AI reviews |
| 123 | + # |
| 124 | + # ai-review: |
| 125 | + # name: AI Review (Automated) |
| 126 | + # runs-on: ubuntu-latest |
| 127 | + # needs: extract-context |
| 128 | + # if: false # Set to true when ready to enable |
| 129 | + # |
| 130 | + # steps: |
| 131 | + # - uses: actions/checkout@v4 |
| 132 | + # |
| 133 | + # - name: Download Review Context |
| 134 | + # uses: actions/download-artifact@v4 |
| 135 | + # with: |
| 136 | + # name: review-context-pr-${{ github.event.pull_request.number }} |
| 137 | + # |
| 138 | + # - name: Send to Claude API |
| 139 | + # env: |
| 140 | + # ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 141 | + # run: | |
| 142 | + # # Combine prompt and context |
| 143 | + # PROMPT=$(cat review-prompt.txt) |
| 144 | + # CONTEXT=$(cat pr-review-context.ptx) |
| 145 | + # |
| 146 | + # # Call Claude API |
| 147 | + # REVIEW=$(curl -s https://api.anthropic.com/v1/messages \ |
| 148 | + # -H "x-api-key: $ANTHROPIC_API_KEY" \ |
| 149 | + # -H "anthropic-version: 2023-06-01" \ |
| 150 | + # -H "content-type: application/json" \ |
| 151 | + # -d "{ |
| 152 | + # \"model\": \"claude-3-5-sonnet-20241022\", |
| 153 | + # \"max_tokens\": 4096, |
| 154 | + # \"messages\": [{ |
| 155 | + # \"role\": \"user\", |
| 156 | + # \"content\": \"$PROMPT\n\n$CONTEXT\" |
| 157 | + # }] |
| 158 | + # }" | jq -r '.content[0].text') |
| 159 | + # |
| 160 | + # # Save review |
| 161 | + # echo "$REVIEW" > ai-review.txt |
| 162 | + # |
| 163 | + # - name: Post AI Review Comment |
| 164 | + # uses: actions/github-script@v7 |
| 165 | + # with: |
| 166 | + # script: | |
| 167 | + # const fs = require('fs'); |
| 168 | + # const review = fs.readFileSync('ai-review.txt', 'utf8'); |
| 169 | + # |
| 170 | + # github.rest.issues.createComment({ |
| 171 | + # issue_number: context.issue.number, |
| 172 | + # owner: context.repo.owner, |
| 173 | + # repo: context.repo.repo, |
| 174 | + # body: `## 🤖 AI Code Review\n\n${review}\n\n---\n*This review was generated automatically by Claude 3.5 Sonnet*` |
| 175 | + # }); |
0 commit comments