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
|
||||
mkdir -p "$BMS_LOG_DIR"
|
||||
|
||||
# Current log file (by date, UTC)
|
||||
BMS_LOG_FILE="$BMS_LOG_DIR/$(date -u +%Y-%m-%d).jsonl"
|
||||
# Compute log file dynamically based on current BMS_LOG_DIR
|
||||
|
||||
# Sanitize arguments: strip any sensitive values from a JSON object
|
||||
# Usage: sanitized=$(sanitize_args '{"password":"secret","token":"abc"}')
|
||||
sanitize_args() {
|
||||
local input="$1"
|
||||
# Remove known sensitive keys; preserve structure
|
||||
jq 'del(.["BMS_PASSWORD"], .["BMS_MFA_CODE"], .["BMS_CLIENT_SECRET"], .["access_token"], .["refresh_token"], .["token"], .["Authorization"])' 2>/dev/null <<<"$input" || echo "$input"
|
||||
# Remove known sensitive keys; preserve structure; output compact JSON to avoid newline issues
|
||||
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
|
||||
# Arguments: command, args_json, result_json, status (success|error)
|
||||
log_action() {
|
||||
local command="$1"
|
||||
local args_json="${2:-{}}"
|
||||
local result_json="${3:-{}}"
|
||||
local args_json="${2:-{\}}"
|
||||
local result_json="${3:-{\}}"
|
||||
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
|
||||
timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
|
||||
# Sanitize args and result
|
||||
local safe_args safe_result
|
||||
safe_args=$(sanitize_args "$args_json")
|
||||
safe_result=$(sanitize_args "$result_json")
|
||||
# Compute log file path dynamically
|
||||
local log_dir="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||
mkdir -p "$log_dir" 2>/dev/null
|
||||
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
|
||||
entry=$(jq -n \
|
||||
entry=$(jq -nc \
|
||||
--arg ts "$timestamp" \
|
||||
--arg cmd "$command" \
|
||||
--argjson args "$safe_args" \
|
||||
--argjson result "$safe_result" \
|
||||
--arg args "$args_compact" \
|
||||
--arg result "$result_compact" \
|
||||
--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" >> "$BMS_LOG_FILE"
|
||||
echo "$entry" >> "$log_file"
|
||||
}
|
||||
|
||||
# Get current log file 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")
|
||||
|
||||
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')
|
||||
ticket_id=$(echo "$response" | jq -r '.Data.Id // .Id // .result.id // .result.id // empty')
|
||||
ticket_number=$(echo "$response" | jq -r '.Data.TicketNumber // .TicketNumber // .result.ticketNumber // empty')
|
||||
|
||||
if [[ "$success" != "true" ]] || [[ -z "$ticket_id" ]] || [[ "$ticket_id" == "null" ]]; then
|
||||
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
|
||||
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="{}"
|
||||
while [[ $# -gt 0 ]]; do
|
||||
@@ -358,7 +358,7 @@ cmd_update() {
|
||||
|
||||
local response
|
||||
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
|
||||
local args_json result_json
|
||||
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
|
||||
response=$(bms_curl POST "/v2/servicedesk/tickets/${ticket_id}/notes" -d "$body")
|
||||
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
|
||||
echo "Note add failed or returned ambiguous response:" >&2
|
||||
echo "$response" | jq . >&2
|
||||
|
||||
Reference in New Issue
Block a user