From fba1bb6a2270a758482f45ec63a066cd4bad240c Mon Sep 17 00:00:00 2001 From: Steve W Date: Tue, 7 Apr 2026 20:12:21 +0000 Subject: [PATCH] Harden ticket creation validation and response checks --- scripts/bms-tickets.sh | 62 ++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 21 deletions(-) mode change 100755 => 100644 scripts/bms-tickets.sh diff --git a/scripts/bms-tickets.sh b/scripts/bms-tickets.sh old mode 100755 new mode 100644 index 68b5e4d..6f6d487 --- a/scripts/bms-tickets.sh +++ b/scripts/bms-tickets.sh @@ -147,6 +147,7 @@ cmd_create() { local assignee_id="" queue_id="" due_date="" open_date="" local template_id="" local interactive=false + local response="" ticket_id="" ticket_number="" success="" while [[ $# -gt 0 ]]; do case "$1" in @@ -198,29 +199,37 @@ cmd_create() { prompt_if_empty "Priority ID" priority_id prompt_if_empty "Type ID" type_id prompt_if_empty "Source ID" source_id - prompt_if_empty "Assignee ID (optional, Enter to skip)" assignee_id - else + prompt_if_empty "Queue ID (optional if Assignee ID provided)" queue_id + if [[ -z "$queue_id" ]]; then + prompt_if_empty "Assignee ID (optional if Queue ID provided)" assignee_id + fi + elif [[ -n "$template_id" ]]; then # When using a template, prompt only for fields still missing that are required - if [[ -n "$template_id" ]]; then - [[ -n "$title" ]] || { read -r -p "Title: " title; } - [[ -n "$details" ]] || { read -r -p "Details: " details; } - [[ -n "$account_id" ]] || { read -r -p "Account ID: " account_id; } - [[ -n "$location_id" ]] || { read -r -p "Location ID: " location_id; } - [[ -n "$status_id" ]] || { read -r -p "Status ID: " status_id; } - [[ -n "$priority_id" ]] || { read -r -p "Priority ID: " priority_id; } - [[ -n "$type_id" ]] || { read -r -p "Type ID: " type_id; } - [[ -n "$source_id" ]] || { read -r -p "Source ID: " source_id; } + [[ -n "$title" ]] || { read -r -p "Title: " title; } + [[ -n "$details" ]] || { read -r -p "Details: " details; } + [[ -n "$account_id" ]] || { read -r -p "Account ID: " account_id; } + [[ -n "$location_id" ]] || { read -r -p "Location ID: " location_id; } + [[ -n "$status_id" ]] || { read -r -p "Status ID: " status_id; } + [[ -n "$priority_id" ]] || { read -r -p "Priority ID: " priority_id; } + [[ -n "$type_id" ]] || { read -r -p "Type ID: " type_id; } + [[ -n "$source_id" ]] || { read -r -p "Source ID: " source_id; } + if [[ -z "$queue_id" && -z "$assignee_id" ]]; then + read -r -p "Queue ID (or leave blank to provide Assignee ID): " queue_id + if [[ -z "$queue_id" ]]; then + read -r -p "Assignee ID: " assignee_id + fi fi fi - [[ -n "$title" ]] || die "--title is required" - [[ -n "$details" ]] || die "--details is required" - [[ -n "$account_id" ]] || die "--account-id is required" - [[ -n "$location_id" ]] || die "--location-id is required" - [[ -n "$status_id" ]] || die "--status-id is required" - [[ -n "$priority_id" ]] || die "--priority-id is required" - [[ -n "$type_id" ]] || die "--type-id is required" - [[ -n "$source_id" ]] || die "--source-id is required" + [[ -n "$title" ]] || die "Missing required field: --title" + [[ -n "$details" ]] || die "Missing required field: --details" + [[ -n "$account_id" ]] || die "Missing required field: --account-id" + [[ -n "$location_id" ]] || die "Missing required field: --location-id" + [[ -n "$status_id" ]] || die "Missing required field: --status-id" + [[ -n "$priority_id" ]] || die "Missing required field: --priority-id" + [[ -n "$type_id" ]] || die "Missing required field: --type-id" + [[ -n "$source_id" ]] || die "Missing required field: --source-id" + [[ -n "$queue_id" || -n "$assignee_id" ]] || die "Missing required routing: provide either --queue-id or --assignee-id" open_date="${open_date:-$(date -u +%Y-%m-%dT%H:%M:%S)}" @@ -253,9 +262,20 @@ cmd_create() { [[ -n "$queue_id" ]] && body=$(echo "$body" | jq --argjson v "$queue_id" '. + {QueueId: $v}') [[ -n "$due_date" ]] && body=$(echo "$body" | jq --arg v "$due_date" '. + {DueDate: $v}') - local response + # Single create call only. No retries here. response=$(bms_curl POST "/v2/servicedesk/tickets" -d "$body") - echo "$response" | jq -r '"Created ticket ID: \(.Data.Id // .Id) — \(.Data.TicketNumber // .TicketNumber // "N/A")"' + + success=$(echo "$response" | jq -r '.success // .Success // empty') + ticket_id=$(echo "$response" | jq -r '.Data.Id // .Id // empty') + ticket_number=$(echo "$response" | jq -r '.Data.TicketNumber // .TicketNumber // empty') + + if [[ "$success" != "true" ]] || [[ -z "$ticket_id" ]] || [[ "$ticket_id" == "null" ]]; then + echo "Create ticket failed or returned ambiguous response:" >&2 + echo "$response" | jq . >&2 + exit 1 + fi + + echo "Created ticket ID: ${ticket_id} — ${ticket_number:-N/A}" } cmd_update() {