Skip to content

Commit

Permalink
feat: add thread message posting (#445)
Browse files Browse the repository at this point in the history
* feat: add thread message posting

fixes #378 

This enables the slack-orb to post to threads. 
See README updates for details.
  • Loading branch information
atanass authored Feb 27, 2024
1 parent faf827d commit 1e65689
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,51 @@ A comma separated list of regex matchable branch or tag names. Notifications wil

See [usage examples](https://circleci.com/developer/orbs/orb/circleci/slack#usage-examples).

## Thread Messages

Post replies in threads with a special parameter `thread_id`. Including this parameter in the `notify` command reference stores the id of the message in a small portion of bytes in cache. Any subsequent invocation of the command with the same value for `thread_id` will post a reply to the initial message in a thread. Example:

```yaml
- slack/notify:
event: always
channel: engineering
thread_id: testing
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Tests started.*",
"emoji": true
}
]
}
]
}
- slack/notify:
event: always
channel: engineering
thread_id: testing
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Tests finished.*",
"emoji": true
}
]
}
]
}
```
---
## FAQ
Expand Down
26 changes: 26 additions & 0 deletions src/commands/notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ parameters:
type: string
default: Slack - Sending Notification
description: Specify a custom step name for this command, if desired
thread_id:
type: string
default: ""
description: |
When set, the first `notify` with a given `thread_id` will appear as a regular slack message.
Any subsequent `notify` usage with the same identifier will be posted within the initial message's thread.
`thread_id` should be set to any arbitrary string to help you identify different threads. See examples for more information.
Enabling thread messages with this parameter implies using a very small amount of cacheing: ~200 B
steps:
- run:
when: on_fail
Expand All @@ -88,6 +96,14 @@ steps:
name: Slack - Detecting Job Status (PASS)
command: |
echo 'export CCI_STATUS="pass"' > /tmp/SLACK_JOB_STATUS
- when:
condition:
not:
equal: [ "", <<parameters.thread_id>> ]
steps:
- restore_cache:
keys:
- cache-<< parameters.thread_id >>-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- run:
when: always
name: << parameters.step_name >>
Expand All @@ -103,6 +119,7 @@ steps:
SLACK_PARAM_IGNORE_ERRORS: "<<parameters.ignore_errors>>"
SLACK_PARAM_DEBUG: "<<parameters.debug>>"
SLACK_PARAM_CIRCLECI_HOST: "<<parameters.circleci_host>>"
SLACK_PARAM_THREAD: "<<parameters.thread_id>>"
SLACK_SCRIPT_NOTIFY: "<<include(scripts/notify.sh)>>"
SLACK_SCRIPT_UTILS: "<<include(scripts/utils.sh)>>"
# import pre-built templates using the orb-pack local script include.
Expand All @@ -111,3 +128,12 @@ steps:
basic_on_hold_1: "<<include(message_templates/basic_on_hold_1.json)>>"
basic_success_1: "<<include(message_templates/basic_success_1.json)>>"
command: <<include(scripts/main.sh)>>
- when:
condition:
not:
equal: [ "", <<parameters.thread_id>> ]
steps:
- save_cache:
key: cache-<< parameters.thread_id >>-{{ .Environment.CIRCLE_WORKFLOW_ID }}
paths:
- /tmp/SLACK_THREAD_INFO
100 changes: 100 additions & 0 deletions src/examples/notify_thread.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
description: |
Send a Slack notification to a channel and use the same message to post replies in a thread.
`thread_id` parameter holds a thread identifier in case there are multiple notifications in the pipeline
usage:
version: 2.1
orbs:
slack: circleci/[email protected]
node: circleci/node:4.1
jobs:
test:
executor:
name: node/default
steps:
- slack/notify:
event: always
channel: engineering
thread_id: testing
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Tests started.*",
"emoji": true
}
]
}
]
}
- slack/notify:
event: always
channel: engineering
thread_id: testing
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Tests finished.*",
"emoji": true
}
]
}
]
}
deploy:
executor:
name: node/default
steps:
- slack/notify:
event: always
channel: engineering
thread_id: deployment
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Deployment started.*",
"emoji": true
}
]
}
]
}
- slack/notify:
event: always
channel: engineering
thread_id: deployment
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*Deployment finished.*",
"emoji": true
}
]
}
]
}
workflows:
deploy_and_notify:
jobs:
- deploy
- test:
requires:
- deploy
35 changes: 35 additions & 0 deletions src/scripts/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,34 @@ BuildMessageBody() {
PostToSlack() {
# Post once per channel listed by the channel parameter
# The channel must be modified in SLACK_MSG_BODY
# If thread_id is used a file containing the initial message `thread_ts` per each channel is persisted
# /tmp/SLACK_THREAD_INFO/<channel_name> will contain:
# <thread_id>=12345.12345

# shellcheck disable=SC2001
for i in $(eval echo \""$SLACK_PARAM_CHANNEL"\" | sed "s/,/ /g")
do
# replace non-alpha
SLACK_PARAM_THREAD=$(echo "$SLACK_PARAM_THREAD" | sed -r 's/[^[:alpha:][:digit:].]/_/g')
# check if the invoked `notify` command is intended to post threaded messages &
# check for persisted thread info file for each channel listed in channel parameter
if [ ! "$SLACK_PARAM_THREAD" = "" ] && [ -f "/tmp/SLACK_THREAD_INFO/$i" ]; then
# get the initial message thread_ts targeting the correct channel and thread id
# || [ "$?" = "1" ] - this is used to avoid exit status 1 if grep doesn't match anything
# shellcheck disable=SC2002
SLACK_THREAD_EXPORT=$(grep -m1 "$SLACK_PARAM_THREAD" /tmp/SLACK_THREAD_INFO/"$i" || [ "$?" = "1" ])
if [ ! "$SLACK_THREAD_EXPORT" = "" ]; then
# if there is an initial message with a thread id, load it into the environment
# thread_id=12345.12345
eval "$SLACK_THREAD_EXPORT"
fi
# get the value of the specified thread from the environment
# SLACK_THREAD_TS=12345.12345
SLACK_THREAD_TS=$(eval "echo \"\$$SLACK_PARAM_THREAD\"")
# append the thread_ts to the body for posting the message in the correct thread
SLACK_MSG_BODY=$(echo "$SLACK_MSG_BODY" | jq --arg thread_ts "$SLACK_THREAD_TS" '.thread_ts = $thread_ts')
fi

echo "Sending to Slack Channel: $i"
SLACK_MSG_BODY=$(echo "$SLACK_MSG_BODY" | jq --arg channel "$i" '.channel = $channel')
if [ "$SLACK_PARAM_DEBUG" -eq 1 ]; then
Expand All @@ -71,6 +95,17 @@ PostToSlack() {
exit 1
fi
fi

# check if the invoked `notify` command is intended to post messages in threads
if [ ! "$SLACK_PARAM_THREAD" = "" ]; then
# get message thread_ts from response
SLACK_THREAD_TS=$(echo "$SLACK_SENT_RESPONSE" | jq '.ts')
if [ ! "$SLACK_THREAD_TS" = "null" ] ; then
# store the thread_ts in the specified channel for the specified thread_id
mkdir -p /tmp/SLACK_THREAD_INFO
echo "$SLACK_PARAM_THREAD=$SLACK_THREAD_TS" >> /tmp/SLACK_THREAD_INFO/"$i"
fi
fi
done
}

Expand Down

0 comments on commit 1e65689

Please sign in to comment.