Skip to content

Commit 9990d20

Browse files
committed
Interpolate the 'text' field in attachments.
Fixes #25. The gist of this change is to iterate over the attachments, pulling out the `text' field and eval/printfing it, and then merging them back together using jq's `*' operator. This may not be the right way to do this. This may not be the best way to do this, but it does seem to work, and that's got to count for something.
1 parent 6317923 commit 9990d20

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

ci/release_notes.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Bug Fixes
2+
3+
- The `text` field of attachments now interpolates environment
4+
variables from the container environment (i.e. BUILD_* et al).
5+
Fixes #25

out

+18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ then
6868
if [[ "${attachments}" == "null" ]]
6969
then
7070
TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
71+
else
72+
# we have attachments; better interpolate the `text' field...
73+
workfile=$(mktemp /tmp/attach.XXXXXX)
74+
outfile=$(mktemp /tmp/attach.XXXXXX)
75+
cat >$workfile <<<$attachments
76+
for x in $(seq 1 $(jq -r 'length' < $workfile)); do
77+
x=$(( x - 1 ))
78+
if [[ $x != 0 ]]; then
79+
echo -n "," >> $outfile
80+
fi
81+
attachment_text="$(jq -r ".[$x].text // "'""' < $workfile)"
82+
interpolated='{}'
83+
if [[ -n "$attachment_text" ]]; then
84+
interpolated="$(echo $(eval printf "%b" \""$attachment_text"\" | jq -R '{"text":.}'))"
85+
fi
86+
jq -s '.[0] * .[1]' <(jq -r ".[$x]" <$workfile) <(echo "$interpolated") >> $outfile
87+
done
88+
attachments=$((echo '['; cat $outfile; echo ']') | jq -r .)
7189
fi
7290
7391
text="$(eval printf "%b" ${text} )"

test/all.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ test metadata_with_payload | jq -e "
145145
test attachments_no_text | jq -e "
146146
.body.text == null and
147147
.body.attachments[0].color == \"danger\" and
148-
.body.attachments[0].text == \"Build failed!\" and
148+
.body.attachments[0].text == \"Build my-build failed!\" and
149149
( .body.attachments | length == 1 )"
150150

151151
test attachments_with_text | jq -e "
152152
.body.text == \"Inline static text\n\" and
153153
.body.attachments[0].color == \"danger\" and
154-
.body.attachments[0].text == \"Build failed!\" and
154+
.body.attachments[0].text == \"Build my-build failed!\" and
155155
( .body.attachments | length == 1 )"
156156

157157
test multiple_channels | jq -e "

test/attachments_no_text.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"attachments": [
77
{
88
"color": "danger",
9-
"text": "Build failed!"
9+
"text": "Build $BUILD_NAME failed!"
1010
}
1111
]
1212
},

test/attachments_with_text.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"attachments": [
88
{
99
"color": "danger",
10-
"text": "Build failed!"
10+
"text": "Build $BUILD_NAME failed!"
1111
}
1212
]
1313
},

0 commit comments

Comments
 (0)