Skip to content

Commit

Permalink
Revert "Revert "feat: add scheduling messages (#453) (#457)" (#463)" (#…
Browse files Browse the repository at this point in the history
…464)

This reverts commit c0b82d5.
  • Loading branch information
marboledacci authored Sep 20, 2024
1 parent c0b82d5 commit f2b1d77
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ workflows:
filters: *filters
exclude: RC010
- shellcheck/check:
shell: sh
shell: bash
filters: *filters
- bats/run:
path: ./src/tests
Expand Down
20 changes: 19 additions & 1 deletion .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,26 @@ jobs:
}
]
}
- slack/notify:
event: always
debug: true
step_name: "Custom template with scheduled offset"
scheduled_offset_seconds: 600 # 600 seconds after the job starts
custom: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Sample sent using schedule offset after 5 minutes >"
}
}
]
}
- run:
name: Dynamically populate the mention and export the template as an environment variable
shell: bash -eo pipefail
command: |
echo 'export COMMIT_MESSAGE="This text comes from an environment variable"' >> $BASH_ENV
echo 'export SLACK_PARAM_MENTIONS="$COMMIT_MESSAGE"' >> $BASH_ENV
Expand Down Expand Up @@ -273,4 +291,4 @@ executors:
machine:
image: windows-server-2019-vs2019:current
resource_class: windows.medium
shell: bash.exe
shell: powershell.exe
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ Post replies in threads with a special parameter `thread_id`. Including this par
}
```

## Scheduled Message

Set the `scheduled_offset_seconds` special parameter to a number of seconds if you want to post a scheduled message. Example:

```yaml
- slack/notify:
event: always
scheduled_offset_seconds: 30
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*This is a text notification*",
"emoji": true
}
]
}
]
}
```


---

## FAQ
Expand Down
9 changes: 9 additions & 0 deletions src/commands/notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ parameters:
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
scheduled_offset_seconds:
type: integer
default: 0
description: |
When set, the notification is a scheduled message.
steps:
- run:
shell: bash -eo pipefail
when: on_fail
name: Slack - Detecting Job Status (FAIL)
command: |
echo 'export CCI_STATUS="fail"' > /tmp/SLACK_JOB_STATUS
- run:
shell: bash -eo pipefail
when: on_success
name: Slack - Detecting Job Status (PASS)
command: |
Expand All @@ -108,6 +115,7 @@ steps:
when: always
- run:
when: always
shell: bash -eo pipefail
name: << parameters.step_name >>
environment:
SLACK_PARAM_EVENT: "<<parameters.event>>"
Expand All @@ -122,6 +130,7 @@ steps:
SLACK_PARAM_DEBUG: "<<parameters.debug>>"
SLACK_PARAM_CIRCLECI_HOST: "<<parameters.circleci_host>>"
SLACK_PARAM_THREAD: "<<parameters.thread_id>>"
SLACK_PARAM_OFFSET: "<<parameters.scheduled_offset_seconds>>"
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 Down
37 changes: 37 additions & 0 deletions src/examples/scheduled_scheduled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
description: |
Send a custom notification using Slack's Block Kit Builder.
Create the payload code and paste it in your notify command's custom parameter.
Detailed instructions in the GitHub readme.
https://app.slack.com/block-kit-builder
usage:
version: 2.1
orbs:
slack: circleci/[email protected]
jobs:
scheduled_notify:
docker:
- image: cimg/base:stable
steps:
- slack/notify:
event: always
scheduled_offset_seconds: 30 # 30 seconds after the job starts
custom: |
{
"blocks": [
{
"type": "section",
"fields": [
{
"type": "plain_text",
"text": "*This is a scheduled text notification*",
"emoji": true
}
]
}
]
}
workflows:
send-notification:
jobs:
- scheduled_notify:
context: slack-secrets
2 changes: 1 addition & 1 deletion src/scripts/main.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

# Workaround for Windows Support
# For details, see: https://github.com/CircleCI-Public/slack-orb/pull/380
Expand Down
23 changes: 21 additions & 2 deletions src/scripts/notify.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env bash
# shellcheck disable=SC2016,SC3043

if [ "$SLACK_PARAM_DEBUG" -eq 1 ]; then
Expand Down Expand Up @@ -80,7 +80,25 @@ PostToSlack() {
echo "The message body being sent to Slack can be found below. To view redacted values, rerun the job with SSH and access: ${SLACK_MSG_BODY_LOG}"
echo "$SLACK_MSG_BODY"
fi
SLACK_SENT_RESPONSE=$(curl -s -f -X POST -H 'Content-type: application/json' -H "Authorization: Bearer $SLACK_ACCESS_TOKEN" --data "$SLACK_MSG_BODY" https://slack.com/api/chat.postMessage)

if [ "${SLACK_PARAM_OFFSET:0}" -ne 0 ]; then
if date --version >/dev/null 2>&1; then
# GNU date function
POST_AT=$(date -d "now + ${SLACK_PARAM_OFFSET} seconds" +%s)
elif date -v+1S >/dev/null 2>&1; then
# BSD date function
POST_AT=$(date -v"+${SLACK_PARAM_OFFSET}S" +%s)
else
# Alpine
POST_AT=$(date -u +%s | awk -v sec="$SLACK_PARAM_OFFSET" '{print $1 + sec}')
fi
SLACK_MSG_BODY=$(echo "$SLACK_MSG_BODY" | jq --arg post_at "$POST_AT" '.post_at = ($post_at|tonumber)')
# text is required for scheduled messages
SLACK_MSG_BODY=$(echo "$SLACK_MSG_BODY" | jq '.text = "Dummy fallback text"')
SLACK_SENT_RESPONSE=$(curl -s -f -X POST -H 'Content-type: application/json' -H "Authorization: Bearer $SLACK_ACCESS_TOKEN" --data "$SLACK_MSG_BODY" https://slack.com/api/chat.scheduleMessage)
else
SLACK_SENT_RESPONSE=$(curl -s -f -X POST -H 'Content-type: application/json' -H "Authorization: Bearer $SLACK_ACCESS_TOKEN" --data "$SLACK_MSG_BODY" https://slack.com/api/chat.postMessage)
fi

if [ "$SLACK_PARAM_DEBUG" -eq 1 ]; then
printf "%s\n" "$SLACK_SENT_RESPONSE" > "$SLACK_SENT_RESPONSE_LOG"
Expand Down Expand Up @@ -144,6 +162,7 @@ FilterBy() {
fi
# If any pattern supplied matches the current branch or the current tag, proceed; otherwise, exit with message.
FLAG_MATCHES_FILTER="false"
# shellcheck disable=SC2001
for i in $(echo "$1" | sed "s/,/ /g"); do
if echo "$2" | grep -Eq "^${i}$"; then
FLAG_MATCHES_FILTER="true"
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/false
# shellcheck shell=sh
# shellcheck shell=bash
# shellcheck disable=SC2154

detect_os() {
Expand Down

0 comments on commit f2b1d77

Please sign in to comment.