#!/usr/bin/env bash
# nerf-az-repos-pr-create -- Create a pull request from the current branch. Source branch is auto-detected from HEAD. The target branch defaults to the repo's default branch unless --target-branch is given. Cannot be run from detached HEAD or from main.
# Generated from az-repos manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-az-repos-pr-create [--draft] [--target-branch|-t <target_branch>] [--project|-p <project>] <title> <description>

Switches:
  --draft
      Create the PR as a draft

Options:
  --target-branch, -t <target_branch>
      Target branch for the PR (defaults to the repo's default branch)
      Must match: ^[a-zA-Z0-9_][a-zA-Z0-9_./-]*$
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <title> (required)
      PR title
  <description> (required)
      PR description (body text; supports markdown)

Maps to: az repos pr create --title <title> --description <description> <draft> <target_branch> <project> --output json

Create a pull request from the current branch. Source branch is auto-detected from HEAD. The target branch defaults to the repo's default branch unless --target-branch is given. Cannot be run from detached HEAD or from main.
EOF
  exit 1
}

DRAFT=""
TARGET_BRANCH=""
_TARGET_BRANCH_SET=""
PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --draft) if [[ -n "${DRAFT}" ]]; then echo "error: --draft can only be specified once" >&2; exit 1; fi; DRAFT="true"; shift 1 ;;
    --target-branch|-t) if [[ -n "${_TARGET_BRANCH_SET}" ]]; then echo "error: --target-branch can only be specified once" >&2; exit 1; fi; TARGET_BRANCH="$2"; _TARGET_BRANCH_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

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

_NERF_PATTERN='^[a-zA-Z0-9_][a-zA-Z0-9_./-]*$'
if [[ -n "${_TARGET_BRANCH_SET}" ]] && ! [[ "${TARGET_BRANCH}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-repos-pr-create: option --target-branch does not match required pattern" >&2
  echo "  value:   \"${TARGET_BRANCH}\"" >&2
  echo "  pattern: ^[a-zA-Z0-9_][a-zA-Z0-9_./-]*$" >&2
  echo "  hint: value must match ^[a-zA-Z0-9_][a-zA-Z0-9_./-]*$" >&2
  exit 1
fi

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

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

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

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

git symbolic-ref --quiet HEAD > /dev/null 2>&1 || { echo 'error: nerf-az-repos-pr-create: Cannot create a PR from detached HEAD state. Check out a feature branch first.' >&2; exit 1; }
( [ "$(git symbolic-ref --short HEAD 2>/dev/null)" != "main" ] ) || { echo 'error: nerf-az-repos-pr-create: Cannot create a PR from main. Check out a feature branch first.' >&2; exit 1; }

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az repos pr create --title "${TITLE}" --description "${DESCRIPTION}" ${DRAFT:+"--draft"} ${_TARGET_BRANCH_SET:+"--target-branch"} ${_TARGET_BRANCH_SET:+"$TARGET_BRANCH"} ${_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 create --title "${TITLE}" --description "${DESCRIPTION}" ${DRAFT:+"--draft"} ${_TARGET_BRANCH_SET:+"--target-branch"} ${_TARGET_BRANCH_SET:+"$TARGET_BRANCH"} ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json
