#!/usr/bin/env bash
# nerf-az-repos-pr-edit -- Edit a pull request's title and/or description. At least one of --title or --description must be provided. Useful for keeping a PR's metadata in sync with its scope as the branch evolves. Does not change the PR's status, target branch, or reviewers.
# Generated from az-repos manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-az-repos-pr-edit 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-edit [--title <title>] [--description <description>] [--project|-p <project>] <pr_id>

Options:
  --title <title>
      New PR title
  --description <description>
      New PR description (body text; supports markdown)
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <pr_id> (required)
      Pull request ID (numeric)
      Must match: ^[0-9]+$

Maps to: az repos pr update --id <pr_id> <title> <description> <project> --output json

Edit a pull request's title and/or description. At least one of --title or --description must be provided. Useful for keeping a PR's metadata in sync with its scope as the branch evolves. Does not change the PR's status, target branch, or reviewers.
EOF
  exit 1
}

TITLE=""
_TITLE_SET=""
DESCRIPTION=""
_DESCRIPTION_SET=""
PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --title) if [[ -n "${_TITLE_SET}" ]]; then echo "error: --title can only be specified once" >&2; exit 1; fi; TITLE="$2"; _TITLE_SET=true; shift 2 ;;
    --description) if [[ -n "${_DESCRIPTION_SET}" ]]; then echo "error: --description can only be specified once" >&2; exit 1; fi; DESCRIPTION="$2"; _DESCRIPTION_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 ;;
    *) break ;;
  esac
done

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

if [[ -n "${_PR_ID_SET}" ]] && [[ "${PR_ID}" == -* ]]; then
  echo "error: nerf-az-repos-pr-edit: <pr_id> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${PR_ID}" ]]; then
  echo "error: nerf-az-repos-pr-edit: missing required argument <pr_id>" >&2
  echo "  hint: provide a value for <pr_id>" >&2
  usage
fi

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

( [ -n "$TITLE" ] || [ -n "$DESCRIPTION" ] ) || { echo 'error: nerf-az-repos-pr-edit: At least one of --title or --description must be provided.' >&2; exit 1; }

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az repos pr update --id "${PR_ID}" ${_TITLE_SET:+"--title"} ${_TITLE_SET:+"$TITLE"} ${_DESCRIPTION_SET:+"--description"} ${_DESCRIPTION_SET:+"$DESCRIPTION"} ${_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 update --id "${PR_ID}" ${_TITLE_SET:+"--title"} ${_TITLE_SET:+"$TITLE"} ${_DESCRIPTION_SET:+"--description"} ${_DESCRIPTION_SET:+"$DESCRIPTION"} ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json
