From d1ffdb03d9f5e4343a1c76f8a0feda9689ae39c4 Mon Sep 17 00:00:00 2001 From: Jason Kohles Date: Fri, 17 Dec 2021 14:58:32 -0500 Subject: [PATCH] Added --get-thread-url option --- CHANGELOG.md | 9 ++++++++- README.md | 2 +- bin/inform-slack | 2 +- lib/inform-slack.md | 6 ++++++ lib/inform-slack.sh | 43 ++++++++++++++++++++++++++++++++----------- test/info.bats | 30 ++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 14 deletions(-) create mode 100755 test/info.bats diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f890cd..20c73b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ adheres to [Semantic Versioning][semver]. ## [Unreleased] +## [v1.1.0] - 2021-12-17 + +- Added options for getting some useful information about Slack. In + particular the `--get-thread-url` option that will return the URL + you can click on to go directly to the thread being produced. + ## [v1.0.9] - 2021-12-06 - Fix issue with automatic mode not working with `--say`/`--message`. @@ -64,7 +70,8 @@ adheres to [Semantic Versioning][semver]. - First actual release. -[Unreleased]: https://github.com/jasonk/inform-slack/compare/v1.0.9...HEAD +[Unreleased]: https://github.com/jasonk/inform-slack/compare/v1.1.0...HEAD +[v1.1.0]: https://github.com/jasonk/inform-slack/releases/tag/v1.1.0 [v1.0.9]: https://github.com/jasonk/inform-slack/releases/tag/v1.0.9 [v1.0.7]: https://github.com/jasonk/inform-slack/releases/tag/v1.0.7 [v1.0.6]: https://github.com/jasonk/inform-slack/releases/tag/v1.0.6 diff --git a/README.md b/README.md index 037211b..b9cbcc6 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ written to the disk). However, this curl feature was added in version 7.55, so if you have a version of curl older than that, we fall back to including the `Authorization` header as a command-line argument. If you are using -this on a multi-user machine you want to ensure that this doesn't +this on a multi-user machine and you want to ensure that this doesn't happen, you can set `$INFORM_SLACK_REQUIRE_HEADER_SAFETY` to any non-empty value, and if we detect that your curl version is too old we'll just die instead of falling back. diff --git a/bin/inform-slack b/bin/inform-slack index 3b531b0..04be7f9 100755 --- a/bin/inform-slack +++ b/bin/inform-slack @@ -1,7 +1,7 @@ #!/usr/bin/env bash # https://github.com/jasonk/inform-slack -export INFORM_SLACK_VERSION="v1.0.9" +export INFORM_SLACK_VERSION="v1.1.0" export INFORM_SLACK_SELF="$(realpath -P "${BASH_SOURCE[0]}")" export INFORM_SLACK_DIR="$(cd -P "$(dirname "$INFORM_SLACK_SELF")/.." && pwd)" diff --git a/lib/inform-slack.md b/lib/inform-slack.md index e085633..1a6331a 100644 --- a/lib/inform-slack.md +++ b/lib/inform-slack.md @@ -18,6 +18,12 @@ export INFORM_SLACK_THREAD="$(inform-slack --initialize)" exit. * `--list-builders` - List the available builders and exit. * `--list-functions` - List the available functions and exit. +* `--get-team-id` - Get the Slack Team ID associated with your token. +* `--get-team-url` - Get the Slack Team URL for your token. +* `--get-team-name` - Get the Slack Team name for your token. +* `--get-thread-url` - Get the URL to the thread being produced. This + can be useful if you want to include links to the thread from other + tools (such as CI or deployment tools). * `--dry-run` or `-n` - Don't actually send to Slack, just show the JSON that would have been sent. * `--thread ` or `-t ` - Specify the ID of the main thread as diff --git a/lib/inform-slack.sh b/lib/inform-slack.sh index e4de2c6..5c966af 100755 --- a/lib/inform-slack.sh +++ b/lib/inform-slack.sh @@ -202,16 +202,27 @@ run-curl() { } send-api() { - local API="$1" - local PAYLOAD="${2:-$(cat -)}" + local API="$1" PAYLOAD="${2:-$(cat -)}" ; shift 2 local TOKEN="${INFORM_SLACK_TOKEN:-${SLACK_TOKEN:-}}" if [ -z "$TOKEN" ]; then die "No token found"; fi - shift 2 local URL="https://slack.com/api/$API" - local OUTPUT="$(run-curl -XPOST -d@- "$URL" <<<"$PAYLOAD")" + local OUTPUT="$(run-curl -XPOST -d@- "$URL" "$@" <<<"$PAYLOAD")" + check-api-response "$OUTPUT" + echo "$OUTPUT" +} +get-api() { + local API="$1" ; shift 1 + local TOKEN="${INFORM_SLACK_TOKEN:-${SLACK_TOKEN:-}}" + if [ -z "$TOKEN" ]; then die "No token found"; fi + local URL="https://slack.com/api/$API" + local OUTPUT="$(run-curl -XGET "$URL" "$@")" + check-api-response "$OUTPUT" + echo "$OUTPUT" +} +check-api-response() { + local OUTPUT="$(jq . <<<"$1")" local ERR="$(jq -r '.error | select( . != null )' <<<"$OUTPUT")" if [ -n "$ERR" ]; then die "Error: $ERR"; fi - echo "$OUTPUT" } initialize() { @@ -272,14 +283,20 @@ list-builders() { | sort | uniq | grep -v -E '\.md$' } list-functions() { -# while IFS='' read -r FUNC; do -# FUNC="${FUNC#declare -f }" -# echo "FUNC: $FUNC" -# declare -f "$FUNC" -# done < <(declare -F) grep -h -B1 '### TYPE' "$(dirname "$INFORM_SLACK_LIB")"/*.sh | sed -n 's/() {.*//p' -# INFORM_SLACK_LIB="$(realpath -P "${BASH_SOURCE[0]}")" +} +get-team-id() { get-api auth.test | jq -r .team_id; } +get-team-url() { get-api auth.test | jq -r .url; } +get-team-name() { get-api auth.test | jq -r .team; } +get-thread-url() { + local THREAD="${INFORM_SLACK_THREAD:-}" + local CHANNEL="${INFORM_SLACK_CHANNEL:-${SLACK_CHANNEL:-}}" + local TEAM="$(get-team-id)" + if [ -z "$THREAD" ]; then die "No thread id provided"; fi + if [ -z "$CHANNEL" ]; then die "No channel id provided"; fi + if [ -z "$TEAM" ]; then die "Could not determine team id"; fi + echo "https://app.slack.com/client/$TEAM/$CHANNEL/thread/$CHANNEL-$THREAD" } show-help-file() { @@ -342,6 +359,10 @@ inform_slack() { -V|--version) echo "$INFORM_SLACK_VERSION" ; exit ;; --list-builders) list-builders ; exit ;; --list-functions) list-functions ; exit ;; + --get-thread-url) get-thread-url ; exit ;; + --get-team-id) get-team-id ; exit ;; + --get-team-url) get-team-url ; exit ;; + --get-team-name) get-team-name ; exit ;; -n|--dry-run|--dryrun) INFORM_SLACK_DRY_RUN="true" ; shift 1 ;; -I|--msg-id|--msgid) INFORM_SLACK_MSG_ID="true" ; shift 1 ;; diff --git a/test/info.bats b/test/info.bats new file mode 100755 index 0000000..31c8449 --- /dev/null +++ b/test/info.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats + +setup() { load "bats-setup.bash"; } + +@test "cli options --get-team-id" { + run inform-slack --get-team-id + assert_success + assert_output 'TAEFCEDHS' +} + +@test "cli options --get-team-name" { + run inform-slack --get-team-name + assert_success + assert_output 'jasonk-dev' +} + +@test "cli options --get-team-url" { + run inform-slack --get-team-url + assert_success + assert_output 'https://jasonk-dev.slack.com/' +} + +@test "cli options --get-thread-url" { + export INFORM_SLACK_THREAD='FAKE.FAKE' + run inform-slack --get-thread-url + assert_success + assert_output 'https://app.slack.com/client/TAEFCEDHS/C02DXPYRLAE/thread/C02DXPYRLAE-FAKE.FAKE' +} + +# vim:ft=bash