# Review  -  PR picker and diff fetch (Steps 1a and 2)

<!-- toc -->
- [Step 1  -  Parse the input](#step-1---parse-the-input)
- [Step 1a  -  No-input PR picker](#step-1a---no-input-pr-picker)
- [Step 2  -  Fetch the diff](#step-2---fetch-the-diff)
- [Input matrix](#input-matrix)
<!-- /toc -->

> Loaded by `review/SKILL.md` Steps 1, 1a and 2. The SKILL keeps when each step runs, the autopilot fallback and the per-PR loop; this file carries the input parser, the list commands, the picker fields and the per-provider fetch commands.

## Step 1  -  Parse the input

```bash
# Pseudocode  -  orchestrator implements this:
input="$ARGUMENTS"
case "$input" in
  "")                              kind=pick   # interactive: go to Step 1a PR picker; autopilot/no-PRs falls back to branch (see 1a)
                                   ;;
  https://github.com/*/pull/*)     kind=pr; provider=github; org_repo=<extracted>; pr_number=<extracted> ;;
  https://bitbucket.*/projects/*/repos/*/pull-requests/*)
                                   kind=pr; provider=bitbucket-server
                                   bb_server_host=<extracted host>
                                   bb_project_key=<extracted KEY>
                                   bb_repo_slug=<extracted slug>
                                   pr_number=<extracted N>
                                   ;;
  *#[0-9]*)                        kind=pr; provider=github; org_repo="${input%%#*}"; pr_number="${input##*#}" ;;
  '#'[0-9]*)                       kind=pr; provider=github; org_repo=$(gh repo view --json nameWithOwner -q .nameWithOwner); pr_number="${input#'#'}" ;;
  [0-9]*)                          kind=pr; provider=github; org_repo=$(gh repo view --json nameWithOwner -q .nameWithOwner); pr_number="$input" ;;
  *)                               kind=branch; provider=local; ref="$input" ;;
esac
```

Autopilot with no ref goes straight to branch mode (`kind=branch; provider=local; ref=$(git rev-parse --abbrev-ref HEAD)`) so nothing prompts. When `provider=github`, Step 2 also persists `agent-state.review.headCommitSha`  -  GitHub inline-comment posts (`POST /repos/{o}/{r}/pulls/{n}/comments`) require `commit_id`. Bitbucket Server's anchor API does not.

## Step 1a  -  No-input PR picker

**List open PRs across both providers** (a provider contributes nothing if its token/CLI is absent - never hard-fail on one provider):

```bash
# GitHub: current repo's open PRs + the user's own/requested open PRs across repos.
gh pr list --state open --json number,title,url,headRefName,author,updatedAt 2>/dev/null   # current repo
gh search prs --state open --review-requested=@me --json number,title,url,repository,updatedAt 2>/dev/null
gh search prs --state open --author=@me           --json number,title,url,repository,updatedAt 2>/dev/null

# Bitbucket Server: for each configured bitbucket repo (prefs recentRepos / dev-context),
# resolve creds via credential-store (see Step 2 bitbucket block) then:
#   GET https://$host/rest/api/1.0/projects/$KEY/repos/$slug/pull-requests?state=OPEN&role=REVIEWER
```

Merge into one deduped list, each row labeled with its provider and PR URL; sort by `updatedAt` desc. If the merged list is EMPTY -> tell the user "no open PRs found", fall back to branch mode, and continue.

**Select** - follow `$HOME/.claude/multi-agent-refs/picker-contract.md` (breadcrumb narrator line before the prompt), then `AskUserQuestion` `multiSelect: true`:

```
question (outputLanguage): "Hangi PR('lar)ı review edeyim?"
header   (English):        "Review PRs"
options: one per open PR -> label "#<N> <short title>", description (outputLanguage): "<repo> · <provider> · <headRef>"
```

Each selected option maps back to a full `agent-state.review.input` descriptor (provider + orgRepo / bb coordinates + prNumber + raw URL), exactly as Step 1 would have parsed an explicit URL. Then **loop Steps 2-7 once per selected PR** (independent review + post each). If the user selects none, fall back to branch mode.

## Step 2  -  Fetch the diff

**branch mode:**

```bash
git diff origin/develop...{ref}
```

**pr mode  -  github:**

```bash
gh pr diff "$pr_number" --repo "$org_repo" > /tmp/multi-agent-review-${TASK_ID}-diff.patch
# Also capture the head commit SHA for GitHub inline-comment anchors
# (POST .../pulls/{n}/comments requires commit_id):
HEAD_SHA=$(gh api "/repos/$org_repo/pulls/$pr_number" --jq .head.sha)
printf '%s\n' "$HEAD_SHA" > /tmp/multi-agent-review-${TASK_ID}-head.sha
# Persist into agent-state  -  post-pr-review.sh reads it from there.
tmp_state=$(mktemp)
jq --arg sha "$HEAD_SHA" '.review.headCommitSha = $sha' "$AGENT_STATE" > "$tmp_state" \
  && mv "$tmp_state" "$AGENT_STATE"
```

**pr mode  -  bitbucket-server:**

```bash
# Resolve credentials via prefs.global.keychainMapping (never hardcode key names).
. "$HOME/.claude/lib/credential-store-resolver.sh" \
  || . "$HOME/.copilot/lib/credential-store-resolver.sh"
resolve_credential_store
USER_KEY=$(jq -r '.global.keychainMapping.bitbucket_user' "$HOME/.claude/multi-agent-preferences.json")
TOKEN_KEY=$(jq -r '.global.keychainMapping.bitbucket_token' "$HOME/.claude/multi-agent-preferences.json")
BB_USER=$("$CRED_STORE" get "$USER_KEY")
BB_TOKEN=$("$CRED_STORE" get "$TOKEN_KEY")

DIFF_FILE="/tmp/multi-agent-review-${TASK_ID}-diff.patch"
http=$(curl -sS -m 60 --config <(printf 'user = "%s:%s"\n' "$BB_USER" "$BB_TOKEN") -H "Accept: text/plain" \
  "https://$bb_server_host/rest/api/1.0/projects/$bb_project_key/repos/$bb_repo_slug/pull-requests/$pr_number/diff?contextLines=10" \
  -o "$DIFF_FILE" -w '%{http_code}')
if [ "$http" != "200" ]; then
  echo "review: Bitbucket diff fetch failed (HTTP $http)  -  error body in $DIFF_FILE" >&2
  rm -f "$DIFF_FILE"
  exit 1
fi
```

The `credential-store.sh` wrapper reads the macOS Keychain (`security`).

## Input matrix

| Input | Mode | PR action |
|---|---|---|
| no args (interactive) | PR picker (open GitHub + Bitbucket PRs, multi-select) | inline comments + approve/needs-work, per selected PR |
| no args (autopilot / no PRs) | branch (current) | none  -  chat only |
| `feature/foo` | branch (named) | none  -  chat only |
| `#N`, `repo#N`, GitHub PR URL | pr (github) | inline comments + approve/needs-work |
| Bitbucket Server PR URL | pr (bitbucket-server) | inline comments + approve/needs-work |
