|
| 1 | +name: AI Code Review (Claude on Bedrock) |
| 2 | + |
| 3 | +# Automated PR review using Anthropic's Claude Code Action running on Amazon |
| 4 | +# Bedrock (inference stays in-account, CloudTrail-audited, no external API key). |
| 5 | +# |
| 6 | +# Fork safety: this uses pull_request_target (needed to access the Bedrock role |
| 7 | +# and post comments), so it reuses the same collaborator gate as |
| 8 | +# pr-checks-master.yml. Collaborator PRs -> `auto-approve` env (runs immediately). |
| 9 | +# Fork/external PRs -> `manual-approval` env (a maintainer must approve the run |
| 10 | +# before any secret or the Bedrock role is exposed). |
| 11 | + |
| 12 | +on: |
| 13 | + pull_request_target: |
| 14 | + types: [opened, synchronize, ready_for_review, reopened] |
| 15 | + branches: |
| 16 | + - "master" |
| 17 | + # Scope to the same product directories the other PR checks use, so pure |
| 18 | + # docs/example PRs don't trigger a model review. Remove this block to |
| 19 | + # review every PR. |
| 20 | + paths: |
| 21 | + - 'sagemaker-train/**' |
| 22 | + - 'sagemaker-serve/**' |
| 23 | + - 'sagemaker-mlops/**' |
| 24 | + - 'sagemaker-core/**' |
| 25 | + |
| 26 | +concurrency: |
| 27 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.head_ref }} |
| 28 | + cancel-in-progress: true |
| 29 | + |
| 30 | +permissions: |
| 31 | + id-token: write # OIDC federation to assume the Bedrock role |
| 32 | + pull-requests: write # post inline review comments and a summary |
| 33 | + contents: read |
| 34 | + |
| 35 | +jobs: |
| 36 | + # Identical gate to pr-checks-master.yml: collaborators auto-approve, |
| 37 | + # everyone else requires manual approval via the `manual-approval` environment. |
| 38 | + collab-check: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + outputs: |
| 41 | + approval-env: ${{ steps.collab-check.outputs.result }} |
| 42 | + steps: |
| 43 | + - name: Collaborator Check |
| 44 | + uses: actions/github-script@v7 |
| 45 | + id: collab-check |
| 46 | + with: |
| 47 | + github-token: ${{ secrets.COLLAB_CHECK_TOKEN }} |
| 48 | + result-encoding: string |
| 49 | + script: | |
| 50 | + try { |
| 51 | + const res = await github.rest.repos.checkCollaborator({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + username: "${{ github.event.pull_request.user.login }}", |
| 55 | + }); |
| 56 | + console.log("Verified ${{ github.event.pull_request.user.login }} is a repo collaborator. Auto approving AI review.") |
| 57 | + return res.status == "204" ? "auto-approve" : "manual-approval" |
| 58 | + } catch (error) { |
| 59 | + console.log("${{ github.event.pull_request.user.login }} is not a collaborator. Requiring manual approval to run AI review.") |
| 60 | + return "manual-approval" |
| 61 | + } |
| 62 | +
|
| 63 | + wait-for-approval: |
| 64 | + runs-on: ubuntu-latest |
| 65 | + needs: [collab-check] |
| 66 | + environment: ${{ needs.collab-check.outputs.approval-env }} |
| 67 | + steps: |
| 68 | + - run: echo "Approved — starting AI code review." |
| 69 | + |
| 70 | + review: |
| 71 | + runs-on: ubuntu-latest |
| 72 | + needs: [wait-for-approval] |
| 73 | + steps: |
| 74 | + # SECURITY: this job runs in the trusted pull_request_target context (it |
| 75 | + # holds the Bedrock role + secrets). We therefore check out the BASE repo |
| 76 | + # at the PR's base branch — never the fork's head code — so untrusted PR |
| 77 | + # code is never executed here ("pwn request" prevention). The PR contents |
| 78 | + # are pulled in as a read-only diff below, not as an executable tree. |
| 79 | + - uses: actions/checkout@v4 |
| 80 | + with: |
| 81 | + ref: ${{ github.event.pull_request.base.sha }} |
| 82 | + fetch-depth: 1 |
| 83 | + |
| 84 | + # Fetch the PR diff via the API (does not execute any fork code) and store |
| 85 | + # it as a static file. This is the ground truth Claude reviews. |
| 86 | + - name: Fetch PR diff |
| 87 | + id: diff |
| 88 | + env: |
| 89 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 91 | + REPO: ${{ github.repository }} |
| 92 | + run: | |
| 93 | + set -euo pipefail |
| 94 | + gh api "repos/$REPO/pulls/$PR_NUMBER" \ |
| 95 | + -H "Accept: application/vnd.github.v3.diff" > /tmp/pr.diff |
| 96 | + BYTES=$(wc -c < /tmp/pr.diff) |
| 97 | + echo "bytes=$BYTES" >> "$GITHUB_OUTPUT" |
| 98 | + echo "PR diff: $BYTES bytes" |
| 99 | +
|
| 100 | + - name: Configure AWS Credentials |
| 101 | + if: steps.diff.outputs.bytes != '0' |
| 102 | + uses: aws-actions/configure-aws-credentials@v4 |
| 103 | + with: |
| 104 | + role-to-assume: ${{ secrets.CODE_REVIEW_ROLE }} |
| 105 | + aws-region: us-west-2 |
| 106 | + |
| 107 | + - uses: anthropics/claude-code-action@v1 |
| 108 | + if: steps.diff.outputs.bytes != '0' |
| 109 | + with: |
| 110 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 111 | + use_bedrock: "true" |
| 112 | + track_progress: true |
| 113 | + # Don't append "Fix this" deep-links (which open Claude Code) to review |
| 114 | + # comments — external contributors can't use them and they add noise. |
| 115 | + include_fix_links: false |
| 116 | + # Bash is intentionally NOT allowed. The PR diff at /tmp/pr.diff is the |
| 117 | + # only ground truth; the model reads it and uses Read/Grep/Glob against |
| 118 | + # the trusted base checkout for context. It must not execute commands |
| 119 | + # (which could run untrusted PR content) nor re-run git. |
| 120 | + claude_args: | |
| 121 | + --model us.anthropic.claude-opus-4-8 |
| 122 | + --allowedTools "Read Grep Glob mcp__github_inline_comment__create_inline_comment" |
| 123 | + prompt: | |
| 124 | + REPO: ${{ github.repository }} |
| 125 | + PR NUMBER: ${{ github.event.pull_request.number }} |
| 126 | +
|
| 127 | + The complete PR diff is at `/tmp/pr.diff` — read it first with the |
| 128 | + Read tool. That file is the ground truth for what this PR changes; do |
| 129 | + not run git or any shell commands. For context (callers of changed |
| 130 | + functions, existing patterns, project conventions), use Read/Grep/Glob |
| 131 | + against the checked-out base repository. |
| 132 | +
|
| 133 | + Review this pull request for the SageMaker Python SDK. Focus on: |
| 134 | + - Correctness: bugs, incorrect API/argument usage, breaking changes |
| 135 | + to public interfaces, backward-incompatibility for SDK consumers |
| 136 | + - Python best practices and readability |
| 137 | + - Security implications (credential handling, input validation) |
| 138 | + - Performance considerations |
| 139 | + - Missing or inadequate tests for changed behavior |
| 140 | +
|
| 141 | + Post specific issues as inline comments via the |
| 142 | + mcp__github_inline_comment__create_inline_comment tool. Skip nits and |
| 143 | + style the linters already enforce. If the PR looks clean, say so |
| 144 | + briefly. |
0 commit comments