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

Fix SLACK_THREAD_TS not working and improve documentation #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ By default, action is designed to run with minimal configuration but you can alt
| SLACK_FOOTER | Powered By rtCamp's GitHub Actions Library | Slack message footer. |
| MSG_MINIMAL | - | If set to `true`, removes: `Ref`, `Event`, `Actions URL` and `Commit` from the message. You can optionally whitelist any of these 4 removed values by passing it comma separated to the variable instead of `true`. (ex: `MSG_MINIMAL: event` or `MSG_MINIMAL: ref,actions url`, etc.) |
| SLACKIFY_MARKDOWN | - | If set to `true`, it will convert markdown to slack format. (ex: `*bold*` to `bold`) Note: This only works for custom messages and not for the default message generated by the action. Credits: [slackify-markdown-action](https://github.com/marketplace/actions/slack-markdown-converter) |
| SLACK_THREAD_TS | - | If you want to send message in a thread, you can pass the timestamp of the parent message to this variable. You can get the timestamp of the parent message from the message URL in Slack. (ex: `SLACK_THREAD_TS: 1586130833.000100`) |
| SLACK_THREAD_TS | - | If you want to send message in a thread, you can pass the timestamp of the parent message to this variable. You can get the timestamp of the parent message from the message URL in Slack. (ex: `SLACK_THREAD_TS: 1586130833.000100`). To obtain the timestamp of the parent message, follow these steps: |
| | | 1. Open the Slack message you want to reply to in a thread. |
| | | 2. Click on the "More actions" button (three vertical dots) on the message. |
| | | 3. Select "Copy link" from the dropdown menu. |
| | | 4. Paste the copied link in a text editor or any other place where you can see the full URL. |
| | | 5. The timestamp of the parent message is the last part of the URL, which looks like `1586130833.000100`. |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a mention here that it's now possible to get a reference to a previously posted message via the ts output?

| SLACK_TOKEN | - | If you want to send message to a channel using a slack token. You will need to pass a channel in order to send messages using token, requiring a value for ``SLACK_CHANNEL``. Note that in case both webhook url and token are provided, webhook url will be prioritized. |
| SLACK_MESSAGE_ON_SUCCESS | - | If set, will send the provided message instead of the default message when the passed status (through ``SLACK_COLOR``) is `success`. |
| SLACK_MESSAGE_ON_FAILURE | - | If set, will send the provided message instead of the default message when the passed status (through ``SLACK_COLOR``) is `failure`. |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ runs:
uses: "docker://ghcr.io/rtcamp/action-slack-notify:v2.3.2"
env:
GITHUB_RUN: "${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}"
outputs:
ts:
description: 'The timestamp of the sent message'
value: ${{ steps.slack-notify.outputs.ts }}
branding:
icon: 'bell'
color: 'yellow'
5 changes: 5 additions & 0 deletions entrypoint.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ if [[ -d "$custom_path" ]]; then
chmod +x /*.sh
fi

# Handle the SLACK_THREAD_TS variable
if [[ -n "$SLACK_THREAD_TS" ]]; then
export SLACK_THREAD_TS
fi

bash "$main_script"
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ func send_raw(endpoint string, payload []byte) error {
}
}

// Extract the `ts` value from the Slack response
ts, err := extractTs(res)
if err != nil {
return err
}

// Set the `ts` output
fmt.Printf("::set-output name=ts::%s\n", ts)

return nil
}

Expand Down Expand Up @@ -389,3 +398,17 @@ func sendFile(filename string, message string, channel string, thread_ts string)
fmt.Println(res.Status)
return nil
}

// Function to extract the `ts` value from the Slack response
func extractTs(res *http.Response) (string, error) {
defer res.Body.Close()
var responseBody map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&responseBody); err != nil {
return "", err
}
ts, ok := responseBody["ts"].(string)
if !ok {
return "", fmt.Errorf("Failed to extract ts from response")
}
return ts, nil
}
5 changes: 5 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ export SLACK_MESSAGE_ON_SUCCESS
export SLACK_MESSAGE_ON_FAILURE
export SLACK_MESSAGE_ON_CANCEL

# Handle the SLACK_THREAD_TS variable
if [[ -n "$SLACK_THREAD_TS" ]]; then
export SLACK_THREAD_TS
fi

slack-notify "$@"