Skip to content

Commit ec00641

Browse files
authored
feat(api): Allow fetching team details without projects (#13935)
This allows you to fetch team details that do not include projects, which improves performance.
1 parent 1b2f1d5 commit ec00641

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/sentry/api/endpoints/organization_teams.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission
1313
from sentry.api.paginator import OffsetPaginator
1414
from sentry.api.serializers import serialize
15-
from sentry.api.serializers.models.team import TeamWithProjectsSerializer
15+
from sentry.api.serializers.models import team as team_serializers
1616
from sentry.models import (
1717
AuditLogEntryEvent, OrganizationMember, OrganizationMemberTeam, Team, TeamStatus
1818
)
@@ -87,6 +87,7 @@ def get(self, request, organization):
8787
8888
:pparam string organization_slug: the slug of the organization for
8989
which the teams should be listed.
90+
:param string detailed: Specify "0" to return team details that do not include projects
9091
:auth: required
9192
"""
9293
# TODO(dcramer): this should be system-wide default for organization
@@ -100,6 +101,7 @@ def get(self, request, organization):
100101
).order_by('slug')
101102

102103
query = request.GET.get('query')
104+
103105
if query:
104106
tokens = tokenize_query(query)
105107
for key, value in six.iteritems(tokens):
@@ -109,11 +111,14 @@ def get(self, request, organization):
109111
else:
110112
queryset = queryset.none()
111113

114+
is_detailed = request.GET.get('detailed', '1') != '0'
115+
serializer = team_serializers.TeamWithProjectsSerializer if is_detailed else team_serializers.TeamSerializer
116+
112117
return self.paginate(
113118
request=request,
114119
queryset=queryset,
115120
order_by='slug',
116-
on_results=lambda x: serialize(x, request.user, TeamWithProjectsSerializer()),
121+
on_results=lambda x: serialize(x, request.user, serializer()),
117122
paginator_cls=OffsetPaginator,
118123
)
119124

tests/sentry/api/endpoints/test_organization_teams.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ def test_simple(self):
3636
assert response.data[1]['id'] == six.text_type(team1.id)
3737
assert response.data[1]['isMember']
3838

39+
def test_simple_results_no_projects(self):
40+
user = self.create_user()
41+
org = self.create_organization(owner=self.user)
42+
team1 = self.create_team(organization=org, name='foo')
43+
self.create_team(organization=org, name='bar')
44+
45+
self.create_member(
46+
organization=org,
47+
user=user,
48+
has_global_access=False,
49+
teams=[team1],
50+
)
51+
52+
path = u'/api/0/organizations/{}/teams/?detailed=0'.format(org.slug)
53+
54+
self.login_as(user=user)
55+
56+
response = self.client.get(path)
57+
58+
assert response.status_code == 200, response.content
59+
assert len(response.data) == 2
60+
assert 'projects' not in response.data[0]
61+
assert 'projects' not in response.data[1]
62+
3963
def test_search(self):
4064
user = self.create_user()
4165
org = self.create_organization(owner=self.user)

0 commit comments

Comments
 (0)