Skip to content

Commit 4ee7da3

Browse files
authored
Merge pull request #305 from nextstrain/update-vendored
Update ingest/vendored/notify-slack
2 parents 45a96a0 + d0c3dc5 commit 4ee7da3

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

ingest/vendored/.gitrepo

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[subrepo]
77
remote = https://github.com/nextstrain/ingest
88
branch = main
9-
commit = 258ab8ce898a88089bc88caee336f8d683a0e79a
10-
parent = a1dd6dedc766e0ca82c893d1c6bd1118dec89889
9+
commit = cd6d31a3b35cd1bb7eddf830c565be6d6e69f27a
10+
parent = 45a96a043c864f303888bd6704f99d3d6cdbaca3
1111
method = merge
1212
cmdver = 0.4.6

ingest/vendored/notify-slack

+61-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ upload=0
88
output=/dev/null
99
thread_ts=""
1010
broadcast=0
11+
fail_on_error=0
1112
args=()
1213

1314
for arg; do
@@ -20,6 +21,8 @@ for arg; do
2021
thread_ts="${arg#*=}";;
2122
--broadcast)
2223
broadcast=1;;
24+
--fail-on-error)
25+
fail_on_error=1;;
2326
*)
2427
args+=("$arg");;
2528
esac
@@ -29,28 +32,62 @@ set -- "${args[@]}"
2932

3033
text="${1:?Some message text is required.}"
3134

32-
if [[ "$upload" == 1 ]]; then
33-
echo "Uploading data to Slack with the message: $text"
34-
curl https://slack.com/api/files.upload \
35-
--header "Authorization: Bearer $SLACK_TOKEN" \
36-
--form-string channels="$SLACK_CHANNELS" \
37-
--form-string title="$text" \
38-
--form-string filename="$text" \
39-
--form-string thread_ts="$thread_ts" \
40-
--form file=@/dev/stdin \
41-
--form filetype=text \
42-
--fail --silent --show-error \
43-
--http1.1 \
44-
--output "$output"
45-
else
46-
echo "Posting Slack message: $text"
47-
curl https://slack.com/api/chat.postMessage \
48-
--header "Authorization: Bearer $SLACK_TOKEN" \
49-
--form-string channel="$SLACK_CHANNELS" \
50-
--form-string text="$text" \
51-
--form-string thread_ts="$thread_ts" \
52-
--form-string reply_broadcast="$broadcast" \
53-
--fail --silent --show-error \
54-
--http1.1 \
55-
--output "$output"
35+
send_slack_message() {
36+
if [[ "$upload" == 1 ]]; then
37+
echo "Uploading data to Slack with the message: $text"
38+
39+
upload_file="$(mktemp -t upload-file-XXXXXX)"
40+
trap "rm -f '$upload_file'" EXIT
41+
42+
cat /dev/stdin > "$upload_file"
43+
# printf used to strip whitespace from output of macOS/BSD wc
44+
# See <https://github.com/nextstrain/ingest/pull/47#discussion_r1974802967>
45+
length=$(printf '%d' "$(<"$upload_file" wc -c)")
46+
47+
upload_info=$(curl https://slack.com/api/files.getUploadURLExternal \
48+
--header "Authorization: Bearer $SLACK_TOKEN" \
49+
--form-string filename="$text" \
50+
--form-string length="$length" \
51+
--fail --silent --show-error \
52+
--http1.1 )
53+
54+
upload_url="$(jq -r .upload_url <<< "$upload_info")"
55+
curl "$upload_url" \
56+
--form-string filename="$text" \
57+
--form file="@$upload_file" \
58+
--fail --silent --show-error \
59+
--http1.1 > /dev/null
60+
61+
files_uploaded="$(jq -r "[{id: .file_id}]" <<< "$upload_info")"
62+
curl -X POST https://slack.com/api/files.completeUploadExternal \
63+
--header "Authorization: Bearer $SLACK_TOKEN" \
64+
--form-string channel_id="$SLACK_CHANNELS" \
65+
--form-string thread_ts="$thread_ts" \
66+
--form-string files="$files_uploaded" \
67+
--fail --silent --show-error \
68+
--http1.1 \
69+
--output "$output"
70+
71+
else
72+
echo "Posting Slack message: $text"
73+
curl https://slack.com/api/chat.postMessage \
74+
--header "Authorization: Bearer $SLACK_TOKEN" \
75+
--form-string channel="$SLACK_CHANNELS" \
76+
--form-string text="$text" \
77+
--form-string thread_ts="$thread_ts" \
78+
--form-string reply_broadcast="$broadcast" \
79+
--fail --silent --show-error \
80+
--http1.1 \
81+
--output "$output"
82+
fi
83+
}
84+
85+
if ! send_slack_message; then
86+
if [[ "$fail_on_error" == 1 ]]; then
87+
echo "Sending Slack message failed"
88+
exit 1
89+
else
90+
echo "Sending Slack message failed, but exiting with success anyway."
91+
exit 0
92+
fi
5693
fi

0 commit comments

Comments
 (0)