Skip to content

Commit

Permalink
feat(releases): Implement threshold details GET
Browse files Browse the repository at this point in the history
  • Loading branch information
EricHasegawa committed Sep 14, 2023
1 parent 41e8adc commit 18e4043
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/sentry/api/endpoints/release_threshold_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.http import HttpResponse
from rest_framework import serializers
from rest_framework.request import Request
from rest_framework.response import Response

from sentry.api.api_owners import ApiOwner
from sentry.api.api_publish_status import ApiPublishStatus
from sentry.api.base import region_silo_endpoint
from sentry.api.bases.project import ProjectEndpoint
from sentry.api.serializers import serialize
from sentry.api.serializers.rest_framework.project import ProjectField
from sentry.models import Project
from sentry.models.release_threshold.releasethreshold import ReleaseThreshold


class ReleaseThresholdDetailsGETSerializer(serializers.Serializer):
threshold_id = serializers.CharField()
project = ProjectField()


@region_silo_endpoint
class ReleaseThresholdDetailsEndpoint(ProjectEndpoint):
owner: ApiOwner = ApiOwner.ENTERPRISE
publish_status = {
"GET": ApiPublishStatus.PRIVATE,
}

def get(self, request: Request, project: Project, threshold_id: str) -> HttpResponse:
request.data["project"] = project.slug
request.data["threshold_id"] = threshold_id
serializer = ReleaseThresholdDetailsGETSerializer(
data=request.data,
context={
"organization": project.organization,
"access": request.access,
},
)
if not serializer.is_valid():
return Response(serializer.errors, status=400)
result = serializer.validated_data
try:
release_threshold = ReleaseThreshold.objects.get(
id=result.get("threshold_id"),
project=project,
)
return Response(serialize(release_threshold, request.user), status=200)
except ReleaseThreshold.DoesNotExist:
return Response(status=404)
1 change: 1 addition & 0 deletions src/sentry/api/serializers/models/release_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class ReleaseThresholdSerializer(Serializer):
def serialize(self, obj, attrs, user):
return {
"id": str(obj.id),
"threshold_type": THRESHOLD_TYPE_INT_TO_STR[obj.threshold_type],
"trigger_type": TRIGGER_TYPE_INT_TO_STR[obj.trigger_type],
"value": obj.value,
Expand Down
6 changes: 6 additions & 0 deletions src/sentry/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sentry.api.endpoints.source_map_debug_blue_thunder_edition import (
SourceMapDebugBlueThunderEditionEndpoint,
)
from sentry.api.endpoints.release_threshold_details import ReleaseThresholdDetailsEndpoint
from sentry.api.utils import method_dispatch
from sentry.data_export.endpoints.data_export import DataExportEndpoint
from sentry.data_export.endpoints.data_export_details import DataExportDetailsEndpoint
Expand Down Expand Up @@ -2129,6 +2130,11 @@
ReleaseThresholdEndpoint.as_view(),
name="sentry-api-0-project-release-thresholds",
),
re_path(
r"^(?P<organization_slug>[^\/]+)/(?P<project_slug>[^\/]+)/releases/thresholds/(?P<threshold_id>[^/]+)/$",
ReleaseThresholdDetailsEndpoint.as_view(),
name="sentry-api-0-project-release-thresholds-details",
),
re_path(
r"^(?P<organization_slug>[^\/]+)/(?P<project_slug>[^\/]+)/commits/$",
ProjectCommitsEndpoint.as_view(),
Expand Down
73 changes: 73 additions & 0 deletions tests/sentry/api/endpoints/test_release_threshold_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from django.urls import reverse

from sentry.models.environment import Environment
from sentry.models.release_threshold.releasethreshold import ReleaseThreshold
from sentry.testutils.cases import APITestCase


class ReleaseThresholdDetailsTest(APITestCase):
def setUp(self):
super().setUp()
self.user = self.create_user(is_staff=True, is_superuser=True)

self.canary_environment = Environment.objects.create(
organization_id=self.organization.id, name="canary"
)
self.production_environment = Environment.objects.create(
organization_id=self.organization.id, name="production"
)
self.login_as(user=self.user)

self.basic_threshold = ReleaseThreshold.objects.create(
threshold_type=0,
trigger_type=0,
value=100,
window_in_seconds=1800,
project=self.project,
environment=self.canary_environment,
)

def test_invalid_threshold_id(self):
url = reverse(
"sentry-api-0-project-release-thresholds-details",
kwargs={
"organization_slug": self.organization.slug,
"project_slug": self.project.slug,
"threshold_id": 123,
},
)
response = self.client.get(url)

assert response.status_code == 404

def test_invalid_project(self):
url = reverse(
"sentry-api-0-project-release-thresholds-details",
kwargs={
"organization_slug": self.organization.slug,
"project_slug": "kingdom_of_the_crystal_skull",
"threshold_id": self.basic_threshold.id,
},
)
response = self.client.get(url)

assert response.status_code == 404

def test_valid(self):
url = reverse(
"sentry-api-0-project-release-thresholds-details",
kwargs={
"organization_slug": self.organization.slug,
"project_slug": self.project.slug,
"threshold_id": self.basic_threshold.id,
},
)
response = self.client.get(url)

assert response.status_code == 200
assert response.data["id"] == str(self.basic_threshold.id)
assert response.data["threshold_type"] == "total_error_count"
assert response.data["trigger_type"] == "percent_over"
assert response.data["value"] == 100
assert response.data["window_in_seconds"] == 1800
assert response.data["environment"]["name"] == "canary"

0 comments on commit 18e4043

Please sign in to comment.