Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions .github/workflows/pr-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,23 @@ jobs:
echo "$auth_status"
scopes_line="$(printf '%s\n' "$auth_status" | grep 'Token scopes:' || true)"
normalized_scopes="$(printf '%s' "$scopes_line" | sed "s/[',]/ /g")"
required_scopes=(repo read:org)
for required_scope in "${required_scopes[@]}"; do
if ! grep -qE "(^|[[:space:]])${required_scope}([[:space:]]|$)" <<< "$normalized_scopes"; then
echo "::error::GH_TOKEN is missing required scope: ${required_scope}"
exit 1
fi
done

# Check for required scopes. Accept either:
# - Classic PAT: 'repo' scope (grants full repo access)
# - Fine-grained: 'contents' + 'pull_requests' (minimal permissions)
if grep -qE "(^|[[:space:]])repo([[:space:]]|$)" <<< "$normalized_scopes"; then
# Classic PAT with repo scope β€” sufficient
:
else
# Fine-grained token β€” verify minimal scopes
for required_scope in contents pull_requests; do
if ! grep -qE "(^|[[:space:]])${required_scope}([[:space:]]|$)" <<< "$normalized_scopes"; then
Comment on lines +226 to +227

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Stop parsing OAuth scopes for fine-grained PATs

For runs that actually use a fine-grained PAT, this validation still fails before any review starts because gh auth status exposes OAuth scopes from the Token scopes: line, not fine-grained repository permissions such as contents or pull_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 for gh to 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 checking Token scopes: for permission names.

Useful? React with πŸ‘Β / πŸ‘Ž.

echo "::error::GH_TOKEN is missing required scope: ${required_scope}"
echo "::error::Token must have either 'repo' scope (classic) or both 'contents' and 'pull_requests' (fine-grained)"
exit 1
fi
done
fi
Comment thread
don-petry marked this conversation as resolved.
Resolved

- name: Install review engine CLIs
run: |
Expand Down
15 changes: 15 additions & 0 deletions agents/pr-reviewer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ tools: ["read", "edit", "search", "execute", "web"]

You are the PR Review Agent for the petry-projects organization.

## Required GitHub token scopes

The agent requires a GitHub personal access token (PAT) with these minimum permissions:

**Repository permissions:**
- `contents:read` β€” read file contents and diffs

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since the agent is configured to automatically approve and enable auto-merge (or merge) pull requests (as described in the Decision framework), a fine-grained token with only contents:read permission will fail to perform these write operations. To allow the agent to enable auto-merge or merge PRs, the token requires contents:write permission instead of contents:read.

- `pull_requests:write` β€” post reviews and comments on PRs
- `actions:read` β€” check CI status and workflow results
- `metadata:read` β€” read repository metadata

**Organization permissions:**
- `members:read` β€” read organization members for code owner routing

These are fine-grained token scopes. The legacy `repo` scope (which grants full repository control) is NOT required.

## Your role

You review pull requests using a cascading tier system that minimizes token spend
Expand Down
60 changes: 35 additions & 25 deletions docs/pr-review-agent/pr-review-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,39 +129,49 @@
**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. Fine-grained tokens are
recommended for better security (principle of least privilege).

Comment on lines +132 to +139

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine Grained PAT is working bot the pr-review agent

Comment thread
don-petry marked this conversation as resolved.
Resolved
#### 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 (auto-rotate for security)
- **Resource owner:** select your organization (e.g. `petry-projects`)
Comment on lines +148 to +150

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's clear enough.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Document the single-owner limit for fine-grained PATs

With this fine-grained setup, selecting petry-projects as the resource owner only grants access to repositories owned by that organization, but the workflow still enumerates both the bot account namespace and the target org (scripts/list-prs.sh calls search_namespace "$BOT_USER" before search_namespace "$TARGET_ORG"). In deployments that rely on the bot's personal repos being scanned, following these instructions will make those PRs disappear from the candidate pool; the docs should either require a classic PAT for multi-owner coverage or explicitly state that fine-grained PATs only support the selected owner.

Useful? React with πŸ‘Β / πŸ‘Ž.

- **Repository access:** All repositories (or specific repos if preferred)
- **Repository permissions:**
- `contents:read` β€” read files and diffs

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To support enabling auto-merge and merging pull requests, the fine-grained PAT requires write access to repository contents. Please update this to contents:write instead of contents:read.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require Contents write for auto-merge tokens

When the recommended fine-grained token is created with only contents:read, the approval path can still post reviews but cannot complete the workflow’s merge step: I checked scripts/post-pr-review.sh, which runs gh pr merge "$PR_URL" --auto --squash after approval, and GitHub’s pull-request merge API documents fine-grained tokens as needing Contents repository permission write. Because that command is best-effort (|| true), runs using this setup will report success while approved PRs never get auto-merge enabled; the setup docs should request contents:write for fine-grained tokens if auto-merge is part of the agent contract.

Useful? React with πŸ‘Β / πŸ‘Ž.

- `pull_requests:write` β€” post reviews and comments
- `actions:read` β€” check CI status
- `metadata:read` β€” read repo metadata
- **Organization permissions:**
- `members:read` β€” read org members for code owner routing
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 β†’

Check failure on line 164 in docs/pr-review-agent/pr-review-agent.md

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces

docs/pr-review-agent/pr-review-agent.md:164:71 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.40.0/doc/md009.md
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
Expand Down Expand Up @@ -291,8 +301,8 @@
`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`
(org-level secret or repo secret on `.github`). Use the same PAT created above
in [step 2](#2-create-a-pat-for-the-bot-classic-or-fine-grained).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is an inconsistency between this documentation and the templates/mention-listener.yml workflow template. The template references secrets.GH_PAT_WORKFLOWS (e.g., on lines 36, 57, 71, 84), whereas this step instructs the user to add the secret as DON_PETRY_BOT_GH_PAT. Consider aligning the secret name used in the template or clarifying in the documentation that the secret name in the workflow should match.


3. Ensure `donpetry-bot` has at least **Read** collaborator access on
`petry-projects/.github-private`.
Expand Down
Loading