fix(bms): improve logging and response parsing
- bms-logging.sh: fixed brace expansion bug, JSON compaction, dynamic log path, single-line JSONL entries
- bms-tickets.sh: support {success:true,result:{...}} responses; fixed ID extraction for create/note; updated update fetch
Tested full lifecycle with logging: create → note → delete note → close → delete
This commit is contained in:
40
scripts/bms-logging.sh
Executable file → Normal file
40
scripts/bms-logging.sh
Executable file → Normal file
@@ -12,48 +12,52 @@ BMS_LOG_DIR="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
|||||||
# Ensure log directory exists
|
# Ensure log directory exists
|
||||||
mkdir -p "$BMS_LOG_DIR"
|
mkdir -p "$BMS_LOG_DIR"
|
||||||
|
|
||||||
# Current log file (by date, UTC)
|
# Compute log file dynamically based on current BMS_LOG_DIR
|
||||||
BMS_LOG_FILE="$BMS_LOG_DIR/$(date -u +%Y-%m-%d).jsonl"
|
|
||||||
|
|
||||||
# Sanitize arguments: strip any sensitive values from a JSON object
|
# Sanitize arguments: strip any sensitive values from a JSON object
|
||||||
# Usage: sanitized=$(sanitize_args '{"password":"secret","token":"abc"}')
|
# Usage: sanitized=$(sanitize_args '{"password":"secret","token":"abc"}')
|
||||||
sanitize_args() {
|
sanitize_args() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
# Remove known sensitive keys; preserve structure
|
# Remove known sensitive keys; preserve structure; output compact JSON to avoid newline issues
|
||||||
jq 'del(.["BMS_PASSWORD"], .["BMS_MFA_CODE"], .["BMS_CLIENT_SECRET"], .["access_token"], .["refresh_token"], .["token"], .["Authorization"])' 2>/dev/null <<<"$input" || echo "$input"
|
jq -c 'del(.["BMS_PASSWORD"], .["BMS_MFA_CODE"], .["BMS_CLIENT_SECRET"], .["access_token"], .["refresh_token"], .["token"], .["Authorization"])' 2>/dev/null <<<"$input" || echo "$input"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Log an action
|
# Log an action
|
||||||
# Arguments: command, args_json, result_json, status (success|error)
|
# Arguments: command, args_json, result_json, status (success|error)
|
||||||
log_action() {
|
log_action() {
|
||||||
local command="$1"
|
local command="$1"
|
||||||
local args_json="${2:-{}}"
|
local args_json="${2:-{\}}"
|
||||||
local result_json="${3:-{}}"
|
local result_json="${3:-{\}}"
|
||||||
local status="${4:-success}"
|
local status="${4:-success}"
|
||||||
|
|
||||||
|
# Ensure we have valid JSON; if pretty-printed, re-compact to a single line
|
||||||
|
local args_compact result_compact
|
||||||
|
args_compact=$(echo "$args_json" | jq -c . 2>/dev/null || echo "$args_json")
|
||||||
|
result_compact=$(echo "$result_json" | jq -c . 2>/dev/null || echo "$result_json")
|
||||||
|
|
||||||
local timestamp
|
local timestamp
|
||||||
timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||||
|
|
||||||
# Sanitize args and result
|
# Compute log file path dynamically
|
||||||
local safe_args safe_result
|
local log_dir="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||||
safe_args=$(sanitize_args "$args_json")
|
mkdir -p "$log_dir" 2>/dev/null
|
||||||
safe_result=$(sanitize_args "$result_json")
|
local log_file="$log_dir/$(date -u +%Y-%m-%d).jsonl"
|
||||||
|
|
||||||
# Build log entry as single JSON line
|
# Use --arg to pass JSON as string, then parse with fromjson inside jq
|
||||||
local entry
|
local entry
|
||||||
entry=$(jq -n \
|
entry=$(jq -nc \
|
||||||
--arg ts "$timestamp" \
|
--arg ts "$timestamp" \
|
||||||
--arg cmd "$command" \
|
--arg cmd "$command" \
|
||||||
--argjson args "$safe_args" \
|
--arg args "$args_compact" \
|
||||||
--argjson result "$safe_result" \
|
--arg result "$result_compact" \
|
||||||
--arg stat "$status" \
|
--arg stat "$status" \
|
||||||
'{timestamp: $ts, command: $cmd, args: $args, result: $result, status: $stat}')
|
'{timestamp: $ts, command: $cmd, args: ($args|fromjson), result: ($result|fromjson), status: $stat}')
|
||||||
|
|
||||||
# Append atomically
|
echo "$entry" >> "$log_file"
|
||||||
echo "$entry" >> "$BMS_LOG_FILE"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get current log file path
|
# Get current log file path
|
||||||
get_log_path() {
|
get_log_path() {
|
||||||
echo "$BMS_LOG_FILE"
|
local log_dir="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||||
|
echo "$log_dir/$(date -u +%Y-%m-%d).jsonl"
|
||||||
}
|
}
|
||||||
|
|||||||
10
scripts/bms-tickets.sh
Executable file → Normal file
10
scripts/bms-tickets.sh
Executable file → Normal file
@@ -269,8 +269,8 @@ cmd_create() {
|
|||||||
response=$(bms_curl POST "/v2/servicedesk/tickets" -d "$body")
|
response=$(bms_curl POST "/v2/servicedesk/tickets" -d "$body")
|
||||||
|
|
||||||
success=$(echo "$response" | jq -r '.success // .Success // empty')
|
success=$(echo "$response" | jq -r '.success // .Success // empty')
|
||||||
ticket_id=$(echo "$response" | jq -r '.Data.Id // .Id // empty')
|
ticket_id=$(echo "$response" | jq -r '.Data.Id // .Id // .result.id // .result.id // empty')
|
||||||
ticket_number=$(echo "$response" | jq -r '.Data.TicketNumber // .TicketNumber // empty')
|
ticket_number=$(echo "$response" | jq -r '.Data.TicketNumber // .TicketNumber // .result.ticketNumber // empty')
|
||||||
|
|
||||||
if [[ "$success" != "true" ]] || [[ -z "$ticket_id" ]] || [[ "$ticket_id" == "null" ]]; then
|
if [[ "$success" != "true" ]] || [[ -z "$ticket_id" ]] || [[ "$ticket_id" == "null" ]]; then
|
||||||
echo "Create ticket failed or returned ambiguous response:" >&2
|
echo "Create ticket failed or returned ambiguous response:" >&2
|
||||||
@@ -321,7 +321,7 @@ cmd_update() {
|
|||||||
|
|
||||||
# Fetch current ticket first so we can do a full PUT with changes merged
|
# Fetch current ticket first so we can do a full PUT with changes merged
|
||||||
local current
|
local current
|
||||||
current=$(bms_curl GET "/v2/servicedesk/tickets/${ticket_id}" | jq '.Data // .')
|
current=$(bms_curl GET "/v2/servicedesk/tickets/${ticket_id}" | jq '.Data // .result // .')
|
||||||
|
|
||||||
local patch="{}"
|
local patch="{}"
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
@@ -358,7 +358,7 @@ cmd_update() {
|
|||||||
|
|
||||||
local response
|
local response
|
||||||
response=$(bms_curl PUT "/v2/servicedesk/tickets/${ticket_id}" -d "$body")
|
response=$(bms_curl PUT "/v2/servicedesk/tickets/${ticket_id}" -d "$body")
|
||||||
echo "$response" | jq -r '"Updated ticket \(.Data.Id // .Id // "'"$ticket_id"'")"'
|
echo "$response" | jq -r '"Updated ticket \(.Data.Id // .Id // .result.id // "'"$ticket_id"'")"'
|
||||||
# Log success
|
# Log success
|
||||||
local args_json result_json
|
local args_json result_json
|
||||||
args_json=$(jq -n --argjson ticket_id "$ticket_id" --argjson patch "$patch" '{ticket_id: $ticket_id, patch: $patch}')
|
args_json=$(jq -n --argjson ticket_id "$ticket_id" --argjson patch "$patch" '{ticket_id: $ticket_id, patch: $patch}')
|
||||||
@@ -404,7 +404,7 @@ cmd_note() {
|
|||||||
local response
|
local response
|
||||||
response=$(bms_curl POST "/v2/servicedesk/tickets/${ticket_id}/notes" -d "$body")
|
response=$(bms_curl POST "/v2/servicedesk/tickets/${ticket_id}/notes" -d "$body")
|
||||||
local note_id
|
local note_id
|
||||||
note_id=$(echo "$response" | jq -r '.Data.Id // .Id // empty')
|
note_id=$(echo "$response" | jq -r '.Data.Id // .Id // .result.id // .result.id // empty')
|
||||||
if [[ -z "$note_id" || "$note_id" == "null" ]]; then
|
if [[ -z "$note_id" || "$note_id" == "null" ]]; then
|
||||||
echo "Note add failed or returned ambiguous response:" >&2
|
echo "Note add failed or returned ambiguous response:" >&2
|
||||||
echo "$response" | jq . >&2
|
echo "$response" | jq . >&2
|
||||||
|
|||||||
Reference in New Issue
Block a user