-
Notifications
You must be signed in to change notification settings - Fork 1
fix: support fine-grained tokens in pr-review scope validation #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b765813
56171b2
271f738
43c260c
6d5ed78
da8fe6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,13 +71,13 @@ and **Copilot**. | |
|
|
||
| 4. **Post-review actions** — after the review is posted, the action tier takes | ||
| additional actions depending on the decision: | ||
| - **If approved:** enables auto-merge (`gh pr merge --auto --squash`), | ||
| rebases the branch if behind base, and removes the `needs-human-review` | ||
| label. GitHub merges automatically once all required checks pass. | ||
| - **If approved:** removes the `needs-human-review` label if present, and | ||
| rebases the branch if it's behind base. The PR remains open for manual merge | ||
| by the author or maintainers. | ||
| - **If escalated + AI delegation enabled:** posts a follow-up comment | ||
| with specific fix instructions. An AI agent watches for these comments, | ||
| pushes fixes → next cron tick detects new SHA → cascade re-reviews → | ||
| approve + auto-merge when clean. This creates an autonomous fix loop. | ||
| approve when clean. This creates an autonomous fix loop. | ||
| - **If escalated + no delegation (or max cycles reached):** labels | ||
| `needs-human-review` and re-requests don-petry as reviewer. | ||
| - **Cycle guard:** before running the cascade, `scripts/review-one-pr.sh` | ||
|
|
@@ -129,39 +129,50 @@ authored by `don-petry`; the bot approves them. | |
| **Invite member** → enter `donpetry-bot` → Role: **Member**. | ||
| 6. Accept the invite from the bot account. | ||
|
|
||
| ### 2. Create a **classic** PAT for the bot | ||
|
|
||
| > [!IMPORTANT] | ||
| > **Fine-grained PATs do not work for this workflow.** Use a classic PAT only. | ||
| > | ||
| > Fine-grained tokens are blocked by org policy gates: even after the org owner | ||
| > approves the token request and the bot has Write collaborator access, the | ||
| > GraphQL `addPullRequestReview` mutation fails with: | ||
| > | ||
| > ``` | ||
| > failed to create review: GraphQL: Resource not accessible by personal access token (addPullRequestReview) | ||
| > ``` | ||
| > | ||
| > If you see that error in a workflow run, the secret holds a fine-grained | ||
| > token. Replace it with a classic PAT generated as below. The same gate also | ||
| > blocks the rulesets bypass that branch protections rely on. | ||
| ### 2. Create a PAT for the bot (classic or fine-grained) | ||
|
|
||
| You can use either a **classic** or **fine-grained** PAT. Classic PATs are the | ||
| safe default — see [bot-setup.md](bot-setup.md) for a known failure with | ||
| fine-grained tokens in some org configurations (`addPullRequestReview` blocked | ||
| at the org policy layer). Fine-grained tokens work when your org permits them | ||
| and follow the principle of least privilege. | ||
|
|
||
|
Comment on lines
+132
to
+139
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine Grained PAT is working bot the pr-review agent |
||
| #### Option A: Fine-grained PAT (recommended) | ||
|
|
||
| 1. Sign in as the bot account (e.g. `donpetry-bot`) — sign out of `don-petry` | ||
| first, or use a private window. The PAT must be created **from the bot's | ||
| account**, not yours. | ||
| 2. Go to **Settings → Developer settings → Personal access tokens → | ||
| Tokens (classic)** → **Generate new token (classic)**. | ||
| Fine-grained tokens** → **Generate new token**. | ||
| 3. Settings: | ||
| - **Note:** `pr-review-agent` | ||
| - **Expiration:** 1 year (set a calendar reminder to rotate) | ||
| - **Scopes:** ✅ `repo`, ✅ `workflow`, ✅ `read:org` | ||
| - **Token name:** `pr-review-agent` | ||
| - **Expiration:** 90 days (set a calendar reminder to rotate — fine-grained PATs do not auto-rotate) | ||
| - **Resource owner:** select your organization (e.g. `petry-projects`) | ||
|
Comment on lines
+148
to
+150
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's clear enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With this fine-grained setup, selecting Useful? React with 👍 / 👎. |
||
| - **Repository access:** All repositories (or specific repos if preferred) | ||
| - **Repository permissions:** | ||
| - `contents:read` — read files and diffs for analysis | ||
| - `pull_requests:write` — post reviews and comments | ||
| - **Organization permissions:** | ||
| - `members:read` — (optional) read org members for code owner routing at escalation time | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a repo's CODEOWNERS contains team entries (for example Useful? React with 👍 / 👎. |
||
| 4. Generate and copy the token immediately. | ||
| 5. Sign back in as `don-petry` and store the token in the agent repo's secret | ||
| (the secret name is `DON_PETRY_BOT_GH_PAT` in `petry-projects/.github-private`). | ||
|
|
||
| After saving, trigger a workflow run and confirm the install step's | ||
| `gh auth status` reports the bot's login (not yours) and lists the three | ||
| scopes above. | ||
| #### Option B: Classic PAT (legacy) | ||
|
|
||
| If you prefer classic PATs or need broader scopes: | ||
|
|
||
| 1. Sign in as the bot account, go to **Settings → Developer settings → | ||
| Personal access tokens → Tokens (classic)** → **Generate new token (classic)**. | ||
| 2. Settings: | ||
| - **Note:** `pr-review-agent` | ||
| - **Expiration:** 1 year | ||
| - **Scopes:** ✅ `repo`, ✅ `workflow`, ✅ `read:org` | ||
| 3. Generate and store as `DON_PETRY_BOT_GH_PAT` (same as above). | ||
|
|
||
| After saving either token type, trigger a workflow run and confirm the | ||
| `gh auth status` output reports the bot's login (not yours) and lists the | ||
| required scopes. | ||
|
|
||
| > **Branch protection / rulesets:** add `donpetry-bot` as an allowed | ||
| > approver on each protected repo. In the repo ruleset or branch protection | ||
|
|
@@ -290,9 +301,12 @@ PR comment "@donpetry-bot please review" | |
| 1. Copy [`templates/mention-listener.yml`](templates/mention-listener.yml) to | ||
| `petry-projects/.github` as `.github/workflows/pr-review-mention.yml`. | ||
|
|
||
| 2. Add the `DON_PETRY_BOT_GH_PAT` secret to `petry-projects/.github` | ||
| (org-level secret or repo secret on `.github`). Use a classic PAT from | ||
| `donpetry-bot` with scopes: ✅ `repo`, ✅ `workflow`, ✅ `read:org` | ||
| 2. Ensure the `GH_PAT_WORKFLOWS` org-level secret is available to | ||
| `petry-projects/.github`. This secret is already present in the org; no | ||
| additional secret setup is required. The workflow template references | ||
| `GH_PAT_WORKFLOWS` throughout — use the same PAT created above in | ||
|
Comment on lines
+304
to
+307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In the mention-trigger setup, this now says no additional secret is required and that the template references Useful? React with 👍 / 👎. |
||
| [step 2](#2-create-a-pat-for-the-bot-classic-or-fine-grained) if you need to | ||
| rotate or recreate it. | ||
|
|
||
| 3. Ensure `donpetry-bot` has at least **Read** collaborator access on | ||
| `petry-projects/.github-private`. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For runs that actually use a fine-grained PAT, this validation still fails before any review starts because
gh auth statusexposes OAuth scopes from theToken scopes:line, not fine-grained repository permissions such ascontentsorpull_requests; GitHub CLI’s own auth docs describe fine-grained PATs as having inherent resource scoping, and the CLI maintainers note that fine-grained permission information is not readily available forghto detect. As a result, the recommended token path hits this loop with no matching scopes and exits 1 even when the token has the requested permissions; validate fine-grained tokens with a cheap API probe instead of checkingToken scopes:for permission names.Useful? React with 👍 / 👎.