Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 20 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,14 @@ 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).

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 +202,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 +280,13 @@ 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` written before this rule, 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 (`skipped: could not read its state: no gh login for '<login>' on <host>`), 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). 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 @@ -773,7 +782,8 @@ 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 (with `--user`, that
login's): **`gh not authenticated on <host>`** — `gh auth login --hostname <host>`.
- **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
19 changes: 15 additions & 4 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 @@ -379,10 +382,18 @@ 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
# gh's login on the PR URL's host (github.com for a bare PR number, or --doctor alone), with -u that account's token
# (use_gh_user). 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.
local gh_host
gh_host=$(url_api_host "$TARGET")
gh_host=${gh_host:-github.com}
if gh auth status -h "$gh_host" &>/dev/null; then
pf_ok "gh authenticated"
else
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
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
19 changes: 12 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,12 @@ 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 (gh's own, before it, says it again).
err=$(grep -m1 '^Error: no gh login for ' "$GC_ERR") ||
err=$(sed -e '/^[[:space:]]*$/d' -e 's/^Error: //' "$GC_ERR" | paste -sd ';' - | sed 's/;/; /g')
err=${err#Error: }
GC_RC=1
gc_report "$label" skipped "" "could not read its state: ${err:-gh failed}"
return 0
Expand Down
Loading