Skip to content

Commit

Permalink
feat(issues): Encode stacktrace url file path (#85384)
Browse files Browse the repository at this point in the history
Support Nextjs style square brackets in paths `pages/posts/[id].js` and properly encode a valid URL.

Fixes #82802
  • Loading branch information
scttcper authored Feb 19, 2025
1 parent ecab965 commit feffcab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/sentry/integrations/source_code_management/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from abc import ABC, abstractmethod
from collections.abc import Mapping
from typing import Any
from urllib.parse import quote as urlquote
from urllib.parse import urlparse, urlunparse

import sentry_sdk

Expand Down Expand Up @@ -165,16 +167,23 @@ def get_stacktrace_link(
)
scope = sentry_sdk.Scope.get_isolation_scope()
scope.set_tag("stacktrace_link.tried_version", False)

def encode_url(url: str) -> str:
parsed = urlparse(url)
# Encode elements of the filepath like square brackets
# Preserve path separators and query params etc.
return urlunparse(parsed._replace(path=urlquote(parsed.path, safe="/")))

if version:
scope.set_tag("stacktrace_link.tried_version", True)
source_url = self.check_file(repo, filepath, version)
if source_url:
scope.set_tag("stacktrace_link.used_version", True)
return source_url
return encode_url(source_url)

scope.set_tag("stacktrace_link.used_version", False)
source_url = self.check_file(repo, filepath, default)

return source_url
return encode_url(source_url) if source_url else None

def get_codeowner_file(
self, repo: Repository, ref: str | None = None
Expand Down
32 changes: 32 additions & 0 deletions tests/sentry/integrations/github/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,3 +1163,35 @@ def test_extract_source_path_from_source_url(self):
installation.extract_source_path_from_source_url(repo, source_url)
== "src/sentry/integrations/github/integration.py"
)

@responses.activate
def test_get_stacktrace_link_with_special_chars(self):
"""Test that URLs with special characters (like square brackets) are properly encoded"""
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.com/Test-Organization/foo",
provider="integrations:github",
external_id=123,
config={"name": "Test-Organization/foo"},
integration_id=integration.id,
)

installation = get_installation_of_type(
GitHubIntegration, integration, self.organization.id
)

filepath = "src/components/[id]/test.py"
branch = "master"
responses.add(
responses.HEAD,
f"{self.base_url}/repos/{repo.name}/contents/{filepath}?ref={branch}",
)
source_url = installation.get_stacktrace_link(repo, filepath, branch, branch)
assert (
source_url
== "https://github.com/Test-Organization/foo/blob/master/src/components/%5Bid%5D/test.py"
)

0 comments on commit feffcab

Please sign in to comment.