#!/usr/bin/env bash
# nerf-az-boards-wi-comment -- Add a comment to any work item.
# Generated from az-boards 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-boards-wi-comment 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-boards-wi-comment [--project|-p <project>] <wi_id> <comment>

Options:
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <wi_id> (required)
      Work item ID (numeric)
      Must match: ^[0-9]+$
  <comment> (required)
      Comment text. Stored as HTML on the work item, so HTML markup renders (e.g. "<p>See <a href='...'>this</a></p>"). Plain text is stored verbatim and renders as-is.

Maps to: az boards work-item update --id <wi_id> --discussion <comment> <project> --output none

Add a comment to any work item.
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

_WI_ID_SET=""
if [[ $# -gt 0 ]]; then
  WI_ID="$1"
  _WI_ID_SET=true
  shift
else
  WI_ID=""
fi
_COMMENT_SET=""
if [[ $# -gt 0 ]]; then
  COMMENT="$1"
  _COMMENT_SET=true
  shift
else
  COMMENT=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-az-boards-wi-comment: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_WI_ID_SET}" ]] && [[ "${WI_ID}" == -* ]]; then
  echo "error: nerf-az-boards-wi-comment: <wi_id> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${WI_ID}" ]]; then
  echo "error: nerf-az-boards-wi-comment: missing required argument <wi_id>" >&2
  echo "  hint: provide a value for <wi_id>" >&2
  usage
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${_WI_ID_SET}" ]] && ! [[ "${WI_ID}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-boards-wi-comment: argument <wi_id> does not match required pattern" >&2
  echo "  value:   \"${WI_ID}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ -n "${_COMMENT_SET}" ]] && [[ "${COMMENT}" == -* ]]; then
  echo "error: nerf-az-boards-wi-comment: <comment> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${COMMENT}" ]]; then
  echo "error: nerf-az-boards-wi-comment: missing required argument <comment>" >&2
  echo "  hint: provide a value for <comment>" >&2
  usage
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az boards work-item update --id "${WI_ID}" --discussion "${COMMENT}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output none)
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec az boards work-item update --id "${WI_ID}" --discussion "${COMMENT}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output none
