Skip to content

Commit

Permalink
Autoformat hyperlinks in Slack alerts
Browse files Browse the repository at this point in the history
Format the Slack message using the "mrkdwn" type, which will make
hyperlinks clickable.

New test for Slack destination.
  • Loading branch information
github-actions committed Apr 2, 2024
1 parent 1672cd9 commit c619cd7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions redash/destinations/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def notify(self, alert, query, user, new_state, app, host, metadata, options):
fields = [
{
"title": "Query",
"type": "mrkdwn",
"value": "{host}/queries/{query_id}".format(host=host, query_id=query.id),
"short": True,
},
{
"title": "Alert",
"type": "mrkdwn",
"value": "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id),
"short": True,
},
]
if alert.custom_body:
Expand Down
54 changes: 54 additions & 0 deletions tests/handlers/test_destinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from redash.destinations.asana import Asana
from redash.destinations.datadog import Datadog
from redash.destinations.discord import Discord
from redash.destinations.slack import Slack
from redash.destinations.webex import Webex
from redash.models import Alert, NotificationDestination
from tests import BaseTestCase
Expand Down Expand Up @@ -201,6 +202,59 @@ def test_asana_notify_calls_requests_post():
assert mock_response.status_code == 204


def test_slack_notify_calls_requests_post():
alert = mock.Mock(spec_set=["id", "name", "custom_subject", "custom_body", "render_template"])
alert.id = 1
alert.name = "Test Alert"
alert.custom_subject = "Test custom subject"
alert.custom_body = "Test custom body"

alert.render_template = mock.Mock(return_value={"Rendered": "template"})
query = mock.Mock()
query.id = 1

user = mock.Mock()
app = mock.Mock()
host = "https://localhost:5000"
options = {"url": "https://slack.com/api/api.test"}
metadata = {"Scheduled": False}

new_state = Alert.TRIGGERED_STATE
destination = Slack(options)

with mock.patch("redash.destinations.slack.requests.post") as mock_post:
mock_response = mock.Mock()
mock_response.status_code = 204
mock_post.return_value = mock_response

destination.notify(alert, query, user, new_state, app, host, metadata, options)

query_link = f"{host}/queries/{query.id}"
alert_link = f"{host}/alerts/{alert.id}"

expected_payload = {
"attachments": [
{
"text": "Test custom subject",
"color": "#c0392b",
"fields": [
{"title": "Query", "type": "mrkdwn", "value": query_link},
{"title": "Alert", "type": "mrkdwn", "value": alert_link},
{"title": "Description", "value": "Test custom body"},
],
}
]
}

mock_post.assert_called_once_with(
"https://slack.com/api/api.test",
data=json.dumps(expected_payload).encode(),
timeout=5.0,
)

assert mock_response.status_code == 204


def test_webex_notify_calls_requests_post():
alert = mock.Mock(spec_set=["id", "name", "custom_subject", "custom_body", "render_template"])
alert.id = 1
Expand Down

0 comments on commit c619cd7

Please sign in to comment.