8
8
output=/dev/null
9
9
thread_ts=" "
10
10
broadcast=0
11
+ fail_on_error=0
11
12
args=()
12
13
13
14
for arg; do
@@ -20,6 +21,8 @@ for arg; do
20
21
thread_ts=" ${arg#* =} " ;;
21
22
--broadcast)
22
23
broadcast=1;;
24
+ --fail-on-error)
25
+ fail_on_error=1;;
23
26
* )
24
27
args+=(" $arg " );;
25
28
esac
@@ -29,28 +32,62 @@ set -- "${args[@]}"
29
32
30
33
text=" ${1:? Some message text is required.} "
31
34
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
56
93
fi
0 commit comments