Browse Source

feat(create-project): expose sdk versions from registry (#55018)

Ogi 1 year ago
parent
commit
a0b5760f08

+ 21 - 1
src/sentry/api/endpoints/organization_sdk_updates.py

@@ -7,9 +7,11 @@ from packaging import version
 from rest_framework.request import Request
 from rest_framework.response import Response
 
+from sentry.api.api_owners import ApiOwner
 from sentry.api.base import region_silo_endpoint
 from sentry.api.bases import OrganizationEventsEndpointBase
-from sentry.sdk_updates import SdkIndexState, SdkSetupState, get_suggested_updates
+from sentry.api.bases.organization import OrganizationEndpoint
+from sentry.sdk_updates import SdkIndexState, SdkSetupState, get_sdk_index, get_suggested_updates
 from sentry.snuba import discover
 from sentry.utils.numbers import format_grouped_length
 
@@ -95,3 +97,21 @@ class OrganizationSdkUpdatesEndpoint(OrganizationEventsEndpointBase):
             )
 
         return Response(serialize(result["data"], projects))
+
+
+@region_silo_endpoint
+class OrganizationSdksEndpoint(OrganizationEndpoint):
+    owner = ApiOwner.TELEMETRY_EXPERIENCE
+
+    def get(self, request: Request, organization) -> Response:
+        try:
+            sdks = get_sdk_index()
+
+            if len(sdks) == 0:
+                raise Exception("No SDKs found in index")
+
+            return Response(sdks)
+
+        except Exception as e:
+            sentry_sdk.capture_exception(e)
+            return Response({"detail": "Error occurred while fetching SDKs"}, status=500)

+ 9 - 1
src/sentry/api/urls.py

@@ -388,7 +388,10 @@ from .endpoints.organization_repositories import OrganizationRepositoriesEndpoin
 from .endpoints.organization_repository_commits import OrganizationRepositoryCommitsEndpoint
 from .endpoints.organization_repository_details import OrganizationRepositoryDetailsEndpoint
 from .endpoints.organization_request_project_creation import OrganizationRequestProjectCreation
-from .endpoints.organization_sdk_updates import OrganizationSdkUpdatesEndpoint
+from .endpoints.organization_sdk_updates import (
+    OrganizationSdksEndpoint,
+    OrganizationSdkUpdatesEndpoint,
+)
 from .endpoints.organization_search_details import OrganizationSearchDetailsEndpoint
 from .endpoints.organization_searches import OrganizationSearchesEndpoint
 from .endpoints.organization_sentry_function import OrganizationSentryFunctionEndpoint
@@ -1185,6 +1188,11 @@ ORGANIZATION_URLS = [
         OrganizationSdkUpdatesEndpoint.as_view(),
         name="sentry-api-0-organization-sdk-updates",
     ),
+    re_path(
+        r"^(?P<organization_slug>[^\/]+)/sdks/$",
+        OrganizationSdksEndpoint.as_view(),
+        name="sentry-api-0-organization-sdks",
+    ),
     re_path(
         r"^(?P<organization_slug>[^\/]+)/events/$",
         OrganizationEventsEndpoint.as_view(),

+ 44 - 0
tests/sentry/api/endpoints/test_organization_sdk_updates.py

@@ -198,3 +198,47 @@ class OrganizationSdkUpdates(APITestCase, SnubaTestCase):
             warn_msg
             == "Creating a LegacyVersion has been deprecated and will be removed in the next major release"
         )
+
+
+@region_silo_test(stable=True)
+class OrganizationSdks(APITestCase):
+    endpoint = "sentry-api-0-organization-sdks"
+
+    def setUp(self):
+        super().setUp()
+        self.login_as(user=self.user)
+
+    @mock.patch("sentry.api.endpoints.organization_sdk_updates.get_sdk_index", return_value={})
+    def test_sdks_empty(self, mocked_sdk_index):
+        response = self.get_error_response(self.organization.slug)
+
+        assert mocked_sdk_index.call_count == 1
+        assert response.data == {"detail": "Error occurred while fetching SDKs"}
+
+    @mock.patch(
+        "sentry.api.endpoints.organization_sdk_updates.get_sdk_index",
+        return_value={
+            "sentry.cocoa": {
+                "canonical": "cocoapods:sentry-cocoa",
+                "main_docs_url": "https://docs.sentry.io/platforms/cocoa/",
+                "name": "Sentry Cocoa",
+                "repo_url": "https://github.com/getsentry/sentry-cocoa",
+                "version": "8.10.0",
+            }
+        },
+    )
+    def test_sdks_contains_sdk(self, mocked_sdk_index):
+        response = self.get_success_response(self.organization.slug)
+
+        assert mocked_sdk_index.call_count == 1
+        assert response.data["sentry.cocoa"]
+
+    @mock.patch(
+        "sentry.api.endpoints.organization_sdk_updates.get_sdk_index",
+        side_effect=Exception("Something went wrong"),
+    )
+    def test_sdks_error(self, mocked_sdk_index):
+        response = self.get_error_response(self.organization.slug, status_code=500)
+
+        assert mocked_sdk_index.call_count == 1
+        assert response.data == {"detail": "Error occurred while fetching SDKs"}