Skip to content

Commit

Permalink
Safely handle Sonar results without textRange (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella authored Oct 8, 2024
1 parent f79900e commit 23030a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core_codemods/sonar/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def from_result(cls, result: dict) -> Self:
if not (rule_id := result.get("rule", None) or result.get("ruleKey", None)):
raise ValueError("Could not extract rule id from sarif result.")

locations: list[Location] = [SonarLocation.from_json_location(result)]
locations: list[Location] = (
[SonarLocation.from_json_location(result)]
if result.get("textRange")
else []
)
all_flows: list[list[Location]] = [
[
SonarLocation.from_json_location(json_location)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_sonar_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from core_codemods.sonar.results import SonarResult


def test_result_without_textrange():
result = {
"cleanCodeAttribute": "FORMATTED",
"cleanCodeAttributeCategory": "CONSISTENT",
"component": "PixeeSandbox_DVWA:vulnerabilities/exec/help/help.php",
"creationDate": "2020-10-21T16:03:39+0200",
"debt": "2min",
"effort": "2min",
"flows": [],
"impacts": [{"severity": "LOW", "softwareQuality": "MAINTAINABILITY"}],
"issueStatus": "OPEN",
"key": "AZJnP4pZPJb5bI8DP25Y",
"message": "Replace all tab characters in this file by sequences of "
"white-spaces.",
"organization": "pixee-sandbox",
"project": "PixeeSandbox_DVWA",
"rule": "php:S105",
"severity": "MINOR",
"status": "OPEN",
"tags": ["convention", "psr2"],
"type": "CODE_SMELL",
"updateDate": "2024-10-07T15:50:36+0200",
}
sonar_result = SonarResult.from_result(result)
assert sonar_result.rule_id == "php:S105"
assert sonar_result.finding_id == "AZJnP4pZPJb5bI8DP25Y"
assert sonar_result.locations == []
assert sonar_result.codeflows == []

0 comments on commit 23030a6

Please sign in to comment.