#!/usr/bin/env bash
# nerf-az-repos-pr-list -- List pull requests in the project as JSON.
# Generated from az-repos manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-az-repos-pr-list requires bash 4+. Found bash ${BASH_VERSION:-unknown}" >&2
  echo "  hint: on macOS, install a newer bash via 'brew install bash'" >&2
  exit 1
fi

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-az-repos-pr-list [--status <status>] [--top <top>] [--creator <creator>] [--reviewer <reviewer>] [--project|-p <project>]

Options:
  --status <status>
      Filter by status (default: active)
      Allowed values: active, abandoned, completed, all
  --top <top>
      Maximum number of PRs to return (default 10)
      Must match: ^[0-9]+$
  --creator <creator>
      Filter by PR creator
  --reviewer <reviewer>
      Filter by reviewer
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Maps to: az repos pr list <status> <top> <creator> <reviewer> <project> --output json

List pull requests in the project as JSON.
EOF
  exit 1
}

STATUS=""
_STATUS_SET=""
TOP=""
_TOP_SET=""
CREATOR=""
_CREATOR_SET=""
REVIEWER=""
_REVIEWER_SET=""
PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --status) if [[ -n "${_STATUS_SET}" ]]; then echo "error: --status can only be specified once" >&2; exit 1; fi; STATUS="$2"; _STATUS_SET=true; shift 2 ;;
    --top) if [[ -n "${_TOP_SET}" ]]; then echo "error: --top can only be specified once" >&2; exit 1; fi; TOP="$2"; _TOP_SET=true; shift 2 ;;
    --creator) if [[ -n "${_CREATOR_SET}" ]]; then echo "error: --creator can only be specified once" >&2; exit 1; fi; CREATOR="$2"; _CREATOR_SET=true; shift 2 ;;
    --reviewer) if [[ -n "${_REVIEWER_SET}" ]]; then echo "error: --reviewer can only be specified once" >&2; exit 1; fi; REVIEWER="$2"; _REVIEWER_SET=true; shift 2 ;;
    --project|-p) if [[ -n "${_PROJECT_SET}" ]]; then echo "error: --project can only be specified once" >&2; exit 1; fi; PROJECT="$2"; _PROJECT_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) echo "error: unknown argument: $1" >&2; usage ;;
  esac
done

if [[ -n "${_STATUS_SET}" ]] && [[ "${STATUS}" != "active" && "${STATUS}" != "abandoned" && "${STATUS}" != "completed" && "${STATUS}" != "all" ]]; then
  echo "error: nerf-az-repos-pr-list: option --status is not an allowed value" >&2
  echo "  value:   \"${STATUS}\"" >&2
  echo "  allowed: active, abandoned, completed, all" >&2
  echo "  hint: use one of the allowed values" >&2
  exit 1
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${_TOP_SET}" ]] && ! [[ "${TOP}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-repos-pr-list: option --top does not match required pattern" >&2
  echo "  value:   \"${TOP}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az repos pr list ${_STATUS_SET:+"--status"} ${_STATUS_SET:+"$STATUS"} ${_TOP_SET:+"--top"} ${_TOP_SET:+"$TOP"} ${_CREATOR_SET:+"--creator"} ${_CREATOR_SET:+"$CREATOR"} ${_REVIEWER_SET:+"--reviewer"} ${_REVIEWER_SET:+"$REVIEWER"} ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json)
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec az repos pr list ${_STATUS_SET:+"--status"} ${_STATUS_SET:+"$STATUS"} ${_TOP_SET:+"--top"} ${_TOP_SET:+"$TOP"} ${_CREATOR_SET:+"--creator"} ${_CREATOR_SET:+"$CREATOR"} ${_REVIEWER_SET:+"--reviewer"} ${_REVIEWER_SET:+"$REVIEWER"} ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json
