Skip to content

Commit c38b36c

Browse files
authored
feat(code-mapping): Support Java (#90201)
For Java, the `filename` is not enough to determine the stack and source code roots to save the code mapping. The call to this endpoint happens from the Set-up Code Mapping modal during the Save call. This is a follow-up to #90176.
1 parent cbb42f7 commit c38b36c

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/sentry/api/endpoints/project_repo_path_parsing.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ def post(self, request: Request, project) -> Response:
111111

112112
data = serializer.validated_data
113113
source_url = data["source_url"]
114-
stack_path = data["stack_path"]
114+
frame_info = get_frame_info_from_request(request)
115115

116116
repo = serializer.repo
117117
integration = serializer.integration
118118
installation = integration.get_installation(project.organization_id)
119119

120120
branch = installation.extract_branch_from_source_url(repo, source_url)
121121
source_path = installation.extract_source_path_from_source_url(repo, source_url)
122-
stack_root, source_root = find_roots(FrameInfo({"filename": stack_path}), source_path)
122+
stack_root, source_root = find_roots(frame_info, source_path)
123123

124124
return self.respond(
125125
{
@@ -131,3 +131,12 @@ def post(self, request: Request, project) -> Response:
131131
"defaultBranch": branch,
132132
}
133133
)
134+
135+
136+
def get_frame_info_from_request(request: Request) -> FrameInfo:
137+
frame = {
138+
"abs_path": request.data.get("absPath"),
139+
"filename": request.data["stackPath"],
140+
"module": request.data.get("module"),
141+
}
142+
return FrameInfo(frame, request.data.get("platform"))

src/sentry/issues/auto_source_code_config/derived_code_mappings_endpoint.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def get_file_and_repo_matches(request: Request, organization: Organization) -> l
2727
def get_frame_info_from_request(request: Request) -> FrameInfo:
2828
frame = {
2929
"abs_path": request.GET.get("absPath"),
30-
# Currently, the only required parameter, thus, avoiding the `get` method
3130
"filename": request.GET["stacktraceFilename"],
3231
"module": request.GET.get("module"),
3332
}

tests/sentry/api/endpoints/test_project_repo_path_parsing.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ def setUp(self):
1313
name="foo", organization=self.org, teams=[self.create_team(organization=self.org)]
1414
)
1515

16-
def make_post(self, source_url, stack_path, project=None, user=None):
16+
def make_post(
17+
self,
18+
source_url,
19+
stack_path,
20+
module=None,
21+
abs_path=None,
22+
platform=None,
23+
project=None,
24+
user=None,
25+
):
1726
self.login_as(user=user or self.user)
1827
if not project:
1928
project = self.project
@@ -26,7 +35,16 @@ def make_post(self, source_url, stack_path, project=None, user=None):
2635
},
2736
)
2837

29-
return self.client.post(url, data={"sourceUrl": source_url, "stackPath": stack_path})
38+
return self.client.post(
39+
url,
40+
data={
41+
"sourceUrl": source_url,
42+
"stackPath": stack_path,
43+
"module": module,
44+
"absPath": abs_path,
45+
"platform": platform,
46+
},
47+
)
3048

3149

3250
class PathMappingSerializerTest(TestCase):
@@ -204,6 +222,28 @@ def test_basic(self):
204222
"defaultBranch": "master",
205223
}
206224

225+
def test_java_path(self):
226+
src_file = "src/com/example/foo/Bar.kt"
227+
source_url = f"https://github.com/getsentry/sentry/blob/master/{src_file}"
228+
filename = "Bar.kt" # The filename in Java does not contain the package name
229+
resp = self.make_post(
230+
source_url,
231+
filename,
232+
module="com.example.foo.Bar", # The module misses the extension
233+
abs_path="Bar.kt", # abs_path includes the extension
234+
platform="java",
235+
)
236+
assert resp.status_code == 200, resp.content
237+
238+
assert resp.data == {
239+
"defaultBranch": "master",
240+
"integrationId": self.integration.id,
241+
"repositoryId": self.repo.id,
242+
"provider": "github",
243+
"sourceRoot": "src/com/example/",
244+
"stackRoot": "com/example/",
245+
}
246+
207247
def test_short_path(self):
208248
source_url = "https://github.com/getsentry/sentry/blob/main/project_stacktrace_link.py"
209249
stack_path = "sentry/project_stacktrace_link.py"

0 commit comments

Comments
 (0)