Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ install it.
| Option | Default | Purpose |
| --- | --- | --- |
| `--model <id>` | `github-copilot/gpt-5.6-sol` | Any pi model ID |
| `-u`, `--user <login>` | active `gh` account | Use another logged-in `gh` account for this run only; global `gh auth` is not changed |
| `-u`, `--user <login>` | active `gh` account | Use another logged-in `gh` account for this run only, a login of the PR URL's host (see GitHub Enterprise below); global `gh auth` is not changed |
| `--no-post` | posting on (with confirm) | Print the review only |
| `--instructions <text>` | none | Extra guidance for both passes, for example `"Focus on db/; skip style nits"`. For a file: `--instructions "$(cat notes.md)"`. The tool does not post them, and the reviewer is told not to quote them, but the review can still reflect them: use `--no-post` to preview a review shaped by sensitive notes |
| `--skip-approach` | off | Skip the maintainer's approach pass and run the code review only, for example when the approach is already agreed |
Expand All @@ -90,11 +90,17 @@ review), `GITHUB_TOKEN` (private repo clones; `gh` auth usually
suffices).

GitHub Enterprise: pass the full PR URL (e.g. `https://git.example.com/o/r/pull/1`)
after `gh auth login --hostname git.example.com`. `--user` selects a github.com
account, looked up on github.com whatever host `GH_HOST` names; Enterprise hosts use
their own stored `gh` login. Two exceptions: `status`'s `-u` is a login on the host it
reads, and `show` and `rounds` given a PR URL look the login up on the PR's host and
use its token for every call there (a bare PR number: a github.com login, as above).
after `gh auth login --hostname git.example.com`. Without `--user`, each host uses
its own stored `gh` login. `--user` (or `GH_PR_REVIEW_USER`) selects a login of the
PR URL's host, whatever host `GH_HOST` names: a review run (`--check` included),
`rounds` and `show` look it up on that host (`gh auth token --hostname <host> --user
<login>`) and use its token for every call there, the post included, and a run with
no such login there stops before anything else, naming the host. A bare PR number
selects a github.com login. `gc` looks the login up on each PR's host too (see
[Cleaning up](#cleaning-up)). `status`'s `-u` is a login on the host it reads (below).
Git's clones and fetches for a review run ask `gh auth git-credential` for a login on
the PR's host only (its HTTPS URL), with no other credential helper: a mirror that an
`insteadOf` rule of yours sends them to gets no token from the run.

Note: `gh pr review` (spaces) is GitHub's built-in approve/comment
command; `gh pr-review` (hyphen) is this extension. They coexist.
Expand Down Expand Up @@ -199,7 +205,8 @@ folders are readable by you only (`0700`), whatever the umask. It holds:
- `loop-check.md`: the last loop diagnosis that stopped a run (or `--check`)
- `lock/`: held while a review of the PR runs (see [Locks](#locks))
- `pr.json`: facts about the PR kept between runs: its clone, its review worktree and the commit the run made it at
(`worktree_sha`), its head branch, and the `gh` account the run selected (`--user` or `GH_PR_REVIEW_USER`), if any
(`worktree_sha`), its head branch, and the `gh` account the run selected (`--user` or `GH_PR_REVIEW_USER`), if any:
the login as given, a login of the PR's host

A run folder holds:

Expand Down Expand Up @@ -276,8 +283,16 @@ gh pr-review gc [--dry-run] [-u <login>]

For each PR in the state folder with a `pr.json` (`<host>/<owner>/<repo>/<N>`, a repo whose name starts with a dot, such
as `.github`, included), `gc` asks GitHub the PR's state, with the `gh` account that `-u` names, else the one in
`pr.json`, else `GH_PR_REVIEW_USER`, else the active one, selected as a review run selects it (a github.com account). An
open PR is kept. For a merged or closed one, it takes the PR's lock, then the clone's lock for the PR, and:
`pr.json`, else `GH_PR_REVIEW_USER`, else the active one, selected as a review run selects it: a login of the PR's host
(the `<host>` of its state folder), its token used for that host. A `pr.json` for a PR on another host than github.com
may hold a github.com login: one written before this rule, or one of a run given a bare PR number in a clone of a GitHub
Enterprise repo with `-u` or `GH_PR_REVIEW_USER` (a bare number selects a github.com login, saved as given). When `gh`
has no such login on the PR's host, the PR is skipped (`skipped: could not read its state: no gh login for '<login>' on
<host>`, after what `gh` said when it is something else, such as a config it can't read), as any PR whose state can't be
read: the other PRs are still done, and the exit status is 1. Log that login in on that host, or pass `-u` a login it
has (`-u` names the login for every PR). To review an Enterprise PR with `-u`, pass its full URL: the login saved is
then the Enterprise host's. An open PR is kept. For a merged or closed one, it takes the PR's lock, then the clone's
lock for the PR, and:

- removes the review worktree, only when it is clean: `git status`, with untracked and ignored files and submodule
changes, prints nothing (with the fsmonitor and the untracked cache off, and each file's stat data checked in full,
Expand Down Expand Up @@ -778,7 +793,11 @@ scripts. `gh pr-review rounds <PR>` shows the same decision under the history.

- **Empty review** — pi may have failed or timed out; retry with
`--model github-copilot/gpt-5.5` (or any other model from `pi --list-models`).
- **`gh not authenticated`** — `gh auth login`.
- **`gh not authenticated`** — `gh auth login`. A run checks `gh`'s login on the PR URL's host by reading the account
there (`gh api --hostname <host> user`) with the token the run uses: with `--user`, that login's, else the one `gh`
reads for the host (an environment token's, or the stored login's), so another login's expired token on that host
does not fail it. **`gh not authenticated on <host>`** — `gh auth login --hostname <host>`. The check needs the
network, as `gh auth status` does.
- **Private repo clone fails** — export `GITHUB_TOKEN` with repo read access.
- **`Note: skipping <clone>: …`** — the lookup can't use that clone: a shallow clone (`git fetch --unshallow`), a
partial or sparse one, one inside another clone's working tree, or one below a folder with a `.git`. It goes on to
Expand Down
45 changes: 36 additions & 9 deletions gh-pr-review
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# Options:
# --model <id> Pi model ID (default: github-copilot/gpt-5.6-sol)
# -u, --user <login> gh account to use for this run only (global gh auth is untouched)
# -u, --user <login> gh account to use for this run only, a login of the PR's host (global gh auth is untouched)
# --no-post Print review but don't post to GitHub
# --skip-approach Skip the maintainer's approach pass; run the code review only
# --instructions <text> Extra guidance for both passes (a file: "$(cat notes.md)")
Expand Down Expand Up @@ -223,7 +223,10 @@ exec 3>&1
[[ "$CHECK_JSON" == "true" ]] && exec 1>&2

# ── per-run gh account (env token only; never runs `gh auth switch`) ─────────
use_gh_user "$GH_USER"
# The login of the PR's host, as show and rounds take it: a github.com one for a bare number. Every gh call of the run
# (the PR reads, the history, the account's login, the post) and git's credential helper then use its token for that
# host; pr.json saves the login as given, which gc looks up on the PR's host too.
use_gh_user "$GH_USER" "$(url_api_host "$TARGET")"

# ── pr context ─────────────────────────────────────────────────────────────────────
# The author's description and the discussion, as files both passes read after forming their own view.
Expand Down Expand Up @@ -353,6 +356,8 @@ assemble_review() { # <loop/head lines> <approach> <code-review>
# ── preflight ────────────────────────────────────────────────────────────────
PI_AUTH_FILE="${HOME}/.pi/agent/auth.json"
PREFLIGHT_FAILURES=0
# The login of the account the run uses, and its host, as the preflight read them: none yet (not the environment's).
PREFLIGHT_LOGIN="" PREFLIGHT_HOST=""

pf_ok() {
[[ "$DOCTOR" == "true" ]] && echo "✓ $1"
Expand All @@ -379,10 +384,22 @@ run_preflight() {
pf_fail "jq not found" "macOS: brew install jq · Linux: apt/dnf install jq"
fi

if gh auth status -h github.com &>/dev/null; then
pf_ok "gh authenticated"
else
# gh's login on the PR URL's host (github.com for a bare PR number, or --doctor alone), read with the token the run
# uses there: with -u that account's (use_gh_user), else the one gh reads for the host, an environment variable's or
# the stored login's. Not github.com's for every PR: for a GHE.com PR, -u's token is in GH_TOKEN, which gh would
# check with github.com, and refuse. Not gh auth status either: it checks every login gh stores for the host
# (another account's expired token fails it) and, without GH_HOST naming the host, no token the environment gives
# for a host it stores no login for. The login is the round history's (PREFLIGHT_LOGIN, on PREFLIGHT_HOST).
local gh_host login
gh_host=$(url_api_host "$TARGET")
gh_host=${gh_host:-github.com}
if login=$(gh api --hostname "$gh_host" user --jq .login 2>/dev/null) && [[ -n "$login" ]]; then
PREFLIGHT_LOGIN=$login PREFLIGHT_HOST=$gh_host
pf_ok "gh authenticated on $gh_host as $login"
elif [[ "$gh_host" == github.com ]]; then
pf_fail "gh not authenticated" "run: gh auth login"
else
pf_fail "gh not authenticated on $gh_host" "run: gh auth login --hostname $gh_host"
fi

# --check reads GitHub only: it needs no model.
Expand Down Expand Up @@ -514,8 +531,13 @@ echo ""
HEAD_OID=$(echo "$PR_JSON" | jq -r '.headRefOid // empty')
# Only trusted reviews are rounds: by an owner, member or collaborator of the repo, or by this account (ROUNDS_ME, the
# same login for the final re-read). The others are named once, here. Without this account's login its own rounds
# would be missed: the run stops here, before any clone or model call.
ME=$(operator_login "$GH_API_HOST") || exit 1
# would be missed: the run stops here, before any clone or model call. The preflight read it on the PR's host, with the
# same token, unless that host was only known now (a bare PR number in a clone of another host than github.com).
if [[ -n "$PREFLIGHT_LOGIN" && "$PREFLIGHT_HOST" == "$GH_API_HOST" ]]; then
ME=$PREFLIGHT_LOGIN
else
ME=$(operator_login "$GH_API_HOST") || exit 1
fi
ROUNDS_ME="$ME"
IGNORED_FILE=$(mktemp)
RECORDS=$(fetch_round_records "$GH_API_HOST" "$GH_HOST_NAME" "$OWNER" "$REPO" "$PR_NUM" "$ROUNDS_ME" \
Expand Down Expand Up @@ -626,9 +648,14 @@ APPROACH_FILE="${SESSION_DIR}/approach.md"

# Per-command credential helper: the token never lands in a clone's .git/config. GIT_AUTH and DELTA_REMOTE are read
# by lib/clones.sh and lib/delta.sh. The URL's host is git_url_host's: without :443, for gh to find the host's login.
# Every other helper is cleared, and gh's is set for the PR's HTTPS host only (git compares the host without case, and
# the port): gh auth git-credential answers any host but github.com with GH_ENTERPRISE_TOKEN, the token -u gives the
# run, and git asks for the URL an insteadOf rule of the user's rewrites the clone's to, a mirror's.
GIT_URL_HOST=$(git_url_host "$GH_HOST_NAME")
# shellcheck disable=SC2034
GIT_AUTH=(-c credential.helper= -c "credential.helper=!gh auth git-credential")
CLONE_URL="https://$(git_url_host "$GH_HOST_NAME")/${OWNER}/${REPO}.git"
GIT_AUTH=(-c credential.helper= -c "credential.https://$GIT_URL_HOST.helper="
-c "credential.https://$GIT_URL_HOST.helper=!gh auth git-credential")
CLONE_URL="https://$GIT_URL_HOST/${OWNER}/${REPO}.git"
# The PR's key, host/owner/repo/N: its private refs are PR_REFS/* (lib/worktree.sh).
PR_KEY=$(pr_key "$PR_HOST" "$OWNER" "$REPO" "$PR_NUM") || exit 1
PR_REFS=$(pr_refs "$PR_KEY") || exit 1
Expand Down
5 changes: 3 additions & 2 deletions lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ url_api_host() { # <URL | number>
# Uses another logged-in gh account for this run only: an env token, never `gh auth switch`. Without <host>, or with
# github.com, a github.com account (README): gh looks the login up on github.com, not on the host GH_HOST may name (an
# Enterprise host, which uses its own stored login, or one gh refuses, GHE.example.com:443), and its token is GH_TOKEN.
# With another <host> (as gh_api_host gives it: the PR's, for show and rounds), the login gh has on that host, whose
# token is the one gh reads for it: GH_ENTERPRISE_TOKEN, and GH_TOKEN as well for a GHE.com host (*.ghe.com).
# With another <host> (as gh_api_host gives it: the PR's, for a review run, show, rounds and gc), the login gh has on
# that host, whose token is the one gh reads for it: GH_ENTERPRISE_TOKEN, and GH_TOKEN as well for a GHE.com host
# (*.ghe.com).
use_gh_user() { # <login> (empty: keep the active account) [host]
local host=${2:-github.com} token
[[ -z "$1" ]] && return 0
Expand Down
20 changes: 13 additions & 7 deletions lib/gc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ gc_usage() {
echo "Removes the review worktree and the private refs of each merged or closed PR in the state folder. The PR's"
echo "state folder (its review history) stays."
echo " --dry-run print what it would do, and change nothing"
echo " -u <login> the gh account that reads the PRs' states (default: the one in the PR's pr.json, else"
echo " GH_PR_REVIEW_USER, else the active one)"
echo " -u <login> the gh account that reads the PRs' states, a login of each PR's host (default: the one in the"
echo " PR's pr.json, else GH_PR_REVIEW_USER, else the active one)"
}

# The first error line of a message (else its first line), without its "Error: ": a lock taken over prints a note
Expand Down Expand Up @@ -588,9 +588,11 @@ gc_report() { # <label> <status> <state> [<reason>]
# (gc_report). A folder whose names are not a PR's is passed over, and so is one without a pr.json that is a file, gh
# not asked: --fresh runs write none, as they leave nothing but history. The PR's state comes from gh, with the account
# that -u names, else pr.json's `user` (the run's -u or GH_PR_REVIEW_USER), else GH_PR_REVIEW_USER, selected as the run
# does (use_gh_user). An open PR is kept. A merged or closed one is cleaned up under its lock (the one a review of the
# PR holds), and then the clone's lock for it (gc_clone): a PR either lock of which a live run holds is skipped. A
# state that can't be read is a skip, and an error (exit 1).
# does (use_gh_user): a login of the PR's host, <host>. A pr.json an earlier version wrote for a PR on another host than
# github.com may hold a github.com login: when gh has no such login on the PR's host, the PR is skipped, saying so. An
# open PR is kept. A merged or closed one is cleaned up under its lock (the one a review of the PR holds), and then the
# clone's lock for it (gc_clone): a PR either lock of which a live run holds is skipped. A state that can't be read (the
# login missing on the PR's host too) is a skip, and an error (exit 1).
gc_pr() { # <dir>
local dir=$1 rel host owner repo n key label user="" state pid err clone="" saved="" made=""
rel=${dir#"$GC_ROOT/"}
Expand All @@ -606,9 +608,13 @@ gc_pr() { # <dir>
fi
[[ -z "$GC_USER" ]] || user=$GC_USER
[[ -n "$user" ]] || user=${GH_PR_REVIEW_USER:-}
if ! state=$( (use_gh_user "$user" && gh pr view "$n" --repo "$host/$owner/$repo" --json state --jq .state) \
if ! state=$( (use_gh_user "$user" "$host" && gh pr view "$n" --repo "$host/$owner/$repo" --json state --jq .state) \
2>"$GC_ERR"); then
err=$(sed -e '/^[[:space:]]*$/d' -e 's/^Error: //' "$GC_ERR" | paste -sd ';' - | sed 's/;/; /g')
# No such login on the PR's host: use_gh_user's line names both, and gh's own line before it ("no oauth token
# found for <host> account <login>") says it again, so that one is dropped. gh's other lines stay, before it:
# they say why when it is something else (a config gh can't read).
err=$(sed -e '/^[[:space:]]*$/d' -e '/^no oauth token found for /d' -e 's/^Error: //' "$GC_ERR" |
paste -sd ';' - | sed 's/;/; /g')
GC_RC=1
gc_report "$label" skipped "" "could not read its state: ${err:-gh failed}"
return 0
Expand Down
Loading