diff --git a/.github/workflows/pr-agent.yml b/.github/workflows/pr-agent.yml new file mode 100644 index 0000000..6191c19 --- /dev/null +++ b/.github/workflows/pr-agent.yml @@ -0,0 +1,110 @@ +name: PR-Agent review + +# Reusable AI code review: PR-Agent (open source, MIT) on Claude, with no stored key. +# Each run exchanges GitHub's identity token for a 15-minute Anthropic token +# (workload identity federation). The token acts as the `pr-agents` service account +# and works only in the `pr-cicd` workspace, which has its own spend limit. +# +# The federation rule `pr-agent-org` accepts a token only when all of these hold: +# - the repository owner is tilorahdotco (owner id 350058761); +# - the running job is this workflow at `refs/heads/main` of this repository +# (`job_workflow_ref`), so a copy on another branch, commit or fork gets nothing; +# - the caller file is the repository's `.github/workflows/pr-agent.yml`; +# - the event is `pull_request` or `issue_comment`. +# Callers must therefore use `@main`, not a commit SHA: the one exception to the +# pin rule in docs/USAGE.md. Only organization owners can push to `main` here. +# Fork PR events get no identity token and are skipped below. +# +# Callers own triggers and concurrency, and must grant `id-token: write`. +# Models (PB, 2026-11-19): reviews, automatic or `/review`, use Claude Opus 5.5 at +# high effort; the `/ask` and `/improve` comment commands use Claude Haiku 5.5 at +# medium effort. Sonnet 5.5 runs only when the chosen model fails. This workflow owns +# the model: these settings win over a `model` in a repository's `.pr_agent.toml`, which +# only adds review instructions. Other PR-Agent commands and `--config.` arguments in a +# comment are not run. See docs/USAGE.md. +on: + workflow_call: + inputs: + runner: + description: Runner label. Public repositories must use a GitHub-hosted runner. + type: string + default: instabox-small + timeout-minutes: + type: number + default: 15 + +jobs: + review: + name: PR-Agent + # PR events from this repository (not forks or drafts), or a `/review`, `/ask` or + # `/improve` comment on a PR from people with write access to it. A comment with a + # `--config.` argument is skipped, so nobody changes the model from a comment. + if: >- + (github.event_name == 'pull_request' + && github.event.pull_request.head.repo.full_name == github.repository + && !github.event.pull_request.draft) + || (github.event_name == 'issue_comment' + && github.event.issue.pull_request + && (startsWith(github.event.comment.body, '/review') + || startsWith(github.event.comment.body, '/ask') + || startsWith(github.event.comment.body, '/improve')) + && !contains(github.event.comment.body, '--config.') + && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)) + runs-on: ${{ inputs.runner }} + timeout-minutes: ${{ inputs.timeout-minutes }} + permissions: + contents: read + pull-requests: write + issues: write + id-token: write + steps: + - name: Get a short-lived Anthropic token + env: + RULE_ID: fdrl_01HuULKgzQTuwtQDA7Cy8xQf # pr-agent-org + ORGANIZATION_ID: 3efd7970-54a4-4834-bde7-e84f38565f5a + SERVICE_ACCOUNT_ID: svac_01LYi4xr4JDN5ev9mGmcouMu # pr-agents + WORKSPACE_ID: wrkspc_01YNuFrBrkTtRpF7MFBtFqbv # pr-cicd + run: | + set -euo pipefail + JWT=$(curl -fsS --max-time 30 -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ + "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=https://api.anthropic.com" | jq -r .value) + echo "::add-mask::$JWT" + export JWT + if ! response=$(jq -n '{grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", + assertion: env.JWT, federation_rule_id: env.RULE_ID, organization_id: env.ORGANIZATION_ID, + service_account_id: env.SERVICE_ACCOUNT_ID, workspace_id: env.WORKSPACE_ID}' \ + | curl -sS --max-time 30 https://api.anthropic.com/v1/oauth/token \ + -H 'content-type: application/json' -d @-); then + echo "::error::Could not reach the Anthropic token endpoint." + exit 1 + fi + token=$(jq -r '.access_token // empty' <<<"$response" 2>/dev/null || true) + case "$token" in + sk-ant-oat01-*) ;; + *) + # The error body holds no secret: a type and a fixed message. + reason=$(jq -r '"\(.error.type // "unknown"): \(.error.message // "no message")"' <<<"$response" 2>/dev/null \ + || echo "the response was not JSON") + echo "::error::The Anthropic token exchange failed ($reason). The Claude Console shows the deny reason under Workload identity, History." + exit 1 + ;; + esac + echo "::add-mask::$token" + echo "ANTHROPIC_WIF_TOKEN=$token" >> "$GITHUB_ENV" + - name: Run PR-Agent + # PR-Agent v0.50.0, the `0.50.0-github_action` image by digest. The action + # repository only wraps a rolling image tag, so the digest is the real pin + # (PR-Agent SECURITY.md). To upgrade, take the digest of the new + # `-github_action` tag on Docker Hub. + uses: docker://pragent/pr-agent@sha256:548e894a88071180832b2f4233f734d0acd0db1a879e2fcbaa70bac8b8372ebe + env: + # LiteLLM sends an `sk-ant-oat` token as `Authorization: Bearer`. + ANTHROPIC.KEY: ${{ env.ANTHROPIC_WIF_TOKEN }} + GITHUB_TOKEN: ${{ github.token }} + CONFIG.MODEL: ${{ (startsWith(github.event.comment.body, '/ask') || startsWith(github.event.comment.body, '/improve')) && 'anthropic/claude-haiku-5-5' || 'anthropic/claude-opus-5-5' }} + CONFIG.FALLBACK_MODELS: '["anthropic/claude-sonnet-5-5"]' + CONFIG.ENABLE_CLAUDE_ADAPTIVE_THINKING: "true" + CONFIG.REASONING_EFFORT: ${{ (startsWith(github.event.comment.body, '/ask') || startsWith(github.event.comment.body, '/improve')) && 'medium' || 'high' }} + github_action_config.auto_review: "true" + github_action_config.auto_describe: "false" # the PR template is the description + github_action_config.auto_improve: "false" diff --git a/docs/USAGE.md b/docs/USAGE.md index 6548e10..3020d31 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -20,9 +20,10 @@ jobs: | `security.yml` | Parallel jobs `Secret scan` (gitleaks CLI) and `Dependency audit`. | `secret-scan`, `audit`, `audit-level`, `working-directory`, `runner` | none | | `deploy-cpanel.yml` | Job `Deploy` (cPanel Git pull), then `Verify live` (HTTP 200 and byte compare). | `trigger`, `cpanel-repo`, `branch`, `host` (required), `verify-url`, `live-base-url`, `live-files`, `runner` | `CPANEL_USER`, `CPANEL_PASSWORD` | | `deploy-cloudflare.yml` | Job `Build`, then `Deploy` (wrangler) from the built artifact. Manual release only. | `node-version`, `install`, `build`, `check`, `wrangler-version`, `command`, `runner` | `CLOUDFLARE_API_TOKEN`, `CLOUDFLARE_ACCOUNT_ID` | +| `pr-agent.yml` | Job `PR-Agent`: AI review of each PR with PR-Agent on Claude (Opus 5.5 reviews, Haiku 5.5 for `/ask` and `/improve`), and `/review`, `/improve`, `/ask` comments. No stored key: it exchanges GitHub's identity token for a 15-minute Anthropic token. | `runner`, `timeout-minutes` | none | Rules: -- Pin every `uses:` to a full commit SHA. Dependabot keeps the pins in this repository current. +- Pin every `uses:` to a full commit SHA. Dependabot keeps the pins in this repository current. The one exception is `pr-agent.yml`, which callers use at `@main` (see PR-Agent review). - Callers own triggers, concurrency and job names. - Public repositories run on GitHub-hosted runners only. - Keep secrets at the organization level, limited to the repositories that use them. @@ -181,3 +182,57 @@ jobs: root-dir: . args: --verbose --no-progress --exclude-mail '**/*.html' ``` + +## PR-Agent review + +Copy this caller to `.github/workflows/pr-agent.yml` in the repository. The file +name and the `@main` reference both matter. The Anthropic federation rule +`pr-agent-org` gives a token only to this workflow at `refs/heads/main` of this +repository, called from a `tilorahdotco` repository's +`.github/workflows/pr-agent.yml`, on a `pull_request` or `issue_comment` event. +This is the one exception to the SHA pin rule above: a copy of the workflow on +another branch, commit or fork gets no token, and a change merged to `main` here +reaches every repository at once. Only organization owners can push to `main`. + +```yaml +name: PR-Agent review +on: + pull_request: + types: [opened, reopened, ready_for_review] + issue_comment: + types: [created] +permissions: + contents: read + pull-requests: write + issues: write + id-token: write # GitHub's identity token, exchanged for a short-lived Anthropic token +concurrency: + # Commands from people with write access share one queue per PR. Any other comment + # gets its own group, so it never replaces a queued command. + group: pr-agent-${{ github.event.pull_request.number || github.event.issue.number }}-${{ github.event_name == 'issue_comment' && !(github.event.issue.pull_request && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association) && (startsWith(github.event.comment.body, '/review') || startsWith(github.event.comment.body, '/ask') || startsWith(github.event.comment.body, '/improve'))) && github.event.comment.id || 'review' }} + cancel-in-progress: false +jobs: + review: + name: PR-Agent review + # Only PR events and the three commands start a run. The shared workflow checks + # again: drafts, fork PRs, the author's write access and `--config.` arguments. + if: github.event_name == 'pull_request' || (github.event.issue.pull_request && (startsWith(github.event.comment.body, '/review') || startsWith(github.event.comment.body, '/ask') || startsWith(github.event.comment.body, '/improve'))) + uses: tilorahdotco/.github/.github/workflows/pr-agent.yml@main + with: + runner: instabox-small # public repositories: ubuntu-latest +``` + +- Tokens act as the `pr-agents` service account and work only in the `pr-cicd` + Anthropic workspace, which has its own spend limit. No repository needs an + Anthropic secret. +- Reviews (automatic, or a `/review` comment) use Claude Opus 5.5 at high effort. + The `/ask` and `/improve` comment commands use Claude Haiku 5.5 at medium effort. + Sonnet 5.5 runs only when the chosen model fails. The workflow's settings win + over a `model` in a repository's `.pr_agent.toml`; use that file for + `[pr_reviewer] extra_instructions`. No other PR-Agent command runs. +- A fork PR gets no automatic review. A maintainer's `/review` comment on it runs + from the default branch and reviews it; PR-Agent reads the diff and runs no PR code. +- The PR-Agent image is pinned by digest in the workflow. Use a runner with Docker: + instabox or GitHub-hosted, not the `cjbb2-ci` fallback runner. +- A failed exchange fails the job with Anthropic's error type. The deny reason is + in the Claude Console under Workload identity, History.