Browse Source

ref(update-groups): Move mark_reviewed to separate module (#47498)

[Part of a larger effort to
[refactor](https://getsentry.atlassian.net/browse/WOR-2944)
update_groups]

This PR rehomes the logic to mark an issue as reviewed, moving it in/out
of the inbox.

WOR-2979

---------

Co-authored-by: Katie Byers <katie.byers@sentry.io>
Snigdha Sharma 1 year ago
parent
commit
af49b367e0

+ 11 - 22
src/sentry/api/helpers/group_index/update.py

@@ -19,6 +19,7 @@ from sentry.issues.grouptype import GroupCategory
 from sentry.issues.ignored import handle_archived_until_escalating, handle_ignored
 from sentry.issues.ignored import handle_archived_until_escalating, handle_ignored
 from sentry.issues.merge import handle_merge
 from sentry.issues.merge import handle_merge
 from sentry.issues.status_change import handle_status_update
 from sentry.issues.status_change import handle_status_update
+from sentry.issues.update_inbox import update_inbox
 from sentry.models import (
 from sentry.models import (
     TOMBSTONE_FIELDS_FROM_GROUP,
     TOMBSTONE_FIELDS_FROM_GROUP,
     Activity,
     Activity,
@@ -27,7 +28,6 @@ from sentry.models import (
     GroupAssignee,
     GroupAssignee,
     GroupBookmark,
     GroupBookmark,
     GroupHash,
     GroupHash,
-    GroupInboxReason,
     GroupLink,
     GroupLink,
     GroupRelease,
     GroupRelease,
     GroupResolution,
     GroupResolution,
@@ -47,11 +47,11 @@ from sentry.models import (
 from sentry.models.activity import ActivityIntegration
 from sentry.models.activity import ActivityIntegration
 from sentry.models.group import STATUS_UPDATE_CHOICES
 from sentry.models.group import STATUS_UPDATE_CHOICES
 from sentry.models.grouphistory import record_group_history_from_activity_type
 from sentry.models.grouphistory import record_group_history_from_activity_type
-from sentry.models.groupinbox import GroupInboxRemoveAction, add_group_to_inbox
+from sentry.models.groupinbox import GroupInboxRemoveAction
 from sentry.notifications.types import SUBSCRIPTION_REASON_MAP, GroupSubscriptionReason
 from sentry.notifications.types import SUBSCRIPTION_REASON_MAP, GroupSubscriptionReason
 from sentry.services.hybrid_cloud import coerce_id_from
 from sentry.services.hybrid_cloud import coerce_id_from
 from sentry.services.hybrid_cloud.user import RpcUser, user_service
 from sentry.services.hybrid_cloud.user import RpcUser, user_service
-from sentry.signals import issue_mark_reviewed, issue_resolved
+from sentry.signals import issue_resolved
 from sentry.tasks.integrations import kick_off_status_syncs
 from sentry.tasks.integrations import kick_off_status_syncs
 from sentry.types.activity import ActivityType
 from sentry.types.activity import ActivityType
 from sentry.types.group import SUBSTATUS_UPDATE_CHOICES, GroupSubStatus
 from sentry.types.group import SUBSTATUS_UPDATE_CHOICES, GroupSubStatus
@@ -656,27 +656,16 @@ def update_groups(
 
 
         result["merge"] = handle_merge(group_list, project_lookup, acting_user)
         result["merge"] = handle_merge(group_list, project_lookup, acting_user)
 
 
-    # Support moving groups in or out of the inbox
     inbox = result.get("inbox", None)
     inbox = result.get("inbox", None)
     if inbox is not None:
     if inbox is not None:
-        if inbox:
-            for group in group_list:
-                add_group_to_inbox(group, GroupInboxReason.MANUAL)
-        elif not inbox:
-            for group in group_list:
-                remove_group_from_inbox(
-                    group,
-                    action=GroupInboxRemoveAction.MARK_REVIEWED,
-                    user=acting_user,
-                    referrer=request.META.get("HTTP_REFERER"),
-                )
-                issue_mark_reviewed.send_robust(
-                    project=project_lookup[group.project_id],
-                    user=acting_user,
-                    group=group,
-                    sender=update_groups,
-                )
-        result["inbox"] = inbox
+        result["inbox"] = update_inbox(
+            inbox,
+            group_list,
+            project_lookup,
+            acting_user,
+            http_referrer=request.META.get("HTTP_REFERER"),
+            sender=update_groups,
+        )
 
 
     return Response(result)
     return Response(result)
 
 

+ 45 - 0
src/sentry/issues/update_inbox.py

@@ -0,0 +1,45 @@
+from __future__ import annotations
+
+from typing import Any, Dict, List
+
+from sentry.models import Group, Project, User
+from sentry.models.groupinbox import (
+    GroupInboxReason,
+    GroupInboxRemoveAction,
+    add_group_to_inbox,
+    remove_group_from_inbox,
+)
+from sentry.signals import issue_mark_reviewed
+
+
+def update_inbox(
+    in_inbox: bool,
+    group_list: List[Group],
+    project_lookup: Dict[int, Project],
+    acting_user: User | None,
+    http_referrer: str,
+    sender: Any,
+) -> bool:
+    """
+    Support moving groups in or out of the inbox via the Mark Reviewed button.
+
+    Returns a boolean indicating whether or not the groups are now in the inbox.
+    """
+    if in_inbox:
+        for group in group_list:
+            add_group_to_inbox(group, GroupInboxReason.MANUAL)
+    elif not in_inbox:
+        for group in group_list:
+            remove_group_from_inbox(
+                group,
+                action=GroupInboxRemoveAction.MARK_REVIEWED,
+                user=acting_user,
+                referrer=http_referrer,
+            )
+            issue_mark_reviewed.send_robust(
+                project=project_lookup[group.project_id],
+                user=acting_user,
+                group=group,
+                sender=sender,
+            )
+    return in_inbox

+ 38 - 0
tests/sentry/issues/test_update_inbox.py

@@ -0,0 +1,38 @@
+from sentry.issues.update_inbox import update_inbox
+from sentry.models import GroupInbox, GroupInboxReason, add_group_to_inbox
+from sentry.testutils import TestCase
+
+
+class MarkReviewedTest(TestCase):  # type: ignore
+    def setUp(self) -> None:
+        super().setUp()
+        self.group = self.create_group()
+        self.group_list = [self.group]
+        self.group_ids = [self.group]
+        self.project_lookup = {self.group.project_id: self.group.project}
+        add_group_to_inbox(self.group, GroupInboxReason.NEW)
+
+    def test_mark_reviewed(self) -> None:
+        update_inbox(
+            in_inbox=False,
+            group_list=self.group_list,
+            project_lookup=self.project_lookup,
+            acting_user=self.user,
+            http_referrer="",
+            sender=self,
+        )
+        assert not GroupInbox.objects.filter(group=self.group).exists()
+
+    def test_add_to_inbox(self) -> None:
+        new_group = self.create_group()
+        _ = update_inbox(
+            in_inbox=True,
+            group_list=self.group_list + [new_group],
+            project_lookup=self.project_lookup,
+            acting_user=self.user,
+            http_referrer="",
+            sender=self,
+        )
+
+        assert GroupInbox.objects.filter(group=self.group).exists()
+        assert GroupInbox.objects.filter(group=new_group).exists()