|
| 1 | +import logging |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | +from rest_framework.request import Request |
| 5 | +from rest_framework.response import Response |
| 6 | + |
| 7 | +from sentry.api.api_owners import ApiOwner |
| 8 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 9 | +from sentry.api.base import region_silo_endpoint |
| 10 | +from sentry.api.bases.project import ProjectEndpoint, ProjectReleasePermission |
| 11 | +from sentry.preprod.models import PreprodArtifact |
| 12 | +from sentry.types.ratelimit import RateLimit, RateLimitCategory |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class InstallableBuildDetails(BaseModel): |
| 18 | + build_version: str |
| 19 | + build_number: int |
| 20 | + |
| 21 | + |
| 22 | +class CheckForUpdatesApiResponse(BaseModel): |
| 23 | + update: InstallableBuildDetails | None = None |
| 24 | + current: InstallableBuildDetails | None = None |
| 25 | + |
| 26 | + |
| 27 | +@region_silo_endpoint |
| 28 | +class ProjectPreprodArtifactCheckForUpdatesEndpoint(ProjectEndpoint): |
| 29 | + owner = ApiOwner.EMERGE_TOOLS |
| 30 | + publish_status = { |
| 31 | + "GET": ApiPublishStatus.EXPERIMENTAL, |
| 32 | + } |
| 33 | + permission_classes = (ProjectReleasePermission,) |
| 34 | + |
| 35 | + enforce_rate_limit = True |
| 36 | + rate_limits = { |
| 37 | + "GET": { |
| 38 | + RateLimitCategory.ORGANIZATION: RateLimit( |
| 39 | + limit=100, window=60 |
| 40 | + ), # 100 requests per minute per org |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + def get(self, request: Request, project) -> Response: |
| 45 | + """ |
| 46 | + Check for updates for a preprod artifact |
| 47 | + """ |
| 48 | + main_binary_identifier = request.GET.get("main_binary_identifier") |
| 49 | + if not main_binary_identifier: |
| 50 | + # Not implemented yet |
| 51 | + return Response(CheckForUpdatesApiResponse().dict()) |
| 52 | + |
| 53 | + try: |
| 54 | + preprod_artifact = PreprodArtifact.objects.filter( |
| 55 | + project=project, main_binary_identifier=main_binary_identifier |
| 56 | + ).latest("date_added") |
| 57 | + except PreprodArtifact.DoesNotExist: |
| 58 | + return Response({"error": "Not found"}, status=404) |
| 59 | + |
| 60 | + if preprod_artifact.build_version and preprod_artifact.build_number: |
| 61 | + current = InstallableBuildDetails( |
| 62 | + build_version=preprod_artifact.build_version, |
| 63 | + build_number=preprod_artifact.build_number, |
| 64 | + ) |
| 65 | + else: |
| 66 | + current = None |
| 67 | + return Response(CheckForUpdatesApiResponse(current=current).dict()) |
0 commit comments