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

Autoformat hyperlinks in Slack alerts #6845

Merged
merged 2 commits into from
Apr 3, 2024
Merged
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
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
Loading