Skip to content

Commit

Permalink
fix: sanitise slack message (#184)
Browse files Browse the repository at this point in the history
* fix: sanitise slack message with quotes

* fix

* fix

* fix

* fix

* fix

* Apply suggestions from code review
  • Loading branch information
v1v authored Dec 3, 2024
1 parent 4263006 commit 8de5f9a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test-slack-send.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ jobs:
Test some characters: & and * and < > \n \r
Failed to assign <${{ github.event.repository.html_url }}|SDH #my-issue>.
- uses: ./slack/send
id: slack-multi-with-double-quotes
with:
bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
channel-id: "#observablt-bots"
thread-timestamp: ${{ steps.slack-send.outputs.thread-timestamp || '' }}
message: |
:wave: !
I have assigned you the following SDH, as to my knowledge you are currently on duty for `area::observability`:
*`urgency:48h` <https://github.com/elastic/kibana/issues/5098|#5098 - Adding the field "system.filesystem.free" to the output of an alert>*
12 changes: 7 additions & 5 deletions slack/send/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ runs:
# https://api.slack.com/reference/surfaces/formatting#escaping
import os
from urllib.parse import unquote
import json
if os.environ["MESSAGE"]:
message = os.environ.get('MESSAGE', 'No message')
Expand All @@ -55,11 +56,12 @@ runs:
else:
raise Exception("message must be set.")
# Escape special characters for GitHub Actions output
message = message.replace('\n', '\\n').replace('\r', '\\r')
print("message={}".format(message))
sanitizedMessage = json.dumps(message)
print("sanitized message={}".format(sanitizedMessage))
with open(os.environ.get('GITHUB_OUTPUT'), "a") as f:
f.write("message={}".format(message))
f.write("message={}".format(sanitizedMessage))
- uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
id: slack_message
Expand All @@ -71,9 +73,9 @@ runs:
unfurl_media: false
channel: "${{ inputs.channel-id }}"
thread_ts: "${{ inputs.thread-timestamp }}"
text: "${{ steps.prepare.outputs.message }}"
text: ${{ steps.prepare.outputs.message }}
blocks:
- type: "section"
text:
type: mrkdwn
text: "${{ steps.prepare.outputs.message }}"
text: ${{ steps.prepare.outputs.message }}

0 comments on commit 8de5f9a

Please sign in to comment.