-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(integrations): Add GitHub repository platform detection #109699
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5786c58
feat(integrations): Add GitHub repository platform detection
jaydgoss 9f0de20
fix(integrations): Handle additional exceptions in _get_repo_file_con…
jaydgoss b605c56
fix(integrations): Use appropriate language name in test assertion
jaydgoss 4269cdf
:hammer_and_wrench: Sync API Urls to TypeScript
getsantry[bot] 560ed4b
ref(integrations): Extract repo lookup into OrganizationRepositoryEnd…
jaydgoss 33847db
fix(integrations): Use exact equality for GitHub provider check
jaydgoss 4736d0d
feat(integrations): Gate platform detection endpoint behind feature flag
jaydgoss e549345
fix(integrations): Handle TypeError in _get_repo_file_content
jaydgoss f128525
fix(integrations): Catch ValueError from HTML responses in platform d…
jaydgoss 59ed13c
fix(integrations): Catch ValueError for non-numeric repo_id in Organi…
jaydgoss 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
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
33 changes: 33 additions & 0 deletions
33
src/sentry/integrations/api/bases/organization_repository.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,33 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from rest_framework.request import Request | ||
|
|
||
| from sentry.api.bases.organization import OrganizationEndpoint, OrganizationIntegrationsPermission | ||
| from sentry.api.exceptions import ResourceDoesNotExist | ||
| from sentry.models.organization import Organization | ||
| from sentry.models.repository import Repository | ||
|
|
||
|
|
||
| class OrganizationRepositoryEndpoint(OrganizationEndpoint): | ||
| """Base endpoint that resolves repo_id to a Repository in convert_args.""" | ||
|
|
||
| permission_classes = (OrganizationIntegrationsPermission,) | ||
|
|
||
| def convert_args( | ||
| self, | ||
| request: Request, | ||
| organization_id_or_slug: int | str | None = None, | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> tuple[tuple[Any, ...], dict[str, Any]]: | ||
| args, kwargs = super().convert_args(request, organization_id_or_slug, *args, **kwargs) | ||
| organization: Organization = kwargs["organization"] | ||
| repo_id = kwargs.pop("repo_id") | ||
| try: | ||
| kwargs["repo"] = Repository.objects.get(id=repo_id, organization_id=organization.id) | ||
| except (Repository.DoesNotExist, ValueError): | ||
| raise ResourceDoesNotExist | ||
|
|
||
| return args, kwargs | ||
72 changes: 72 additions & 0 deletions
72
src/sentry/integrations/api/endpoints/organization_repository_platforms.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,72 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from rest_framework.request import Request | ||
| from rest_framework.response import Response | ||
|
|
||
| from sentry import features | ||
| 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.integrations.api.bases.organization_repository import OrganizationRepositoryEndpoint | ||
| from sentry.integrations.github.client import GitHubApiClient | ||
| from sentry.integrations.github.platform_detection import detect_platforms | ||
| from sentry.integrations.services.integration import integration_service | ||
| from sentry.integrations.types import IntegrationProviderSlug | ||
| from sentry.models.organization import Organization | ||
| from sentry.models.repository import Repository | ||
| from sentry.shared_integrations.exceptions import ApiError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @region_silo_endpoint | ||
| class OrganizationRepositoryPlatformsEndpoint(OrganizationRepositoryEndpoint): | ||
| owner = ApiOwner.INTEGRATIONS | ||
| publish_status = { | ||
| "GET": ApiPublishStatus.PRIVATE, | ||
| } | ||
|
|
||
| def get(self, request: Request, organization: Organization, repo: Repository) -> Response: | ||
| if not features.has( | ||
| "organizations:integrations-github-platform-detection", | ||
| organization, | ||
| actor=request.user, | ||
| ): | ||
| return Response(status=404) | ||
|
|
||
| if ( | ||
| not repo.integration_id | ||
| or repo.provider != f"integrations:{IntegrationProviderSlug.GITHUB}" | ||
| ): | ||
| return Response( | ||
| {"detail": "Platform detection is only supported for GitHub repositories."}, | ||
| status=400, | ||
| ) | ||
|
|
||
| integration = integration_service.get_integration(integration_id=repo.integration_id) | ||
| if integration is None: | ||
| return Response({"detail": "GitHub integration not found."}, status=400) | ||
|
|
||
| org_integration = integration_service.get_organization_integration( | ||
| integration_id=repo.integration_id, organization_id=organization.id | ||
| ) | ||
| if org_integration is None: | ||
| return Response( | ||
| {"detail": "GitHub integration is not configured for this organization."}, | ||
| status=400, | ||
| ) | ||
|
|
||
| client = GitHubApiClient(integration=integration, org_integration_id=org_integration.id) | ||
|
|
||
| try: | ||
| platforms = detect_platforms(client=client, repo=repo.name) | ||
| except (ApiError, ValueError): | ||
| logger.exception( | ||
| "integrations.github.platform_detection_failed", | ||
| extra={"repo_id": repo.id, "repo_name": repo.name}, | ||
| ) | ||
| return Response({"detail": "Failed to detect platforms from GitHub."}, status=502) | ||
sentry-warden[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return Response({"platforms": platforms}) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.