Skip to content

Commit 3898df2

Browse files
committed
Use jq to construct request body
1 parent b1dc74d commit 3898df2

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

hooks/command

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,50 @@ make_body() {
2626
local aggregation_key="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_AGGREGATION_KEY:-}"
2727
local source_type_name="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_SOURCE_TYPE_NAME:-}"
2828
local related_event_id="${BUILDKITE_PLUGIN_CREATE_DATADOG_EVENT_RELATED_EVENT_ID:-}"
29+
local body
2930

30-
local tags=""
31+
local tags=( )
3132

3233
while IFS=$'\n' read -r tag ; do
33-
tags="$tags, \"$(escape_json_string "$tag")\""
34+
tags+=( "$tag" )
3435
done < <(plugin_read_list TAGS)
3536

36-
tags="${tags:2}"
37-
38-
echo '{'
39-
echo " $(escaped_pair title "$msg_title")"
40-
echo " ,$(escaped_pair text "$msg_text")"
41-
echo " ,$(escaped_pair priority "$priority")"
42-
echo " ,$(escaped_pair alert_type "$alert_type")"
37+
body="{}"
38+
body="$(add_json_property "$body" title "$msg_title")"
39+
body="$(add_json_property "$body" text "$msg_text")"
40+
body="$(add_json_property "$body" priority "$priority")"
41+
body="$(add_json_property "$body" alert_type "$alert_type")"
4342

4443
if [ -n "$host" ]; then
45-
echo " ,$(escaped_pair host "$host")"
44+
body="$(add_json_property "$body" host "$host")"
4645
fi
4746

4847
if [ -n "$aggregation_key" ]; then
49-
echo " ,$(escaped_pair aggregation_key "$aggregation_key")"
48+
body="$(add_json_property "$body" aggregation_key "$aggregation_key")"
5049
fi
5150

5251
if [ -n "$related_event_id" ]; then
53-
echo " ,$(escaped_pair related_event_id "$related_event_id")"
52+
body="$(add_json_property "$body" related_event_id "$related_event_id")"
5453
fi
5554

5655
if [ -n "$source_type_name" ]; then
57-
echo " ,$(escaped_pair source_type_name "$source_type_name")"
56+
body="$(add_json_property "$body" source_type_name "$source_type_name")"
5857
fi
5958

60-
if [ -n "$tags" ]; then
61-
echo ' ,"tags": ['"$tags"']'
59+
if [ "${#tags[@]}" -ne 0 ]; then
60+
body="$(echo "$body" | jq '. | .["tags"]=$ARGS.positional' --args "${tags[@]}")"
6261
fi
6362

64-
echo '}'
63+
echo "$body"
6564
}
6665

67-
# probably buggy, PRs welcome
68-
escape_json_string() {
69-
local sed_cmd
70-
sed_cmd="$(command -v sed)"
71-
72-
# use gsed if it's available
73-
if command -v gsed >/dev/null 2>&1; then
74-
sed_cmd="$(command -v gsed)"
75-
fi
66+
# technique from https://stackoverflow.com/a/38862221
67+
add_json_property() {
68+
to="$1"
69+
property="$2"
70+
value="$3"
7671

77-
# escape slashes, tabs, newlines, then double-quotes
78-
echo "$1" | $sed_cmd -Ee 's/\\/\\\\/g' | $sed_cmd -Ee 's/\t/\\t/g' | $sed_cmd -Ee ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | $sed_cmd -Ee 's/"/\\"/g'
72+
echo "$to" | jq ". | .[\"$property\"]=\$value" --arg value "$value"
7973
}
8074

8175
# https://github.com/buildkite-plugins/shellcheck-buildkite-plugin/blob/08694ed7f660dfd21e7639e8e861f23ba605d351/hooks/command#L5-L20
@@ -96,8 +90,4 @@ plugin_read_list() {
9690
fi
9791
}
9892

99-
escaped_pair() {
100-
echo "\"$1\": \"$(escape_json_string "$2")\""
101-
}
102-
10393
main

0 commit comments

Comments
 (0)