|
@@ -14,7 +14,6 @@ from sentry.integrations.slack.message_builder.issues import SlackIssuesMessageB
|
|
|
from sentry.integrations.slack.message_builder.metric_alerts import SlackMetricAlertMessageBuilder
|
|
|
from sentry.models import Group, Team, User
|
|
|
from sentry.testutils import TestCase
|
|
|
-from sentry.utils.assets import get_asset_url
|
|
|
from sentry.utils.dates import to_timestamp
|
|
|
from sentry.utils.http import absolute_uri
|
|
|
|
|
@@ -140,7 +139,6 @@ class BuildGroupAttachmentTest(TestCase):
|
|
|
|
|
|
class BuildIncidentAttachmentTest(TestCase):
|
|
|
def test_simple(self):
|
|
|
- logo_url = absolute_uri(get_asset_url("sentry", "images/sentry-email-avatar.png"))
|
|
|
alert_rule = self.create_alert_rule()
|
|
|
incident = self.create_incident(alert_rule=alert_rule, status=2)
|
|
|
trigger = self.create_alert_rule_trigger(alert_rule, CRITICAL_TRIGGER_LABEL, 100)
|
|
@@ -148,34 +146,35 @@ class BuildIncidentAttachmentTest(TestCase):
|
|
|
alert_rule_trigger=trigger, triggered_for_incident=incident
|
|
|
)
|
|
|
title = f"Resolved: {alert_rule.name}"
|
|
|
- incident_footer_ts = (
|
|
|
- "<!date^{:.0f}^Sentry Incident - Started {} at {} | Sentry Incident>".format(
|
|
|
- to_timestamp(incident.date_started), "{date_pretty}", "{time}"
|
|
|
- )
|
|
|
+ timestamp = "<!date^{:.0f}^Started {} at {} | Sentry Incident>".format(
|
|
|
+ to_timestamp(incident.date_started), "{date_pretty}", "{time}"
|
|
|
)
|
|
|
- assert SlackIncidentsMessageBuilder(incident, IncidentStatus.CLOSED).build() == {
|
|
|
- "fallback": title,
|
|
|
- "title": title,
|
|
|
- "title_link": absolute_uri(
|
|
|
+ link = (
|
|
|
+ absolute_uri(
|
|
|
reverse(
|
|
|
- "sentry-metric-alert",
|
|
|
+ "sentry-metric-alert-details",
|
|
|
kwargs={
|
|
|
- "organization_slug": incident.organization.slug,
|
|
|
- "incident_id": incident.identifier,
|
|
|
+ "organization_slug": alert_rule.organization.slug,
|
|
|
+ "alert_rule_id": alert_rule.id,
|
|
|
},
|
|
|
)
|
|
|
- ),
|
|
|
- "text": "0 events in the last 10 minutes",
|
|
|
- "fields": [],
|
|
|
- "mrkdwn_in": ["text"],
|
|
|
- "footer_icon": logo_url,
|
|
|
- "footer": incident_footer_ts,
|
|
|
+ )
|
|
|
+ + f"?alert={incident.identifier}"
|
|
|
+ )
|
|
|
+ assert SlackIncidentsMessageBuilder(incident, IncidentStatus.CLOSED).build() == {
|
|
|
+ "blocks": [
|
|
|
+ {
|
|
|
+ "type": "section",
|
|
|
+ "text": {
|
|
|
+ "type": "mrkdwn",
|
|
|
+ "text": f"<{link}|*{title}*> \n0 events in the last 10 minutes\n{timestamp}",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ ],
|
|
|
"color": LEVEL_TO_COLOR["_incident_resolved"],
|
|
|
- "actions": [],
|
|
|
}
|
|
|
|
|
|
def test_metric_value(self):
|
|
|
- logo_url = absolute_uri(get_asset_url("sentry", "images/sentry-email-avatar.png"))
|
|
|
alert_rule = self.create_alert_rule()
|
|
|
incident = self.create_incident(alert_rule=alert_rule, status=2)
|
|
|
|
|
@@ -186,34 +185,72 @@ class BuildIncidentAttachmentTest(TestCase):
|
|
|
self.create_alert_rule_trigger_action(
|
|
|
alert_rule_trigger=trigger, triggered_for_incident=incident
|
|
|
)
|
|
|
- incident_footer_ts = (
|
|
|
- "<!date^{:.0f}^Sentry Incident - Started {} at {} | Sentry Incident>".format(
|
|
|
- to_timestamp(incident.date_started), "{date_pretty}", "{time}"
|
|
|
+ timestamp = "<!date^{:.0f}^Started {} at {} | Sentry Incident>".format(
|
|
|
+ to_timestamp(incident.date_started), "{date_pretty}", "{time}"
|
|
|
+ )
|
|
|
+ link = (
|
|
|
+ absolute_uri(
|
|
|
+ reverse(
|
|
|
+ "sentry-metric-alert-details",
|
|
|
+ kwargs={
|
|
|
+ "organization_slug": alert_rule.organization.slug,
|
|
|
+ "alert_rule_id": alert_rule.id,
|
|
|
+ },
|
|
|
+ )
|
|
|
)
|
|
|
+ + f"?alert={incident.identifier}"
|
|
|
)
|
|
|
# This should fail because it pulls status from `action` instead of `incident`
|
|
|
assert SlackIncidentsMessageBuilder(
|
|
|
incident, IncidentStatus.CRITICAL, metric_value=metric_value
|
|
|
).build() == {
|
|
|
- "fallback": title,
|
|
|
- "title": title,
|
|
|
- "title_link": absolute_uri(
|
|
|
+ "blocks": [
|
|
|
+ {
|
|
|
+ "type": "section",
|
|
|
+ "text": {
|
|
|
+ "type": "mrkdwn",
|
|
|
+ "text": f"<{link}|*{title}*> \n5000 events in the last 10 minutes\n{timestamp}",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "color": LEVEL_TO_COLOR["fatal"],
|
|
|
+ }
|
|
|
+
|
|
|
+ def test_chart(self):
|
|
|
+ alert_rule = self.create_alert_rule()
|
|
|
+ incident = self.create_incident(alert_rule=alert_rule, status=2)
|
|
|
+ trigger = self.create_alert_rule_trigger(alert_rule, CRITICAL_TRIGGER_LABEL, 100)
|
|
|
+ self.create_alert_rule_trigger_action(
|
|
|
+ alert_rule_trigger=trigger, triggered_for_incident=incident
|
|
|
+ )
|
|
|
+ title = f"Resolved: {alert_rule.name}"
|
|
|
+ timestamp = "<!date^{:.0f}^Started {} at {} | Sentry Incident>".format(
|
|
|
+ to_timestamp(incident.date_started), "{date_pretty}", "{time}"
|
|
|
+ )
|
|
|
+ link = (
|
|
|
+ absolute_uri(
|
|
|
reverse(
|
|
|
- "sentry-metric-alert",
|
|
|
+ "sentry-metric-alert-details",
|
|
|
kwargs={
|
|
|
- "organization_slug": incident.organization.slug,
|
|
|
- "incident_id": incident.identifier,
|
|
|
+ "organization_slug": alert_rule.organization.slug,
|
|
|
+ "alert_rule_id": alert_rule.id,
|
|
|
},
|
|
|
)
|
|
|
- ),
|
|
|
- "text": f"{metric_value} events in the last 10 minutes",
|
|
|
- "fields": [],
|
|
|
- "mrkdwn_in": ["text"],
|
|
|
- "footer_icon": logo_url,
|
|
|
- "footer": incident_footer_ts,
|
|
|
- "color": LEVEL_TO_COLOR["fatal"],
|
|
|
- "actions": [],
|
|
|
- }
|
|
|
+ )
|
|
|
+ + f"?alert={incident.identifier}"
|
|
|
+ )
|
|
|
+ assert SlackIncidentsMessageBuilder(
|
|
|
+ incident, IncidentStatus.CLOSED, chart_url="chart-url"
|
|
|
+ ).build()["blocks"] == [
|
|
|
+ {
|
|
|
+ "type": "section",
|
|
|
+ "text": {
|
|
|
+ "type": "mrkdwn",
|
|
|
+ "text": f"<{link}|*{title}*> \n0 events in the last 10 minutes\n{timestamp}",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {"alt_text": "Metric Alert Chart", "image_url": "chart-url", "type": "image"},
|
|
|
+ ]
|
|
|
|
|
|
|
|
|
class BuildMetricAlertAttachmentTest(TestCase):
|