|
| 1 | +name: PR Risk |
| 2 | + |
| 3 | +# Shadow-mode risk check for small pull requests. Comments whether the PR would have been |
| 4 | +# auto-approved and why; it never approves, so a human review is still what merges a PR. |
| 5 | +# |
| 6 | +# A PR is skipped, with no comment, when it is a draft, comes from a fork, was not opened by an |
| 7 | +# org member (bots included), changes more than 50 lines, or touches a deny-listed path |
| 8 | +# (defaults in scripts/pr_risk.py plus the caller's `deny-paths`). Otherwise Claude classifies the diff and may deny it on its own judgment. |
| 9 | +# If a PR that already has a comment becomes ineligible on a later push, the comment is |
| 10 | +# rewritten so it never shows a stale "yes". |
| 11 | +# |
| 12 | +# Credentials are split by step. The model runs with no tools and no GitHub token, reading |
| 13 | +# only the PR data gathered with the caller's read-only token. The kernel-internal token is |
| 14 | +# minted after the model has finished, scoped to this repo and to pull-request writes. |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_call: |
| 18 | + inputs: |
| 19 | + deny-paths: |
| 20 | + description: Extra glob patterns, one per line, that make a PR ineligible |
| 21 | + type: string |
| 22 | + required: false |
| 23 | + default: '' |
| 24 | + security-workflows-ref: |
| 25 | + description: Ref of kernel/security-workflows to fetch the classifier script and prompt from |
| 26 | + type: string |
| 27 | + required: false |
| 28 | + default: main |
| 29 | + # Named rather than inherited: `secrets: inherit` would hand this cross-repo workflow |
| 30 | + # every secret the caller holds. |
| 31 | + secrets: |
| 32 | + ADMIN_APP_ID: |
| 33 | + required: true |
| 34 | + ADMIN_APP_PRIVATE_KEY: |
| 35 | + required: true |
| 36 | + ANTHROPIC_API_KEY: |
| 37 | + required: true |
| 38 | + |
| 39 | +jobs: |
| 40 | + classify: |
| 41 | + if: github.event.pull_request.draft == false |
| 42 | + runs-on: ubuntu-latest |
| 43 | + timeout-minutes: 10 |
| 44 | + permissions: |
| 45 | + contents: read |
| 46 | + pull-requests: read |
| 47 | + concurrency: |
| 48 | + group: pr-risk-${{ github.repository }}-${{ github.event.pull_request.number }} |
| 49 | + cancel-in-progress: true |
| 50 | + env: |
| 51 | + REPO: ${{ github.repository }} |
| 52 | + NUMBER: ${{ github.event.pull_request.number }} |
| 53 | + WORKDIR: /tmp/pr-risk |
| 54 | + steps: |
| 55 | + - name: Fetch classifier |
| 56 | + env: |
| 57 | + REF: ${{ inputs.security-workflows-ref }} |
| 58 | + run: | |
| 59 | + set -euo pipefail |
| 60 | + base="https://raw.githubusercontent.com/kernel/security-workflows/$REF" |
| 61 | + curl -fsSL "$base/scripts/pr_risk.py" -o /tmp/pr_risk.py |
| 62 | + curl -fsSL "$base/.github/workflows/pr-risk/classify-prompt.md" -o /tmp/pr-risk-prompt.md |
| 63 | +
|
| 64 | + - name: Gather |
| 65 | + id: gather |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ github.token }} |
| 68 | + DENY_PATHS: ${{ inputs.deny-paths }} |
| 69 | + run: python3 /tmp/pr_risk.py gather --repo "$REPO" --number "$NUMBER" --dir "$WORKDIR" |
| 70 | + |
| 71 | + - name: Install Claude Code CLI |
| 72 | + if: steps.gather.outputs.eligible == 'true' |
| 73 | + run: | |
| 74 | + curl -fsSL https://claude.ai/install.sh | bash |
| 75 | + echo "$HOME/.local/bin" >> "$GITHUB_PATH" |
| 76 | +
|
| 77 | + - name: Classify |
| 78 | + if: steps.gather.outputs.eligible == 'true' |
| 79 | + env: |
| 80 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 81 | + MODEL: ${{ vars.CLAUDE_CODE_PREFERRED_MODEL }} |
| 82 | + run: | |
| 83 | + set -euo pipefail |
| 84 | + # An empty directory, so no CLAUDE.md or project settings are picked up. |
| 85 | + mkdir -p "$WORKDIR/cwd" && cd "$WORKDIR/cwd" |
| 86 | + claude -p ${MODEL:+--model "$MODEL"} \ |
| 87 | + --tools "" --strict-mcp-config \ |
| 88 | + --system-prompt-file /tmp/pr-risk-prompt.md \ |
| 89 | + --json-schema "$(python3 /tmp/pr_risk.py schema)" \ |
| 90 | + --output-format json --max-budget-usd 1 \ |
| 91 | + < "$WORKDIR/input.md" > "$WORKDIR/result.json" |
| 92 | +
|
| 93 | + - name: Generate app token |
| 94 | + id: app-token |
| 95 | + if: ${{ !cancelled() && (steps.gather.outputs.eligible == 'true' || steps.gather.outputs.commented == 'true') }} |
| 96 | + # Pinned to a commit because this step is handed the app's private key. d72941d is v1.12.0. |
| 97 | + uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 |
| 98 | + with: |
| 99 | + app-id: ${{ secrets.ADMIN_APP_ID }} |
| 100 | + private-key: ${{ secrets.ADMIN_APP_PRIVATE_KEY }} |
| 101 | + owner: ${{ github.repository_owner }} |
| 102 | + repositories: ${{ github.event.repository.name }} |
| 103 | + permission-pull-requests: write |
| 104 | + |
| 105 | + - name: Comment |
| 106 | + if: ${{ !cancelled() && steps.app-token.outcome == 'success' }} |
| 107 | + env: |
| 108 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 109 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 110 | + run: python3 /tmp/pr_risk.py comment --repo "$REPO" --number "$NUMBER" --dir "$WORKDIR" |
0 commit comments