Skip to content

Commit 23030a6

Browse files
authored
Safely handle Sonar results without textRange (#869)
1 parent f79900e commit 23030a6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/core_codemods/sonar/results.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ def from_result(cls, result: dict) -> Self:
4040
if not (rule_id := result.get("rule", None) or result.get("ruleKey", None)):
4141
raise ValueError("Could not extract rule id from sarif result.")
4242

43-
locations: list[Location] = [SonarLocation.from_json_location(result)]
43+
locations: list[Location] = (
44+
[SonarLocation.from_json_location(result)]
45+
if result.get("textRange")
46+
else []
47+
)
4448
all_flows: list[list[Location]] = [
4549
[
4650
SonarLocation.from_json_location(json_location)

tests/test_sonar_results.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from core_codemods.sonar.results import SonarResult
2+
3+
4+
def test_result_without_textrange():
5+
result = {
6+
"cleanCodeAttribute": "FORMATTED",
7+
"cleanCodeAttributeCategory": "CONSISTENT",
8+
"component": "PixeeSandbox_DVWA:vulnerabilities/exec/help/help.php",
9+
"creationDate": "2020-10-21T16:03:39+0200",
10+
"debt": "2min",
11+
"effort": "2min",
12+
"flows": [],
13+
"impacts": [{"severity": "LOW", "softwareQuality": "MAINTAINABILITY"}],
14+
"issueStatus": "OPEN",
15+
"key": "AZJnP4pZPJb5bI8DP25Y",
16+
"message": "Replace all tab characters in this file by sequences of "
17+
"white-spaces.",
18+
"organization": "pixee-sandbox",
19+
"project": "PixeeSandbox_DVWA",
20+
"rule": "php:S105",
21+
"severity": "MINOR",
22+
"status": "OPEN",
23+
"tags": ["convention", "psr2"],
24+
"type": "CODE_SMELL",
25+
"updateDate": "2024-10-07T15:50:36+0200",
26+
}
27+
sonar_result = SonarResult.from_result(result)
28+
assert sonar_result.rule_id == "php:S105"
29+
assert sonar_result.finding_id == "AZJnP4pZPJb5bI8DP25Y"
30+
assert sonar_result.locations == []
31+
assert sonar_result.codeflows == []

0 commit comments

Comments
 (0)