From d0c3dc5eed381f625b93008dd0681b7233b403f0 Mon Sep 17 00:00:00 2001 From: Jover Lee Date: Fri, 28 Feb 2025 10:19:48 -0800 Subject: [PATCH] git subrepo pull ingest/vendored subrepo: subdir: "ingest/vendored" merged: "cd6d31a" upstream: origin: "https://github.com/nextstrain/ingest" branch: "main" commit: "cd6d31a" git-subrepo: version: "0.4.6" origin: "https://github.com/ingydotnet/git-subrepo" commit: "110b9eb" --- ingest/vendored/.gitrepo | 4 +- ingest/vendored/notify-slack | 85 ++++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/ingest/vendored/.gitrepo b/ingest/vendored/.gitrepo index 216600ef..f395f12b 100644 --- a/ingest/vendored/.gitrepo +++ b/ingest/vendored/.gitrepo @@ -6,7 +6,7 @@ [subrepo] remote = https://github.com/nextstrain/ingest branch = main - commit = 258ab8ce898a88089bc88caee336f8d683a0e79a - parent = a1dd6dedc766e0ca82c893d1c6bd1118dec89889 + commit = cd6d31a3b35cd1bb7eddf830c565be6d6e69f27a + parent = 45a96a043c864f303888bd6704f99d3d6cdbaca3 method = merge cmdver = 0.4.6 diff --git a/ingest/vendored/notify-slack b/ingest/vendored/notify-slack index a343435f..d41a5859 100755 --- a/ingest/vendored/notify-slack +++ b/ingest/vendored/notify-slack @@ -8,6 +8,7 @@ upload=0 output=/dev/null thread_ts="" broadcast=0 +fail_on_error=0 args=() for arg; do @@ -20,6 +21,8 @@ for arg; do thread_ts="${arg#*=}";; --broadcast) broadcast=1;; + --fail-on-error) + fail_on_error=1;; *) args+=("$arg");; esac @@ -29,28 +32,62 @@ set -- "${args[@]}" text="${1:?Some message text is required.}" -if [[ "$upload" == 1 ]]; then - echo "Uploading data to Slack with the message: $text" - curl https://slack.com/api/files.upload \ - --header "Authorization: Bearer $SLACK_TOKEN" \ - --form-string channels="$SLACK_CHANNELS" \ - --form-string title="$text" \ - --form-string filename="$text" \ - --form-string thread_ts="$thread_ts" \ - --form file=@/dev/stdin \ - --form filetype=text \ - --fail --silent --show-error \ - --http1.1 \ - --output "$output" -else - echo "Posting Slack message: $text" - curl https://slack.com/api/chat.postMessage \ - --header "Authorization: Bearer $SLACK_TOKEN" \ - --form-string channel="$SLACK_CHANNELS" \ - --form-string text="$text" \ - --form-string thread_ts="$thread_ts" \ - --form-string reply_broadcast="$broadcast" \ - --fail --silent --show-error \ - --http1.1 \ - --output "$output" +send_slack_message() { + if [[ "$upload" == 1 ]]; then + echo "Uploading data to Slack with the message: $text" + + upload_file="$(mktemp -t upload-file-XXXXXX)" + trap "rm -f '$upload_file'" EXIT + + cat /dev/stdin > "$upload_file" + # printf used to strip whitespace from output of macOS/BSD wc + # See + length=$(printf '%d' "$(<"$upload_file" wc -c)") + + upload_info=$(curl https://slack.com/api/files.getUploadURLExternal \ + --header "Authorization: Bearer $SLACK_TOKEN" \ + --form-string filename="$text" \ + --form-string length="$length" \ + --fail --silent --show-error \ + --http1.1 ) + + upload_url="$(jq -r .upload_url <<< "$upload_info")" + curl "$upload_url" \ + --form-string filename="$text" \ + --form file="@$upload_file" \ + --fail --silent --show-error \ + --http1.1 > /dev/null + + files_uploaded="$(jq -r "[{id: .file_id}]" <<< "$upload_info")" + curl -X POST https://slack.com/api/files.completeUploadExternal \ + --header "Authorization: Bearer $SLACK_TOKEN" \ + --form-string channel_id="$SLACK_CHANNELS" \ + --form-string thread_ts="$thread_ts" \ + --form-string files="$files_uploaded" \ + --fail --silent --show-error \ + --http1.1 \ + --output "$output" + + else + echo "Posting Slack message: $text" + curl https://slack.com/api/chat.postMessage \ + --header "Authorization: Bearer $SLACK_TOKEN" \ + --form-string channel="$SLACK_CHANNELS" \ + --form-string text="$text" \ + --form-string thread_ts="$thread_ts" \ + --form-string reply_broadcast="$broadcast" \ + --fail --silent --show-error \ + --http1.1 \ + --output "$output" + fi +} + +if ! send_slack_message; then + if [[ "$fail_on_error" == 1 ]]; then + echo "Sending Slack message failed" + exit 1 + else + echo "Sending Slack message failed, but exiting with success anyway." + exit 0 + fi fi