Skip to content

Commit

Permalink
Added --get-thread-url option
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonk committed Dec 17, 2021
1 parent 82d3a8b commit d1ffdb0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bin/inform-slack
Original file line number Diff line number Diff line change
@@ -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)"
Expand Down
6 changes: 6 additions & 0 deletions lib/inform-slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id>` or `-t <id>` - Specify the ID of the main thread as
Expand Down
43 changes: 32 additions & 11 deletions lib/inform-slack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 ;;
Expand Down
30 changes: 30 additions & 0 deletions test/info.bats
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1ffdb0

Please sign in to comment.