Skip to content

Commit 7a538f4

Browse files
jianyuanandrewshie-sentry
authored andcommitted
feat(github-enterprise): Stacktrace linking for GitHub Enterprise (#86744)
1 parent b4b2f3e commit 7a538f4

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

src/sentry/integrations/github_enterprise/integration.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from sentry.integrations.base import (
1616
FeatureDescription,
1717
IntegrationData,
18-
IntegrationFeatureNotImplementedError,
1918
IntegrationFeatures,
2019
IntegrationMetadata,
2120
)
@@ -212,18 +211,22 @@ def get_repositories(self, query: str | None = None) -> list[dict[str, Any]]:
212211
]
213212

214213
def source_url_matches(self, url: str) -> bool:
215-
raise IntegrationFeatureNotImplementedError
214+
return url.startswith(f"https://{self.model.metadata["domain_name"]}")
216215

217216
def format_source_url(self, repo: Repository, filepath: str, branch: str | None) -> str:
218217
# Must format the url ourselves since `check_file` is a head request
219218
# "https://github.example.org/octokit/octokit.rb/blob/master/README.md"
220219
return f"{repo.url}/blob/{branch}/{filepath}"
221220

222221
def extract_branch_from_source_url(self, repo: Repository, url: str) -> str:
223-
raise IntegrationFeatureNotImplementedError
222+
url = url.replace(f"{repo.url}/blob/", "")
223+
branch, _, _ = url.partition("/")
224+
return branch
224225

225226
def extract_source_path_from_source_url(self, repo: Repository, url: str) -> str:
226-
raise IntegrationFeatureNotImplementedError
227+
url = url.replace(f"{repo.url}/blob/", "")
228+
_, _, source_path = url.partition("/")
229+
return source_path
227230

228231
def search_issues(self, query: str | None, **kwargs):
229232
return self.get_client().search_issues(query)

tests/sentry/integrations/github_enterprise/test_integration.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,68 @@ def test_get_commit_context_all_frames(self, _, __):
445445
),
446446
)
447447
]
448+
449+
@responses.activate
450+
def test_source_url_matches(self):
451+
self.assert_setup_flow()
452+
integration = Integration.objects.get(provider=self.provider.key)
453+
installation = get_installation_of_type(
454+
GitHubEnterpriseIntegration, integration, self.organization.id
455+
)
456+
457+
test_cases = [
458+
("https://github.example.org/Test-Organization/foo", True),
459+
("https://github.example.org/Test-Organization/bar", True),
460+
("https://github.example.org/Other-Organization/bar", False),
461+
("https://github.com/Test-Organization/foo", False),
462+
]
463+
464+
for url, expected in test_cases:
465+
assert installation.source_url_matches(url) == expected
466+
467+
@responses.activate
468+
def test_extract_branch_from_source_url(self):
469+
self.assert_setup_flow()
470+
integration = Integration.objects.get(provider=self.provider.key)
471+
with assume_test_silo_mode(SiloMode.REGION):
472+
repo = Repository.objects.create(
473+
organization_id=self.organization.id,
474+
name="Test-Organization/foo",
475+
url="https://github.example.org/Test-Organization/foo",
476+
provider="integrations:github_enterprise",
477+
external_id=123,
478+
config={"name": "Test-Organization/foo"},
479+
integration_id=integration.id,
480+
)
481+
installation = get_installation_of_type(
482+
GitHubEnterpriseIntegration, integration, self.organization.id
483+
)
484+
485+
source_url = "https://github.example.org/Test-Organization/foo/blob/master/src/sentry/integrations/github/integration.py"
486+
487+
assert installation.extract_branch_from_source_url(repo, source_url) == "master"
488+
489+
@responses.activate
490+
def test_extract_source_path_from_source_url(self):
491+
self.assert_setup_flow()
492+
integration = Integration.objects.get(provider=self.provider.key)
493+
with assume_test_silo_mode(SiloMode.REGION):
494+
repo = Repository.objects.create(
495+
organization_id=self.organization.id,
496+
name="Test-Organization/foo",
497+
url="https://github.example.org/Test-Organization/foo",
498+
provider="integrations:github_enterprise",
499+
external_id=123,
500+
config={"name": "Test-Organization/foo"},
501+
integration_id=integration.id,
502+
)
503+
installation = get_installation_of_type(
504+
GitHubEnterpriseIntegration, integration, self.organization.id
505+
)
506+
507+
source_url = "https://github.example.org/Test-Organization/foo/blob/master/src/sentry/integrations/github/integration.py"
508+
509+
assert (
510+
installation.extract_source_path_from_source_url(repo, source_url)
511+
== "src/sentry/integrations/github/integration.py"
512+
)

0 commit comments

Comments
 (0)