Skip to content

Commit 4900d98

Browse files
committed
feat: Stacktrace linking for GitHub Enterprise
1 parent 624bcfb commit 4900d98

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

src/sentry/integrations/github_enterprise/integration.py

Lines changed: 9 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,24 @@ 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"]}") or url.startswith(
215+
f"http://{self.model.metadata["domain_name"]}"
216+
)
216217

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

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

225228
def extract_source_path_from_source_url(self, repo: Repository, url: str) -> str:
226-
raise IntegrationFeatureNotImplementedError
229+
url = url.replace(f"{repo.url}/blob/", "")
230+
_, _, source_path = url.partition("/")
231+
return source_path
227232

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

tests/sentry/integrations/github_enterprise/test_integration.py

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

0 commit comments

Comments
 (0)