Просмотр исходного кода

fix(discord): Fix alert formatting (#57290)

Closes https://github.com/getsentry/sentry/issues/57289

<img width="449" alt="Screenshot 2023-10-02 at 9 12 37 AM"
src="https://github.com/getsentry/sentry/assets/22582037/4fc6e1c0-4335-4ed0-9ea2-46a896751775">
Julia Hoge 1 год назад
Родитель
Сommit
2500a36dc4

+ 6 - 5
src/sentry/integrations/discord/message_builder/metric_alerts.py

@@ -10,7 +10,6 @@ from sentry.integrations.discord.message_builder.base.base import DiscordMessage
 from sentry.integrations.discord.message_builder.base.embed.base import DiscordMessageEmbed
 from sentry.integrations.discord.message_builder.base.embed.base import DiscordMessageEmbed
 from sentry.integrations.discord.message_builder.base.embed.image import DiscordMessageEmbedImage
 from sentry.integrations.discord.message_builder.base.embed.image import DiscordMessageEmbedImage
 from sentry.integrations.metric_alerts import metric_alert_attachment_info
 from sentry.integrations.metric_alerts import metric_alert_attachment_info
-from sentry.integrations.slack.utils.escape import escape_slack_text
 
 
 
 
 class DiscordMetricAlertMessageBuilder(DiscordMessageBuilder):
 class DiscordMetricAlertMessageBuilder(DiscordMessageBuilder):
@@ -39,9 +38,9 @@ class DiscordMetricAlertMessageBuilder(DiscordMessageBuilder):
             DiscordMessageEmbed(
             DiscordMessageEmbed(
                 title=data["title"],
                 title=data["title"],
                 url=f"{data['title_link']}&referrer=discord",
                 url=f"{data['title_link']}&referrer=discord",
-                description=f"<{data['title_link']}|*{escape_slack_text(data['title'])}*>  \n{data['text']}",
+                description=f"{data['text']}{get_started_at(data['date_started'])}",
                 color=LEVEL_TO_COLOR[INCIDENT_COLOR_MAPPING.get(data["status"], "")],
                 color=LEVEL_TO_COLOR[INCIDENT_COLOR_MAPPING.get(data["status"], "")],
-                image=DiscordMessageEmbedImage(self.chart_url) if self.chart_url else None,
+                image=DiscordMessageEmbedImage(url=self.chart_url) if self.chart_url else None,
             )
             )
         ]
         ]
 
 
@@ -49,5 +48,7 @@ class DiscordMetricAlertMessageBuilder(DiscordMessageBuilder):
 
 
 
 
 def get_started_at(timestamp: datetime) -> str:
 def get_started_at(timestamp: datetime) -> str:
-    unix_timestamp = int(time.mktime(timestamp.timetuple()))
-    return f"Started <t:{unix_timestamp}:R>"
+    if timestamp:
+        unix_timestamp = int(time.mktime(timestamp.timetuple()))
+        return f"\nStarted <t:{unix_timestamp}:R>"
+    return ""

+ 14 - 13
tests/sentry/integrations/discord/test_message_builder.py

@@ -7,6 +7,7 @@ from sentry.incidents.models import IncidentStatus
 from sentry.integrations.discord.message_builder import LEVEL_TO_COLOR
 from sentry.integrations.discord.message_builder import LEVEL_TO_COLOR
 from sentry.integrations.discord.message_builder.metric_alerts import (
 from sentry.integrations.discord.message_builder.metric_alerts import (
     DiscordMetricAlertMessageBuilder,
     DiscordMetricAlertMessageBuilder,
+    get_started_at,
 )
 )
 from sentry.testutils.cases import TestCase
 from sentry.testutils.cases import TestCase
 from sentry.testutils.silo import region_silo_test
 from sentry.testutils.silo import region_silo_test
@@ -30,12 +31,13 @@ class BuildMetricAlertAttachmentTest(TestCase):
                 },
                 },
             )
             )
         )
         )
+
         assert DiscordMetricAlertMessageBuilder(self.alert_rule).build() == {
         assert DiscordMetricAlertMessageBuilder(self.alert_rule).build() == {
             "content": "",
             "content": "",
             "embeds": [
             "embeds": [
                 {
                 {
                     "title": title,
                     "title": title,
-                    "description": f"<{link}|*{title}*>  \n",
+                    "description": "",
                     "url": f"{link}&referrer=discord",
                     "url": f"{link}&referrer=discord",
                     "color": LEVEL_TO_COLOR["_incident_resolved"],
                     "color": LEVEL_TO_COLOR["_incident_resolved"],
                 }
                 }
@@ -69,7 +71,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
             "embeds": [
             "embeds": [
                 {
                 {
                     "title": title,
                     "title": title,
-                    "description": f"<{link}|*{title}*>  \n",
+                    "description": get_started_at(incident.date_started),
                     "url": f"{link}&referrer=discord",
                     "url": f"{link}&referrer=discord",
                     "color": LEVEL_TO_COLOR["_incident_resolved"],
                     "color": LEVEL_TO_COLOR["_incident_resolved"],
                 }
                 }
@@ -78,9 +80,8 @@ class BuildMetricAlertAttachmentTest(TestCase):
         }
         }
 
 
     def test_metric_alert_with_active_incident(self):
     def test_metric_alert_with_active_incident(self):
-        incident = self.create_incident(
-            alert_rule=self.alert_rule, status=IncidentStatus.CRITICAL.value
-        )
+        new_status = IncidentStatus.CRITICAL.value
+        incident = self.create_incident(alert_rule=self.alert_rule, status=new_status)
         trigger = self.create_alert_rule_trigger(self.alert_rule, CRITICAL_TRIGGER_LABEL, 100)
         trigger = self.create_alert_rule_trigger(self.alert_rule, CRITICAL_TRIGGER_LABEL, 100)
         self.create_alert_rule_trigger_action(
         self.create_alert_rule_trigger_action(
             alert_rule_trigger=trigger, triggered_for_incident=incident
             alert_rule_trigger=trigger, triggered_for_incident=incident
@@ -102,7 +103,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
                 {
                 {
                     "color": LEVEL_TO_COLOR["fatal"],
                     "color": LEVEL_TO_COLOR["fatal"],
                     "title": title,
                     "title": title,
-                    "description": f"<{link}|*{title}*>  \n0 events in the last 10 minutes",
+                    "description": "0 events in the last 10 minutes",
                     "url": f"{link}&referrer=discord",
                     "url": f"{link}&referrer=discord",
                 }
                 }
             ],
             ],
@@ -113,7 +114,6 @@ class BuildMetricAlertAttachmentTest(TestCase):
         incident = self.create_incident(
         incident = self.create_incident(
             alert_rule=self.alert_rule, status=IncidentStatus.CLOSED.value
             alert_rule=self.alert_rule, status=IncidentStatus.CLOSED.value
         )
         )
-
         # This test will use the action/method and not the incident to build status
         # This test will use the action/method and not the incident to build status
         title = f"Critical: {self.alert_rule.name}"
         title = f"Critical: {self.alert_rule.name}"
         metric_value = 5000
         metric_value = 5000
@@ -130,6 +130,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
                 },
                 },
             )
             )
         )
         )
+
         assert DiscordMetricAlertMessageBuilder(
         assert DiscordMetricAlertMessageBuilder(
             self.alert_rule, incident, IncidentStatus.CRITICAL, metric_value=metric_value
             self.alert_rule, incident, IncidentStatus.CRITICAL, metric_value=metric_value
         ).build() == {
         ).build() == {
@@ -138,8 +139,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
                 {
                 {
                     "title": title,
                     "title": title,
                     "color": LEVEL_TO_COLOR["fatal"],
                     "color": LEVEL_TO_COLOR["fatal"],
-                    "description": f"<{link}?alert={incident.identifier}|*{title}*>  \n"
-                    f"{metric_value} events in the last 10 minutes",
+                    "description": f"{metric_value} events in the last 10 minutes{get_started_at(incident.date_started)}",
                     "url": f"{link}?alert={incident.identifier}&referrer=discord",
                     "url": f"{link}?alert={incident.identifier}&referrer=discord",
                 }
                 }
             ],
             ],
@@ -147,6 +147,9 @@ class BuildMetricAlertAttachmentTest(TestCase):
         }
         }
 
 
     def test_metric_alert_chart(self):
     def test_metric_alert_chart(self):
+        incident = self.create_incident(
+            alert_rule=self.alert_rule, status=IncidentStatus.OPEN.value
+        )
         title = f"Resolved: {self.alert_rule.name}"
         title = f"Resolved: {self.alert_rule.name}"
         link = absolute_uri(
         link = absolute_uri(
             reverse(
             reverse(
@@ -157,9 +160,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
                 },
                 },
             )
             )
         )
         )
-        incident = self.create_incident(
-            alert_rule=self.alert_rule, status=IncidentStatus.OPEN.value
-        )
+
         new_status = IncidentStatus.CLOSED
         new_status = IncidentStatus.CLOSED
         assert DiscordMetricAlertMessageBuilder(
         assert DiscordMetricAlertMessageBuilder(
             self.alert_rule, incident, new_status, chart_url="chart_url"
             self.alert_rule, incident, new_status, chart_url="chart_url"
@@ -168,7 +169,7 @@ class BuildMetricAlertAttachmentTest(TestCase):
             "embeds": [
             "embeds": [
                 {
                 {
                     "title": title,
                     "title": title,
-                    "description": f"<{link}?alert={incident.identifier}|*{title}*>  \n",
+                    "description": get_started_at(incident.date_started),
                     "url": f"{link}?alert={incident.identifier}&referrer=discord",
                     "url": f"{link}?alert={incident.identifier}&referrer=discord",
                     "color": 5097329,
                     "color": 5097329,
                     "image": {"url": "chart_url"},
                     "image": {"url": "chart_url"},