#!/usr/bin/env bash
# nerf-gh-pr-reviews -- List reviews on a pull request, latest first. Each entry includes id (numeric), author, state, commit (short SHA), submitted_at, and body (the reviewer's summary text). Pass an entry's id to gh-pr-review-comments to fetch the inline comments for that specific review.
# Generated from gh manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-gh-pr-reviews [--limit|-L <limit>] <pr>

Options:
  --limit, -L <limit>
      Maximum number of reviews to return (default 10, latest first)
      Must match: ^[1-9][0-9]*$

Arguments:
  <pr> (required)
      PR number
      Must match: ^[0-9]+$

List reviews on a pull request, latest first. Each entry includes id (numeric), author, state, commit (short SHA), submitted_at, and body (the reviewer's summary text). Pass an entry's id to gh-pr-review-comments to fetch the inline comments for that specific review.
EOF
  exit 1
}

LIMIT=""
_LIMIT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --limit|-L) if [[ -n "${_LIMIT_SET}" ]]; then echo "error: --limit can only be specified once" >&2; exit 1; fi; LIMIT="$2"; _LIMIT_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_PR_SET=""
if [[ $# -gt 0 ]]; then
  PR="$1"
  _PR_SET=true
  shift
else
  PR=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-gh-pr-reviews: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

_NERF_PATTERN='^[1-9][0-9]*$'
if [[ -n "${_LIMIT_SET}" ]] && ! [[ "${LIMIT}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-gh-pr-reviews: option --limit does not match required pattern" >&2
  echo "  value:   \"${LIMIT}\"" >&2
  echo "  pattern: ^[1-9][0-9]*$" >&2
  echo "  hint: value must match ^[1-9][0-9]*$" >&2
  exit 1
fi

if [[ -n "${_PR_SET}" ]] && [[ "${PR}" == -* ]]; then
  echo "error: nerf-gh-pr-reviews: <pr> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${PR}" ]]; then
  echo "error: nerf-gh-pr-reviews: missing required argument <pr>" >&2
  echo "  hint: provide a value for <pr>" >&2
  usage
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${_PR_SET}" ]] && ! [[ "${PR}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-gh-pr-reviews: argument <pr> does not match required pattern" >&2
  echo "  value:   \"${PR}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: nerf-gh-pr-reviews would run inline script"
  exit 0
fi

LIMIT=${LIMIT:-10}
# --paginate fetches all pages; --slurp collapses them into a single
# JSON array so the jq sort+limit operates over the whole result set
# rather than per page (which would only sort within each page).
# Without these, the latest reviews would be silently missing on PRs
# with >30 reviews because the GitHub API returns them oldest first.
exec gh api --paginate --slurp "repos/{owner}/{repo}/pulls/${PR}/reviews" \
  --jq "sort_by(.submitted_at) | reverse | .[:${LIMIT}] | map({id, author: .user.login, state, commit: .commit_id[0:7], submitted_at, body})"
