-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(preprod): Add distribution error endpoint for launchpad #109497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+149
−2
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68ef48b
feat(preprod): Add distribution error endpoint for launchpad
runningcode d386294
fix(preprod): Validate error_code range and handle non-dict JSON
runningcode 941feec
:hammer_and_wrench: Sync API Urls to TypeScript
getsantry[bot] eb19fe7
fix(preprod): Handle Annotated types in parse_request_with_pydantic
runningcode 49ba389
ref(preprod): Move distribution endpoint to org-scoped URL
runningcode f7c89d4
:hammer_and_wrench: Sync API Urls to TypeScript
getsantry[bot] 30224f3
fix(preprod): Restore project param and update test URLs
runningcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/sentry/preprod/api/endpoints/project_preprod_distribution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Any, cast | ||
|
|
||
| from pydantic import BaseModel, Field | ||
| 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 internal_region_silo_endpoint | ||
| from sentry.models.project import Project | ||
| from sentry.preprod.api.bases.preprod_artifact_endpoint import PreprodArtifactEndpoint | ||
| from sentry.preprod.api.endpoints.project_preprod_size import parse_request_with_pydantic | ||
| from sentry.preprod.authentication import ( | ||
| LaunchpadRpcPermission, | ||
| LaunchpadRpcSignatureAuthentication, | ||
| ) | ||
| from sentry.preprod.models import PreprodArtifact | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PutDistribution(BaseModel): | ||
| error_code: int = Field(ge=0, le=3) | ||
| error_message: str | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @internal_region_silo_endpoint | ||
| class ProjectPreprodDistributionEndpoint(PreprodArtifactEndpoint): | ||
| owner = ApiOwner.EMERGE_TOOLS | ||
| publish_status = { | ||
| "PUT": ApiPublishStatus.PRIVATE, | ||
| } | ||
| authentication_classes = (LaunchpadRpcSignatureAuthentication,) | ||
| permission_classes = (LaunchpadRpcPermission,) | ||
|
|
||
| def put( | ||
| self, | ||
| request: Request, | ||
| project: Project, | ||
| head_artifact_id: int, | ||
| head_artifact: PreprodArtifact, | ||
| ) -> Response: | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| put: PutDistribution = parse_request_with_pydantic(request, cast(Any, PutDistribution)) | ||
|
|
||
| head_artifact.installable_app_error_code = put.error_code | ||
| head_artifact.installable_app_error_message = put.error_message | ||
| head_artifact.save( | ||
| update_fields=[ | ||
| "installable_app_error_code", | ||
| "installable_app_error_message", | ||
| "date_updated", | ||
| ] | ||
| ) | ||
|
|
||
| return Response({"artifactId": str(head_artifact.id)}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/sentry/preprod/api/endpoints/test_project_preprod_distribution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import orjson | ||
| from django.test import override_settings | ||
|
|
||
| from sentry.preprod.models import PreprodArtifact | ||
| from sentry.testutils.auth import generate_service_request_signature | ||
| from sentry.testutils.cases import TestCase | ||
|
|
||
| SHARED_SECRET_FOR_TESTS = "test-secret-key" | ||
|
|
||
|
|
||
| class ProjectPreprodDistributionEndpointTest(TestCase): | ||
| def setUp(self) -> None: | ||
| super().setUp() | ||
| self.file = self.create_file(name="test_artifact.apk", type="application/octet-stream") | ||
| self.artifact = self.create_preprod_artifact( | ||
| project=self.project, | ||
| file_id=self.file.id, | ||
| state=PreprodArtifact.ArtifactState.PROCESSED, | ||
| ) | ||
|
|
||
| def _put(self, data, secret=SHARED_SECRET_FOR_TESTS): | ||
| url = f"/api/0/organizations/{self.organization.slug}/preprodartifacts/{self.artifact.id}/distribution/" | ||
| signature = generate_service_request_signature(url, data, [secret], "Launchpad") | ||
| return self.client.put( | ||
| url, | ||
| data=data, | ||
| content_type="application/json", | ||
| HTTP_AUTHORIZATION=f"rpcsignature {signature}", | ||
| ) | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_bad_auth(self) -> None: | ||
| response = self._put(b"{}", secret="wrong secret") | ||
| assert response.status_code == 401 | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_missing_fields(self) -> None: | ||
| response = self._put(b"{}") | ||
| assert response.status_code == 400 | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_bad_json(self) -> None: | ||
| response = self._put(b"{") | ||
| assert response.status_code == 400 | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_set_error(self) -> None: | ||
| response = self._put( | ||
| orjson.dumps({"error_code": 3, "error_message": "Unsupported artifact type"}) | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| self.artifact.refresh_from_db() | ||
| assert ( | ||
| self.artifact.installable_app_error_code | ||
| == PreprodArtifact.InstallableAppErrorCode.PROCESSING_ERROR | ||
| ) | ||
| assert self.artifact.installable_app_error_message == "Unsupported artifact type" | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_invalid_error_code(self) -> None: | ||
| response = self._put(orjson.dumps({"error_code": 99, "error_message": "bad"})) | ||
| assert response.status_code == 400 | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_non_dict_json_body(self) -> None: | ||
| response = self._put(orjson.dumps([1, 2, 3])) | ||
| assert response.status_code == 400 | ||
|
|
||
| @override_settings(LAUNCHPAD_RPC_SHARED_SECRET=[SHARED_SECRET_FOR_TESTS]) | ||
| def test_requires_launchpad_rpc_authentication(self) -> None: | ||
| self.login_as(self.user) | ||
|
|
||
| url = f"/api/0/organizations/{self.organization.slug}/preprodartifacts/{self.artifact.id}/distribution/" | ||
| response = self.client.put( | ||
| url, | ||
| data=orjson.dumps({"error_code": 3, "error_message": "some error"}), | ||
| content_type="application/json", | ||
| ) | ||
|
|
||
| assert response.status_code == 401 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded magic number coupled to enum max value
Low Severity
The
error_codefield usesField(ge=0, le=3)where3is a magic number implicitly coupled to the current max value ofInstallableAppErrorCode(PROCESSING_ERROR=3). If a new enum member is added toInstallableAppErrorCode, this Pydantic validation will silently reject the new value with no obvious link back to the enum, making it easy to miss during future updates. Referencing the enum directly (e.g., using its max value or validating membership) would keep the validation in sync automatically.