Skip to content

Commit 23ddddc

Browse files
committed
Filter results by finding ID
1 parent d2841b4 commit 23ddddc

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/codemodder/result.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ def results_for_rules(self, rule_ids: list[str]) -> list[ResultType]:
284284
)
285285
)
286286

287+
def result_by_finding_id(self, finding_id: str) -> ResultType | None:
288+
"""Returns first result matching the given finding ID."""
289+
return next(
290+
(
291+
result
292+
for results in self.results_for_rule.values()
293+
for result in results
294+
if result.finding and result.finding.id == finding_id
295+
),
296+
None,
297+
)
298+
287299
def files_for_rule(self, rule_id: str) -> list[Path]:
288300
return list(self.get(rule_id, {}).keys())
289301

tests/test_results.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
from pathlib import Path
3+
from uuid import uuid4
34

5+
from codemodder.semgrep import SemgrepResultSet
46
from core_codemods.sonar.results import SonarResultSet
57

68

@@ -298,3 +300,72 @@ def test_sonar_robustness(self, tmpdir):
298300
result = SonarResultSet.from_json(sonar_json)
299301
# did not crash and returned an empty ResultSet
300302
assert not result
303+
304+
def test_sonar_result_by_finding_id(self, tmpdir):
305+
issues = {
306+
"issues": [
307+
{
308+
"rule": "python:S5659",
309+
"status": "OPEN",
310+
"component": "code.py",
311+
"textRange": {
312+
"startLine": 2,
313+
"endLine": 2,
314+
"startOffset": 2,
315+
"endOffset": 2,
316+
},
317+
"key": "1234",
318+
}
319+
]
320+
}
321+
sonar_json = Path(tmpdir) / "sonar1.json"
322+
sonar_json.write_text(json.dumps(issues))
323+
324+
result_set = SonarResultSet.from_json(sonar_json)
325+
result = result_set.result_by_finding_id("1234")
326+
assert result is not None
327+
assert result.finding.rule.id == "python:S5659"
328+
329+
def test_semgrep_sarif_result_by_finding_id(self, tmpdir):
330+
uuid = str(uuid4())
331+
issues = {
332+
"runs": [
333+
{
334+
"tool": {
335+
"driver": {
336+
"name": "Semgrep",
337+
"version": "0.100.0",
338+
}
339+
},
340+
"results": [
341+
{
342+
"message": {
343+
"text": "Found a potential issue",
344+
},
345+
"guid": uuid,
346+
"ruleId": "python:fake.rule.name",
347+
"locations": [
348+
{
349+
"physicalLocation": {
350+
"artifactLocation": {
351+
"uri": str(Path(tmpdir) / "code.py"),
352+
},
353+
"region": {
354+
"startLine": 2,
355+
"startColumn": 2,
356+
},
357+
}
358+
}
359+
],
360+
}
361+
],
362+
}
363+
]
364+
}
365+
sarif_json = Path(tmpdir) / "semgrep.sarif"
366+
sarif_json.write_text(json.dumps(issues))
367+
368+
result_set = SemgrepResultSet.from_sarif(sarif_json)
369+
result = result_set.result_by_finding_id(uuid)
370+
assert result is not None
371+
assert result.finding.rule.id == "python:fake.rule.name"

0 commit comments

Comments
 (0)