Skip to content

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

src/sentry/api/endpoints/organization_environments.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import TypedDict
2-
31
from drf_spectacular.utils import extend_schema
42
from rest_framework.request import Request
53
from rest_framework.response import Response
@@ -10,18 +8,14 @@
108
from sentry.api.bases import OrganizationEndpoint
119
from sentry.api.helpers.environments import environment_visibility_filter_options
1210
from sentry.api.serializers import serialize
11+
from sentry.api.serializers.models.environment import EnvironmentSerializerResponse
1312
from sentry.apidocs.constants import RESPONSE_BAD_REQUEST, RESPONSE_FORBIDDEN
1413
from sentry.apidocs.examples.environment_examples import EnvironmentExamples
1514
from sentry.apidocs.parameters import EnvironmentParams, GlobalParams
1615
from sentry.apidocs.utils import inline_sentry_response_serializer
1716
from sentry.models.environment import Environment, EnvironmentProject
1817

1918

20-
class OrganizationEnvironmentResponseType(TypedDict):
21-
id: int
22-
name: str
23-
24-
2519
@extend_schema(tags=["Environments"])
2620
@region_silo_endpoint
2721
class OrganizationEnvironmentsEndpoint(OrganizationEndpoint):
@@ -35,7 +29,7 @@ class OrganizationEnvironmentsEndpoint(OrganizationEndpoint):
3529
parameters=[GlobalParams.ORG_ID_OR_SLUG, EnvironmentParams.VISIBILITY],
3630
responses={
3731
200: inline_sentry_response_serializer(
38-
"OrganizationEnvironmentResponse", list[OrganizationEnvironmentResponseType]
32+
"OrganizationEnvironmentResponse", list[EnvironmentSerializerResponse]
3933
),
4034
400: RESPONSE_BAD_REQUEST,
4135
403: RESPONSE_FORBIDDEN,

src/sentry/api/endpoints/project_environments.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from drf_spectacular.utils import OpenApiResponse, extend_schema
12
from rest_framework.request import Request
23
from rest_framework.response import Response
34

@@ -6,33 +7,42 @@
67
from sentry.api.bases.project import ProjectEndpoint
78
from sentry.api.helpers.environments import environment_visibility_filter_options
89
from sentry.api.serializers import serialize
10+
from sentry.api.serializers.models.environment import EnvironmentProjectSerializerResponse
11+
from sentry.apidocs.constants import RESPONSE_FORBIDDEN, RESPONSE_NOT_FOUND, RESPONSE_UNAUTHORIZED
12+
from sentry.apidocs.examples.environment_examples import EnvironmentExamples
13+
from sentry.apidocs.parameters import EnvironmentParams, GlobalParams
14+
from sentry.apidocs.utils import inline_sentry_response_serializer
915
from sentry.models.environment import EnvironmentProject
1016

1117

18+
@extend_schema(tags=["Environments"])
1219
@region_silo_endpoint
1320
class ProjectEnvironmentsEndpoint(ProjectEndpoint):
1421
publish_status = {
15-
"GET": ApiPublishStatus.UNKNOWN,
22+
"GET": ApiPublishStatus.PUBLIC,
1623
}
1724

25+
@extend_schema(
26+
operation_id="List a Project's Environments",
27+
parameters=[
28+
GlobalParams.ORG_ID_OR_SLUG,
29+
GlobalParams.PROJECT_ID_OR_SLUG,
30+
EnvironmentParams.VISIBILITY,
31+
],
32+
responses={
33+
200: inline_sentry_response_serializer(
34+
"ListProjectEnvironments", list[EnvironmentProjectSerializerResponse]
35+
),
36+
400: OpenApiResponse(description="Invalid value for 'visibility'."),
37+
401: RESPONSE_UNAUTHORIZED,
38+
403: RESPONSE_FORBIDDEN,
39+
404: RESPONSE_NOT_FOUND,
40+
},
41+
examples=EnvironmentExamples.GET_PROJECT_ENVIRONMENTS,
42+
)
1843
def get(self, request: Request, project) -> Response:
1944
"""
20-
List a Project's Environments
21-
```````````````````````````````
22-
23-
Return environments for a given project.
24-
25-
:qparam string visibility: when omitted only visible environments are
26-
returned. Set to ``"hidden"`` for only hidden
27-
environments, or ``"all"`` for both hidden
28-
and visible environments.
29-
30-
:pparam string organization_id_or_slug: the id or slug of the organization the project
31-
belongs to.
32-
33-
:pparam string project_id_or_slug: the id or slug of the project.
34-
35-
:auth: required
45+
Lists a project's environments.
3646
"""
3747

3848
queryset = (

src/sentry/api/serializers/models/environment.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import namedtuple
22
from datetime import timedelta
3+
from typing import TypedDict
34

45
from django.utils import timezone
56

@@ -11,15 +12,28 @@
1112
StatsPeriod = namedtuple("StatsPeriod", ("segments", "interval"))
1213

1314

15+
class EnvironmentSerializerResponse(TypedDict):
16+
id: str
17+
name: str
18+
19+
20+
class EnvironmentProjectSerializerResponse(TypedDict):
21+
id: str
22+
name: str
23+
isHidden: bool
24+
25+
1426
@register(Environment)
1527
class EnvironmentSerializer(Serializer):
16-
def serialize(self, obj, attrs, user, **kwargs):
28+
def serialize(self, obj: Environment, attrs, user, **kwargs) -> EnvironmentSerializerResponse:
1729
return {"id": str(obj.id), "name": obj.name}
1830

1931

2032
@register(EnvironmentProject)
2133
class EnvironmentProjectSerializer(Serializer):
22-
def serialize(self, obj, attrs, user, **kwargs):
34+
def serialize(
35+
self, obj: EnvironmentProject, attrs, user, **kwargs
36+
) -> EnvironmentProjectSerializerResponse:
2337
return {
2438
"id": str(obj.id),
2539
"name": obj.environment.name,

src/sentry/apidocs/examples/environment_examples.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ class EnvironmentExamples:
77
"List an Organization's Environments",
88
value=[
99
{
10-
"id": 1,
10+
"id": "1",
1111
"name": "Production",
1212
},
1313
{
14-
"id": 2,
14+
"id": "2",
1515
"name": "Staging",
1616
},
1717
],
1818
status_codes=["200"],
1919
response_only=True,
2020
)
2121
]
22+
23+
GET_PROJECT_ENVIRONMENTS = [
24+
OpenApiExample(
25+
"List a Project's Environments",
26+
value=[
27+
{"id": "1", "name": "Production", "isHidden": False},
28+
{"id": "2", "name": "Staging", "isHidden": True},
29+
],
30+
status_codes=["200"],
31+
response_only=True,
32+
)
33+
]

src/sentry/apidocs/parameters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class EnvironmentParams:
9595
location="query",
9696
required=False,
9797
type=str,
98-
description="""The visibility of the environments to filter by. The options are: `all`, `hidden`, `visible`. Defaults to `visible`.""",
98+
description="""The visibility of the environments to filter by. Defaults to `visible`.""",
99+
enum=["all", "hidden", "visible"],
99100
)
100101

101102

0 commit comments

Comments
 (0)