Skip to content

Commit 114c77c

Browse files
authored
fix(test-classifier): --pr works on closed/merged PRs + repos other than origin (#74)
A user reported '--pr 106' failing with 'could not look up PR #106 … verify the PR number exists' even though 'gh pr view 106' worked. Two problems: 1. Misdiagnosis in the message. PR STATE is not the issue — gh pr view returns baseRefName for open/closed/merged alike (verified). The real cause is repo resolution: #64 pinned the lookup to origin's slug (correct for forks whose PR is on the fork), but that BREAKS when the PR lives on a different repo than origin — e.g. the PR is on the upstream/parent, or the user relies on 'gh repo set-default'. The origin-pinned 'gh pr view -R <origin>' then finds nothing and we wrongly told them the PR doesn't exist. 2. No fallback. Now: try origin first (keeps the fork fix), then fall back to gh's OWN default resolution (no -R); adopt whichever repo actually resolved so the later comment-post + metrics row target the right place. AI_REVIEW_REPO still overrides and short-circuits the fallback. Also rewrote the error to say plainly it's a repo/access issue (not PR state) and to point at AI_REVIEW_REPO=owner/name as the fix. Verified: gh pr view returns baseRefName for a MERGED PR; headRepositoryOwner/ headRepository jq resolves a valid owner/name; bash -n clean. The actual cross-repo fork+upstream case needs a user with that remote layout to confirm end-to-end.
1 parent be837c4 commit 114c77c

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

testing/classifier/.skills/test-classifier/scripts/test-classifier-dispatcher.sh

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,46 @@ ai_review::discover_pr_context() {
285285
done
286286

287287
# If --pr was given, look up that PR's base ref.
288+
#
289+
# Works for OPEN, CLOSED, and MERGED PRs — `gh pr view` returns baseRefName for
290+
# any state (PR state does not gate this). The one real failure mode is repo
291+
# mismatch: we pin to origin's slug (the #64 fork fix), but if the PR actually
292+
# lives on a DIFFERENT repo than origin (e.g. the PR is on the upstream/parent,
293+
# or the user relies on `gh repo set-default`), the origin-pinned lookup finds
294+
# nothing. So: try origin first, then fall back to gh's OWN default resolution
295+
# (no -R), and adopt whichever repo actually resolved so posting + the metrics
296+
# row target the right place. AI_REVIEW_REPO overrides everything.
288297
if [[ -n "${PR_NUMBER}" ]]; then
289298
require_gh_cli "PR number was specified via --pr"
290-
local repo_slug
299+
local repo_slug base resolved_slug=""
291300
repo_slug="$(ai_review::repo_slug)" || exit 1
292-
local base
301+
302+
# 1) origin-pinned lookup (the common/fork case).
293303
base="$(gh pr view "${PR_NUMBER}" -R "${repo_slug}" --json baseRefName --jq '.baseRefName' 2>/dev/null || true)"
304+
if [[ -n "${base}" ]]; then
305+
resolved_slug="${repo_slug}"
306+
elif [[ -z "${AI_REVIEW_REPO:-}" ]]; then
307+
# 2) Fall back to gh's own repo resolution (no -R) — handles a PR that lives
308+
# on a repo other than origin (upstream, or a gh default). Skipped when
309+
# AI_REVIEW_REPO was set explicitly (the caller pinned it on purpose).
310+
base="$(gh pr view "${PR_NUMBER}" --json baseRefName --jq '.baseRefName' 2>/dev/null || true)"
311+
if [[ -n "${base}" ]]; then
312+
resolved_slug="$(gh pr view "${PR_NUMBER}" --json headRepositoryOwner,headRepository \
313+
--jq '.headRepositoryOwner.login + "/" + .headRepository.name' 2>/dev/null || true)"
314+
[[ -z "${resolved_slug}" ]] && resolved_slug="$(gh repo view --json nameWithOwner --jq '.nameWithOwner' 2>/dev/null || true)"
315+
if [[ -n "${resolved_slug}" ]]; then
316+
echo "[test-classifier] PR #${PR_NUMBER} not found on origin (${repo_slug}); using ${resolved_slug} (gh's resolution)." >&2
317+
AI_REVIEW_REPO_SLUG="${resolved_slug}" # adopt it for posting + metrics
318+
fi
319+
fi
320+
fi
321+
294322
if [[ -z "${base}" ]]; then
295-
echo "ERROR: could not look up PR #${PR_NUMBER} via gh CLI." >&2
296-
echo " Verify the PR number exists and you have access to it." >&2
323+
echo "ERROR: could not look up PR #${PR_NUMBER}." >&2
324+
echo " It was not found on '${repo_slug}' (from your 'origin' remote)$([ -z "${AI_REVIEW_REPO:-}" ] && echo " or via gh's default repo")." >&2
325+
echo " PR state (open/closed/merged) does NOT matter — this is a repo/access issue:" >&2
326+
echo " • If the PR lives on a different repo, set it: AI_REVIEW_REPO=owner/name test-classifier --pr ${PR_NUMBER}" >&2
327+
echo " • Or check 'gh pr view ${PR_NUMBER}' works and your token has access to that repo." >&2
297328
exit 1
298329
fi
299330
REMAINING_FOR_LIB+=("--against" "origin/${base}")

0 commit comments

Comments
 (0)