Repository: getsentry/sentry

Issue:
Add filter by assigned teams
I can assignee issue to team but i can't filter issues by assigned teams


Discussion:

Resolving patch:
diff --git a/src/sentry/search/django/backend.py b/src/sentry/search/django/backend.py
--- a/src/sentry/search/django/backend.py
+++ b/src/sentry/search/django/backend.py
@@ -164,13 +164,16 @@ def get_sort_clause(sort_by):
         return SORT_CLAUSES[sort_by]
 
 
-def assigned_to_filter(queryset, user, project):
+def assigned_to_filter(queryset, actor, project):
     from sentry.models import OrganizationMember, OrganizationMemberTeam, Team
 
+    if isinstance(actor, Team):
+        return queryset.filter(assignee_set__team=actor)
+
     teams = Team.objects.filter(
         id__in=OrganizationMemberTeam.objects.filter(
             organizationmember__in=OrganizationMember.objects.filter(
-                user=user,
+                user=actor,
                 organization_id=project.organization_id,
             ),
             is_active=True,
@@ -178,7 +181,7 @@ def assigned_to_filter(queryset, user, project):
     )
 
     return queryset.filter(
-        Q(assignee_set__user=user, assignee_set__project=project) |
+        Q(assignee_set__user=actor, assignee_set__project=project) |
         Q(assignee_set__team__in=teams)
     )
 
diff --git a/src/sentry/search/utils.py b/src/sentry/search/utils.py
--- a/src/sentry/search/utils.py
+++ b/src/sentry/search/utils.py
@@ -8,7 +8,7 @@
 from django.utils import timezone
 
 from sentry.constants import STATUS_CHOICES
-from sentry.models import EventUser, User
+from sentry.models import EventUser, Team, User
 from sentry.search.base import ANY
 from sentry.utils.auth import find_users
 
@@ -121,6 +121,19 @@ def parse_datetime_expression(value):
     return parse_datetime_value(value)
 
 
+def parse_team_value(project, value, user):
+    return Team.objects.filter(
+        slug__iexact=value[1:],
+        projectteam__project=project,
+    ).first() or Team(id=0)
+
+
+def parse_actor_value(project, value, user):
+    if value.startswith('#'):
+        return parse_team_value(project, value, user)
+    return parse_user_value(value, user)
+
+
 def parse_user_value(value, user):
     if value == 'me':
         return user
@@ -342,7 +355,7 @@ def parse_query(project, query, user):
                     except KeyError:
                         raise InvalidQuery(u"'is:' had unknown status code '{}'.".format(value))
             elif key == 'assigned':
-                results['assigned_to'] = parse_user_value(value, user)
+                results['assigned_to'] = parse_actor_value(project, value, user)
             elif key == 'bookmarks':
                 results['bookmarked_by'] = parse_user_value(value, user)
             elif key == 'subscribed':


Test patch:
diff --git a/tests/sentry/search/test_utils.py b/tests/sentry/search/test_utils.py
--- a/tests/sentry/search/test_utils.py
+++ b/tests/sentry/search/test_utils.py
@@ -5,7 +5,7 @@
 from datetime import datetime, timedelta
 from django.utils import timezone
 
-from sentry.models import EventUser, GroupStatus
+from sentry.models import EventUser, GroupStatus, Team, User
 from sentry.testutils import TestCase
 from sentry.search.base import ANY
 from sentry.search.utils import parse_query, get_numeric_field_value
@@ -269,6 +269,22 @@ def test_assigned_email(self):
 
     def test_assigned_unknown_user(self):
         result = self.parse_query('assigned:fake@example.com')
+        assert isinstance(result['assigned_to'], User)
+        assert result['assigned_to'].id == 0
+
+    def test_assigned_valid_team(self):
+        result = self.parse_query('assigned:#{}'.format(self.team.slug))
+        assert result['assigned_to'] == self.team
+
+    def test_assigned_unassociated_team(self):
+        team2 = self.create_team(organization=self.organization)
+        result = self.parse_query('assigned:#{}'.format(team2.slug))
+        assert isinstance(result['assigned_to'], Team)
+        assert result['assigned_to'].id == 0
+
+    def test_assigned_invalid_team(self):
+        result = self.parse_query('assigned:#invalid')
+        assert isinstance(result['assigned_to'], Team)
         assert result['assigned_to'].id == 0
 
     def test_bookmarks_me(self):