|
1 | 1 | import logging
|
2 | 2 |
|
| 3 | +from packaging.version import parse as parse_version |
3 | 4 | from pydantic import BaseModel
|
4 | 5 | from rest_framework.request import Request
|
5 | 6 | from rest_framework.response import Response
|
|
8 | 9 | from sentry.api.api_publish_status import ApiPublishStatus
|
9 | 10 | from sentry.api.base import region_silo_endpoint
|
10 | 11 | from sentry.api.bases.project import ProjectEndpoint, ProjectReleasePermission
|
11 |
| -from sentry.preprod.models import PreprodArtifact |
| 12 | +from sentry.preprod.build_distribution_utils import ( |
| 13 | + get_download_url_for_artifact, |
| 14 | + is_installable_artifact, |
| 15 | +) |
| 16 | +from sentry.preprod.models import PreprodArtifact, PreprodBuildConfiguration |
12 | 17 | from sentry.types.ratelimit import RateLimit, RateLimitCategory
|
13 | 18 |
|
14 | 19 | logger = logging.getLogger(__name__)
|
15 | 20 |
|
16 | 21 |
|
17 | 22 | class InstallableBuildDetails(BaseModel):
|
| 23 | + id: str |
18 | 24 | build_version: str
|
19 | 25 | build_number: int
|
| 26 | + download_url: str |
| 27 | + app_name: str |
| 28 | + created_date: str |
20 | 29 |
|
21 | 30 |
|
22 | 31 | class CheckForUpdatesApiResponse(BaseModel):
|
@@ -46,22 +55,123 @@ def get(self, request: Request, project) -> Response:
|
46 | 55 | Check for updates for a preprod artifact
|
47 | 56 | """
|
48 | 57 | main_binary_identifier = request.GET.get("main_binary_identifier")
|
49 |
| - if not main_binary_identifier: |
50 |
| - # Not implemented yet |
51 |
| - return Response(CheckForUpdatesApiResponse().dict()) |
| 58 | + app_id = request.GET.get("app_id") |
| 59 | + |
| 60 | + platform = request.GET.get("platform") |
| 61 | + provided_version = request.GET.get("version") |
| 62 | + provided_build_configuration_name = request.GET.get("build_configuration") |
| 63 | + |
| 64 | + if not app_id or not platform or not provided_version or not main_binary_identifier: |
| 65 | + return Response({"error": "Missing required parameters"}, status=400) |
| 66 | + |
| 67 | + provided_build_configuration = None |
| 68 | + if provided_build_configuration_name: |
| 69 | + try: |
| 70 | + provided_build_configuration = PreprodBuildConfiguration.objects.get( |
| 71 | + project=project, |
| 72 | + name=provided_build_configuration_name, |
| 73 | + ) |
| 74 | + except PreprodBuildConfiguration.DoesNotExist: |
| 75 | + return Response({"error": "Invalid build configuration"}, status=400) |
| 76 | + |
| 77 | + preprod_artifact = None |
| 78 | + current = None |
| 79 | + update = None |
| 80 | + |
| 81 | + # Common filter logic |
| 82 | + def get_base_filters(): |
| 83 | + filter_kwargs = { |
| 84 | + "project": project, |
| 85 | + "app_id": app_id, |
| 86 | + } |
| 87 | + |
| 88 | + if platform == "android": |
| 89 | + filter_kwargs["artifact_type__in"] = [ |
| 90 | + PreprodArtifact.ArtifactType.AAB, |
| 91 | + PreprodArtifact.ArtifactType.APK, |
| 92 | + ] |
| 93 | + elif platform == "ios": |
| 94 | + filter_kwargs["artifact_type"] = PreprodArtifact.ArtifactType.XCARCHIVE |
| 95 | + |
| 96 | + return filter_kwargs |
52 | 97 |
|
53 | 98 | try:
|
54 |
| - preprod_artifact = PreprodArtifact.objects.filter( |
55 |
| - project=project, main_binary_identifier=main_binary_identifier |
56 |
| - ).latest("date_added") |
| 99 | + current_filter_kwargs = get_base_filters() |
| 100 | + current_filter_kwargs.update( |
| 101 | + { |
| 102 | + "main_binary_identifier": main_binary_identifier, |
| 103 | + "build_version": provided_version, |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + if provided_build_configuration: |
| 108 | + current_filter_kwargs["build_configuration"] = provided_build_configuration |
| 109 | + |
| 110 | + preprod_artifact = PreprodArtifact.objects.filter(**current_filter_kwargs).latest( |
| 111 | + "date_added" |
| 112 | + ) |
57 | 113 | except PreprodArtifact.DoesNotExist:
|
58 |
| - return Response({"error": "Not found"}, status=404) |
| 114 | + logger.warning( |
| 115 | + "No artifact found for binary identifier with version %s", provided_version |
| 116 | + ) |
59 | 117 |
|
60 |
| - if preprod_artifact.build_version and preprod_artifact.build_number: |
| 118 | + if preprod_artifact and preprod_artifact.build_version and preprod_artifact.build_number: |
61 | 119 | current = InstallableBuildDetails(
|
| 120 | + id=str(preprod_artifact.id), |
62 | 121 | build_version=preprod_artifact.build_version,
|
63 | 122 | build_number=preprod_artifact.build_number,
|
| 123 | + app_name=preprod_artifact.app_name, |
| 124 | + download_url=get_download_url_for_artifact(preprod_artifact, request), |
| 125 | + created_date=preprod_artifact.date_added.isoformat(), |
64 | 126 | )
|
65 |
| - else: |
66 |
| - current = None |
67 |
| - return Response(CheckForUpdatesApiResponse(current=current).dict()) |
| 127 | + |
| 128 | + # Get the update object - find the highest version available |
| 129 | + # Get all build versions for this app and platform |
| 130 | + new_build_filter_kwargs = get_base_filters() |
| 131 | + if preprod_artifact: |
| 132 | + new_build_filter_kwargs["build_configuration"] = preprod_artifact.build_configuration |
| 133 | + elif provided_build_configuration: |
| 134 | + new_build_filter_kwargs["build_configuration"] = provided_build_configuration |
| 135 | + all_versions = ( |
| 136 | + PreprodArtifact.objects.filter(**new_build_filter_kwargs) |
| 137 | + .values_list("build_version", flat=True) |
| 138 | + .distinct() |
| 139 | + ) |
| 140 | + |
| 141 | + # Find the highest semver version |
| 142 | + highest_version = None |
| 143 | + for version in all_versions: |
| 144 | + if version: |
| 145 | + try: |
| 146 | + parsed_version = parse_version(version) |
| 147 | + if highest_version is None or parsed_version > parse_version(highest_version): |
| 148 | + highest_version = version |
| 149 | + except Exception: |
| 150 | + # Skip invalid version strings |
| 151 | + continue |
| 152 | + |
| 153 | + # Get all artifacts for the highest version |
| 154 | + if highest_version: |
| 155 | + new_build_filter_kwargs["build_version"] = highest_version |
| 156 | + potential_artifacts = PreprodArtifact.objects.filter(**new_build_filter_kwargs) |
| 157 | + |
| 158 | + # Filter for installable artifacts and get the one with highest build_number |
| 159 | + installable_artifacts = [ |
| 160 | + artifact for artifact in potential_artifacts if is_installable_artifact(artifact) |
| 161 | + ] |
| 162 | + if len(installable_artifacts) > 0: |
| 163 | + best_artifact = max( |
| 164 | + installable_artifacts, key=lambda a: (a.build_number, a.date_added) |
| 165 | + ) |
| 166 | + if not preprod_artifact or preprod_artifact.id != best_artifact.id: |
| 167 | + if best_artifact.build_version and best_artifact.build_number: |
| 168 | + update = InstallableBuildDetails( |
| 169 | + id=str(best_artifact.id), |
| 170 | + build_version=best_artifact.build_version, |
| 171 | + build_number=best_artifact.build_number, |
| 172 | + app_name=best_artifact.app_name, |
| 173 | + download_url=get_download_url_for_artifact(best_artifact, request), |
| 174 | + created_date=best_artifact.date_added.isoformat(), |
| 175 | + ) |
| 176 | + |
| 177 | + return Response(CheckForUpdatesApiResponse(current=current, update=update).dict()) |
0 commit comments