Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 35 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,23 @@ 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, and a run stops before contacting a host other than
github.com that `gh` stores no login for and `GH_HOST` does not name (`gh has no login
for <host>`): a token in `GH_ENTERPRISE_TOKEN` never goes to a host only the URL names.
`--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. Every git command
a run starts, checkouts included, runs without `GH_TOKEN`, `GITHUB_TOKEN`,
`GH_ENTERPRISE_TOKEN` and `GITHUB_ENTERPRISE_TOKEN` and without a password prompt
(`GIT_ASKPASS`, `SSH_ASKPASS`, the terminal): only that helper gets the token.

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 +211,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 +289,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 +799,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
83 changes: 74 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 @@ -365,6 +370,17 @@ pf_fail() {
PREFLIGHT_FAILURES=$((PREFLIGHT_FAILURES + 1))
}

# 0 when the run may send a token to <host> (as gh_api_host gives it), which the PR's URL names: a host bound to the
# token apart from that URL. github.com; the host -u found the account's login on (use_gh_user stops the run without
# one); the host GH_HOST names, for a token in the environment; or a host gh stores a login for, which gh config get
# reads in gh's own config (hosts.yml), with no request and no token. Not gh auth status: it contacts every host it
# knows. Else gh would send an Enterprise token of the environment (GH_ENTERPRISE_TOKEN) to whatever host the URL names.
host_bound() { # <host>
[[ "$1" == github.com || (-n "$GH_USER" && "$1" == "$(url_api_host "$TARGET")") ]] && return 0
[[ -n "${GH_HOST:-}" && "$(gh_api_host "$GH_HOST")" == "$1" ]] && return 0
[[ -n "$(gh config get --host "$1" user 2>/dev/null)" ]]
}

run_preflight() {
if command -v git &>/dev/null; then
pf_ok "git installed"
Expand All @@ -379,10 +395,27 @@ 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). Only
# on a host bound to the token apart from the URL (host_bound): no gh call before this one sends a token to it. A
# bare number's host is the one gh repo view finds among the hosts gh knows (resolve_target).
local gh_host login
gh_host=$(url_api_host "$TARGET")
gh_host=${gh_host:-github.com}
if ! host_bound "$gh_host"; then
pf_fail "gh has no login for $gh_host" \
"run: gh auth login --hostname $gh_host (or set GH_HOST=$gh_host for a token in GH_ENTERPRISE_TOKEN)"
elif 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 +547,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 @@ -607,6 +645,8 @@ pr_lock || exit 1
SESSION_DIR=$(run_dir_new) || exit 1
# The clone's lock for the PR, once taken (clone_lock): runs with other state folders use the same clone.
CLONE_LOCK=""
# The folder of the file git's credential helper reads the run's token from, once made (GIT_AUTH), removed at the end.
TOKEN_DIR=""
# From here on, every exit names the run folder, by its final name once it has one (on stderr after a failure), and
# releases the locks. The exit status stays.
run_exit() {
Expand All @@ -617,6 +657,7 @@ run_exit() {
echo " Session: ${SESSION_DIR}" >&2 || true
fi
[[ -z "$CLONE_LOCK" ]] || lock_release "$CLONE_LOCK" || true
[[ -z "$TOKEN_DIR" ]] || rm -rf "$TOKEN_DIR" || true
pr_unlock || true
}
trap run_exit EXIT
Expand All @@ -626,9 +667,33 @@ 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 runs without the environment's tokens (rgit), so the token gh reads for the PR's host (-u's, or the caller's:
# GH_TOKEN or GITHUB_TOKEN for github.com and *.ghe.com, GH_ENTERPRISE_TOKEN or GITHUB_ENTERPRISE_TOKEN for another
# host) goes to that helper only, in a file only this user can read, removed when the run ends: never in git's
# environment, nor in a command line. Without such a token, gh uses its stored login, as before.
GIT_URL_HOST=$(git_url_host "$GH_HOST_NAME")
case "$GH_API_HOST" in
github.com | *.ghe.com) GIT_TOKEN=${GH_TOKEN:-${GITHUB_TOKEN:-}} ;;
*) GIT_TOKEN=${GH_ENTERPRISE_TOKEN:-${GITHUB_ENTERPRISE_TOKEN:-}} ;;
esac
GIT_HELPER='!gh auth git-credential'
if [[ -n "$GIT_TOKEN" ]]; then
if ! TOKEN_DIR=$(mktemp -d) || ! (umask 077 && printf '%s\n' "$GIT_TOKEN" >"$TOKEN_DIR/token"); then
echo "Error: could not write the token file for git's credential helper" >&2
exit 1
fi
# shellcheck disable=SC2016 # expanded by the shell git runs the helper with
GIT_HELPER=$(printf '!f() { t=$(cat %q) && GH_TOKEN=$t GH_ENTERPRISE_TOKEN=$t gh auth git-credential "$@"; }; f' \
"$TOKEN_DIR/token")
fi
GIT_TOKEN=""
# 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=$GIT_HELPER")
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
16 changes: 10 additions & 6 deletions lib/clones.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

# Git against a clone, with the clone's hooks off (core.fsmonitor too, a hook that core.hooksPath leaves on) and no
# automatic maintenance: a `gc --auto` (which a fetch starts) runs `git worktree prune`, which drops the registration
# of a worktree of the user's on a volume that is not mounted. The fetches also say --no-auto-maintenance. rgit_net
# adds GIT_AUTH, the per-command credential helper that the review run defines (unset: no extra options, as with
# file:// remotes in the tests).
# of a worktree of the user's on a volume that is not mounted. The fetches also say --no-auto-maintenance. Git runs
# without the GitHub tokens of the environment (-u's, or the caller's) and with no password prompt (GIT_ASKPASS,
# SSH_ASKPASS, the terminal): no credential helper, askpass adapter or Git LFS download that git starts can read the
# token or ask for one. rgit_net adds GIT_AUTH, the per-command credential helper that the review run defines, the
# only one that gets the token (unset: no extra options, as with file:// remotes in the tests): for every command that
# may reach the network, a checkout too (Git LFS fetches the files it checks out).
rgit() {
git -c core.hooksPath=/dev/null -c core.fsmonitor=false -c maintenance.auto=false -c gc.auto=0 "$@"
env -u GH_TOKEN -u GITHUB_TOKEN -u GH_ENTERPRISE_TOKEN -u GITHUB_ENTERPRISE_TOKEN GIT_ASKPASS= SSH_ASKPASS= \
GIT_TERMINAL_PROMPT=0 git -c core.hooksPath=/dev/null -c core.fsmonitor=false -c maintenance.auto=false \
-c gc.auto=0 "$@"
}
rgit_net() {
git -c core.hooksPath=/dev/null -c core.fsmonitor=false -c maintenance.auto=false -c gc.auto=0 \
${GIT_AUTH[@]+"${GIT_AUTH[@]}"} "$@"
rgit ${GIT_AUTH[@]+"${GIT_AUTH[@]}"} "$@"
}

# Unsets the variables that send git to another repository, index or object store than the one -C names: a caller
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
Loading