Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ingest/vendored/notify-slack #305

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ingest/vendored/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -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
85 changes: 61 additions & 24 deletions ingest/vendored/notify-slack
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ upload=0
output=/dev/null
thread_ts=""
broadcast=0
fail_on_error=0
args=()

for arg; do
Expand All @@ -20,6 +21,8 @@ for arg; do
thread_ts="${arg#*=}";;
--broadcast)
broadcast=1;;
--fail-on-error)
fail_on_error=1;;
*)
args+=("$arg");;
esac
Expand All @@ -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 <https://github.com/nextstrain/ingest/pull/47#discussion_r1974802967>
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