From 92cf34feff2f1ef4bf062fe603c30d8b83995c85 Mon Sep 17 00:00:00 2001 From: Jian Yuan Lee Date: Mon, 10 Mar 2025 22:17:49 +0000 Subject: [PATCH] feat: Stacktrace linking for GitHub Enterprise --- .../github_enterprise/integration.py | 11 ++-- .../github_enterprise/test_integration.py | 65 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/sentry/integrations/github_enterprise/integration.py b/src/sentry/integrations/github_enterprise/integration.py index 68814788b9d556..910a2dd4a1cc88 100644 --- a/src/sentry/integrations/github_enterprise/integration.py +++ b/src/sentry/integrations/github_enterprise/integration.py @@ -15,7 +15,6 @@ from sentry.integrations.base import ( FeatureDescription, IntegrationData, - IntegrationFeatureNotImplementedError, IntegrationFeatures, IntegrationMetadata, ) @@ -212,7 +211,7 @@ def get_repositories(self, query: str | None = None) -> list[dict[str, Any]]: ] def source_url_matches(self, url: str) -> bool: - raise IntegrationFeatureNotImplementedError + return url.startswith(f"https://{self.model.metadata["domain_name"]}") def format_source_url(self, repo: Repository, filepath: str, branch: str | None) -> str: # Must format the url ourselves since `check_file` is a head request @@ -220,10 +219,14 @@ def format_source_url(self, repo: Repository, filepath: str, branch: str | None) return f"{repo.url}/blob/{branch}/{filepath}" def extract_branch_from_source_url(self, repo: Repository, url: str) -> str: - raise IntegrationFeatureNotImplementedError + url = url.replace(f"{repo.url}/blob/", "") + branch, _, _ = url.partition("/") + return branch def extract_source_path_from_source_url(self, repo: Repository, url: str) -> str: - raise IntegrationFeatureNotImplementedError + url = url.replace(f"{repo.url}/blob/", "") + _, _, source_path = url.partition("/") + return source_path def search_issues(self, query: str | None, **kwargs): return self.get_client().search_issues(query) diff --git a/tests/sentry/integrations/github_enterprise/test_integration.py b/tests/sentry/integrations/github_enterprise/test_integration.py index 186dda3f691244..4591556453c135 100644 --- a/tests/sentry/integrations/github_enterprise/test_integration.py +++ b/tests/sentry/integrations/github_enterprise/test_integration.py @@ -445,3 +445,68 @@ def test_get_commit_context_all_frames(self, _, __): ), ) ] + + @responses.activate + def test_source_url_matches(self): + self.assert_setup_flow() + integration = Integration.objects.get(provider=self.provider.key) + installation = get_installation_of_type( + GitHubEnterpriseIntegration, integration, self.organization.id + ) + + test_cases = [ + ("https://github.example.org/Test-Organization/foo", True), + ("https://github.example.org/Test-Organization/bar", True), + ("https://github.example.org/Other-Organization/bar", False), + ("https://github.com/Test-Organization/foo", False), + ] + + for url, expected in test_cases: + assert installation.source_url_matches(url) == expected + + @responses.activate + def test_extract_branch_from_source_url(self): + self.assert_setup_flow() + integration = Integration.objects.get(provider=self.provider.key) + with assume_test_silo_mode(SiloMode.REGION): + repo = Repository.objects.create( + organization_id=self.organization.id, + name="Test-Organization/foo", + url="https://github.example.org/Test-Organization/foo", + provider="integrations:github_enterprise", + external_id=123, + config={"name": "Test-Organization/foo"}, + integration_id=integration.id, + ) + installation = get_installation_of_type( + GitHubEnterpriseIntegration, integration, self.organization.id + ) + + source_url = "https://github.example.org/Test-Organization/foo/blob/master/src/sentry/integrations/github/integration.py" + + assert installation.extract_branch_from_source_url(repo, source_url) == "master" + + @responses.activate + def test_extract_source_path_from_source_url(self): + self.assert_setup_flow() + integration = Integration.objects.get(provider=self.provider.key) + with assume_test_silo_mode(SiloMode.REGION): + repo = Repository.objects.create( + organization_id=self.organization.id, + name="Test-Organization/foo", + url="https://github.example.org/Test-Organization/foo", + provider="integrations:github_enterprise", + external_id=123, + config={"name": "Test-Organization/foo"}, + integration_id=integration.id, + ) + installation = get_installation_of_type( + GitHubEnterpriseIntegration, integration, self.organization.id + ) + + source_url = "https://github.example.org/Test-Organization/foo/blob/master/src/sentry/integrations/github/integration.py" + + assert ( + installation.extract_source_path_from_source_url(repo, source_url) + == "src/sentry/integrations/github/integration.py" + )