Browse Source

fix(workflow): Removing groups assigned to others from owner:me and owner:me_or_none search (#22910)

Chris Fuller 4 years ago
parent
commit
edf47b6a62

+ 20 - 27
src/sentry/search/snuba/backend.py

@@ -147,7 +147,12 @@ def owner_filter(owner, projects):
             .values_list("group_id", flat=True)
             .distinct()
         ) | assigned_to_filter(owner, projects)
-    elif isinstance(owner, User):
+    elif isinstance(owner, User) or (isinstance(owner, list) and owner[0] == "me_or_none"):
+        include_none = False
+        if isinstance(owner, list) and owner[0] == "me_or_none":
+            include_none = True
+            owner = owner[1]
+
         teams = Team.objects.filter(
             id__in=OrganizationMemberTeam.objects.filter(
                 organizationmember__in=OrganizationMember.objects.filter(
@@ -156,40 +161,28 @@ def owner_filter(owner, projects):
                 is_active=True,
             ).values("team")
         )
-        relevant_owners = GroupOwner.objects.filter(
-            project_id__in=project_ids, organization_id=organization_id
-        )
-        filter_query = Q(team__in=teams) | Q(user=owner)
-        return Q(
-            id__in=relevant_owners.filter(filter_query)
-            .values_list("group_id", flat=True)
-            .distinct()
-        ) | assigned_to_filter(owner, projects)
-    elif isinstance(owner, list) and owner[0] == "me_or_none":
-        teams = Team.objects.filter(
-            id__in=OrganizationMemberTeam.objects.filter(
-                organizationmember__in=OrganizationMember.objects.filter(
-                    user=owner[1], organization_id=organization_id
-                ),
-                is_active=True,
-            ).values("team")
-        )
-
-        owned_me = Q(
+        owned_by_me = Q(
             id__in=GroupOwner.objects.filter(
-                Q(user_id=owner[1].id) | Q(team__in=teams),
+                Q(user_id=owner.id) | Q(team__in=teams),
+                Q(group__assignee_set__isnull=True),
                 project_id__in=[p.id for p in projects],
                 organization_id=organization_id,
             )
             .values_list("group_id", flat=True)
             .distinct()
         )
-        no_owner = unassigned_filter(True, projects) & ~Q(
-            id__in=GroupOwner.objects.filter(project_id__in=[p.id for p in projects]).values_list(
-                "group_id", flat=True
+
+        owner_query = owned_by_me | assigned_to_filter(owner, projects)
+
+        if include_none:
+            no_owner = unassigned_filter(True, projects) & ~Q(
+                id__in=GroupOwner.objects.filter(
+                    project_id__in=[p.id for p in projects]
+                ).values_list("group_id", flat=True)
             )
-        )
-        return no_owner | owned_me | assigned_to_filter(owner[1], projects)
+            return no_owner | owner_query
+        else:
+            return owner_query
 
     raise InvalidSearchQuery(u"Unsupported owner type.")
 

+ 35 - 6
tests/snuba/api/endpoints/test_organization_group_index.py

@@ -720,6 +720,7 @@ class GroupListTest(APITestCase, SnubaTestCase):
                 },
                 project_id=self.project.id,
             )
+
             assigned_event = self.store_event(
                 data={
                     "timestamp": iso_format(before_now(seconds=195)),
@@ -728,11 +729,27 @@ class GroupListTest(APITestCase, SnubaTestCase):
                 project_id=self.project.id,
             )
 
+            assigned_to_other_event = self.store_event(
+                data={
+                    "timestamp": iso_format(before_now(seconds=195)),
+                    "fingerprint": ["group-5"],
+                },
+                project_id=self.project.id,
+            )
+
             self.login_as(user=self.user)
             response = self.get_response(sort_by="date", limit=10, query="owner:me")
             assert response.status_code == 200
             assert len(response.data) == 0
 
+            GroupOwner.objects.create(
+                group=assigned_to_other_event.group,
+                project=assigned_to_other_event.group.project,
+                organization=assigned_to_other_event.group.project.organization,
+                type=0,
+                team_id=None,
+                user_id=self.user.id,
+            )
             GroupOwner.objects.create(
                 group=event.group,
                 project=event.group.project,
@@ -744,19 +761,31 @@ class GroupListTest(APITestCase, SnubaTestCase):
 
             response = self.get_response(sort_by="date", limit=10, query="owner:me")
             assert response.status_code == 200
-            assert len(response.data) == 1
+            assert len(response.data) == 2
             assert int(response.data[0]["id"]) == event.group.id
-
+            assert int(response.data[1]["id"]) == assigned_to_other_event.group.id
+            # Because assigned_to_other_event is assigned to self.other_user, it should not show up in owner search for anyone but self.other_user. (aka. they are now the only owner)
+            other_user = self.create_user("other@user.com", is_superuser=False)
             GroupAssignee.objects.create(
-                group=assigned_event.group, project=assigned_event.group.project, user=self.user
+                group=assigned_to_other_event.group,
+                project=assigned_to_other_event.group.project,
+                user=other_user,
             )
-
             response = self.get_response(sort_by="date", limit=10, query="owner:me")
             assert response.status_code == 200
-            assert len(response.data) == 2
+            assert len(response.data) == 1
             assert int(response.data[0]["id"]) == event.group.id
-            assert int(response.data[1]["id"]) == assigned_event.group.id
 
+            response = self.get_response(
+                sort_by="date", limit=10, query="owner:{}".format(other_user.email)
+            )
+            assert response.status_code == 200
+            assert len(response.data) == 1
+            assert int(response.data[0]["id"]) == assigned_to_other_event.group.id
+
+            GroupAssignee.objects.create(
+                group=assigned_event.group, project=assigned_event.group.project, user=self.user
+            )
             response = self.get_response(
                 sort_by="date", limit=10, query="owner:{}".format(self.user.email)
             )