#!/usr/bin/env bash
# nerf-az-repos-pr-comments -- List all comment threads on a pull request as JSON. Project and repository are auto-detected from the git remote (origin); pass --project to override the project (the repository is always taken from origin).
# 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-comments 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-comments [--project|-p <project>] <pr_id>

Options:
  --project, -p <project>
      Azure DevOps project name or ID (overrides auto-detection from the origin remote)

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

List all comment threads on a pull request as JSON. Project and repository are auto-detected from the git remote (origin); pass --project to override the project (the repository is always taken from origin).
EOF
  exit 1
}

PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --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-comments: 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-comments: <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-comments: 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-comments: 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

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

REMOTE_URL=$(git remote get-url origin 2>/dev/null || true)
if [ -z "$REMOTE_URL" ]; then
  echo "error: could not detect git remote 'origin'" >&2
  exit 1
fi
# Extract project and repo from Azure DevOps remote URL
# HTTPS: https://dev.azure.com/{org}/{project}/_git/{repo}
# SSH:   git@ssh.dev.azure.com:v3/{org}/{project}/{repo}
if echo "$REMOTE_URL" | grep -q '/_git/'; then
  DETECTED_PROJECT=$(echo "$REMOTE_URL" | sed -E 's|.*dev.azure.com/[^/]+/([^/]+)/_git/.*|\1|')
  REPO_NAME=$(echo "$REMOTE_URL" | sed -E 's|.*/_git/||; s|\.git$||')
elif echo "$REMOTE_URL" | grep -q 'ssh.dev.azure.com'; then
  DETECTED_PROJECT=$(echo "$REMOTE_URL" | sed -E 's|.*:v3/[^/]+/([^/]+)/.*|\1|')
  REPO_NAME=$(echo "$REMOTE_URL" | sed -E 's|.*:v3/[^/]+/[^/]+/||; s|\.git$||')
else
  echo "error: unrecognized Azure DevOps remote URL format: '$REMOTE_URL'" >&2
  exit 1
fi
# User-supplied --project overrides the auto-detected project; repo is always taken from origin.
PROJECT="${PROJECT:-$DETECTED_PROJECT}"
if [ -z "$PROJECT" ] || [ -z "$REPO_NAME" ]; then
  echo "error: could not extract project/repo from remote URL '$REMOTE_URL'" >&2
  exit 1
fi
exec az devops invoke \
  --area git \
  --resource pullRequestThreads \
  --route-parameters "project=$PROJECT" "repositoryId=$REPO_NAME" "pullRequestId=$PR_ID" \
  --output json
