Repository: getsentry/sentry

Issue:
Unfurled issues in Slack are always error red instead of matching level
## Important Details
How are you running Sentry?
Saas (sentry.io)

## Description
When a warning level issue is pasted in Slack, the color of the attachment is red.

## Steps to Reproduce
1. Set up the Slack integration.
2. Trigger a warning in your application.
3. Copy and paste the issue URL into Slack
4. Observe the unfurled URL has a red bar.

![image](https://user-images.githubusercontent.com/549473/97138752-2147b900-1716-11eb-89bb-cdb2bad2efcf.png)

### What you expected to happen
The unfurled attachment should have had a yellow bar.

### Possible Solution
The logic in src/sentry/integrations/slack/utils.py incorrectly falls back to assuming
error level instead of using the last event like it uses for all other tags.



Discussion:

Resolving patch:
diff --git a/src/sentry/integrations/slack/utils.py b/src/sentry/integrations/slack/utils.py
--- a/src/sentry/integrations/slack/utils.py
+++ b/src/sentry/integrations/slack/utils.py
@@ -197,10 +197,6 @@ def build_group_attachment(group, event=None, tags=None, identity=None, actions=
     teams = get_team_assignees(group)
 
     logo_url = absolute_uri(get_asset_url("sentry", "images/sentry-email-avatar.png"))
-    color = (
-        LEVEL_TO_COLOR.get(event.get_tag("level"), "error") if event else LEVEL_TO_COLOR["error"]
-    )
-
     text = build_attachment_text(group, event) or ""
 
     if actions is None:
@@ -257,11 +253,19 @@ def build_group_attachment(group, event=None, tags=None, identity=None, actions=
         },
     ]
 
-    fields = []
+    # If an event is unspecified, use the tags of the latest event (if one exists).
+    event_for_tags = event if event else group.get_latest_event()
 
-    if tags:
-        event_tags = event.tags if event else group.get_latest_event().tags
+    fallback_color = LEVEL_TO_COLOR["error"]
+    color = (
+        LEVEL_TO_COLOR.get(event_for_tags.get_tag("level"), fallback_color)
+        if event_for_tags
+        else fallback_color
+    )
 
+    fields = []
+    if tags:
+        event_tags = event_for_tags.tags if event_for_tags else []
         for key, value in event_tags:
             std_key = tagstore.get_standardized_key(key)
             if std_key not in tags:


Test patch:
diff --git a/tests/sentry/integrations/slack/test_utils.py b/tests/sentry/integrations/slack/test_utils.py
--- a/tests/sentry/integrations/slack/test_utils.py
+++ b/tests/sentry/integrations/slack/test_utils.py
@@ -283,3 +283,18 @@ def test_build_group_attachment(self):
             "fallback": u"[{}] {}".format(self.project.slug, event.title),
             "footer_icon": u"http://testserver/_static/{version}/sentry/images/sentry-email-avatar.png",
         }
+
+    def test_build_group_attachment_color_no_event_error_fallback(self):
+        group_with_no_events = self.create_group(project=self.project)
+        assert build_group_attachment(group_with_no_events)["color"] == "#E03E2F"
+
+    def test_build_group_attachment_color_unxpected_level_error_fallback(self):
+        unexpected_level_event = self.store_event(
+            data={"level": "trace"}, project_id=self.project.id, assert_no_errors=False
+        )
+        assert build_group_attachment(unexpected_level_event.group)["color"] == "#E03E2F"
+
+    def test_build_group_attachment_color_warning(self):
+        warning_event = self.store_event(data={"level": "warning"}, project_id=self.project.id)
+        assert build_group_attachment(warning_event.group)["color"] == "#FFC227"
+        assert build_group_attachment(warning_event.group, warning_event)["color"] == "#FFC227"