Skip to content

Commit 7b2d930

Browse files
authored
Fix sonar issues and hotspots parsing (#962)
* Fix bug when handling empty Sonar issues JSON * Fix deprecation warning: pydantic configdict
1 parent 8d0af71 commit 7b2d930

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/codemodder/codetf.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pathlib import Path
1313
from typing import TYPE_CHECKING, Optional
1414

15-
from pydantic import BaseModel, model_validator
15+
from pydantic import BaseModel, ConfigDict, model_validator
1616

1717
from codemodder import __version__
1818
from codemodder.logging import logger
@@ -138,16 +138,14 @@ class Rule(BaseModel):
138138
name: str
139139
url: Optional[str] = None
140140

141-
class Config:
142-
frozen = True
141+
model_config = ConfigDict(frozen=True)
143142

144143

145144
class Finding(BaseModel):
146145
id: Optional[str] = None
147146
rule: Rule
148147

149-
class Config:
150-
frozen = True
148+
model_config = ConfigDict(frozen=True)
151149

152150
def to_unfixed_finding(
153151
self,

src/core_codemods/sonar/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def from_json(cls, json_file: str | Path) -> Self:
9999
data = json.load(file)
100100

101101
result_set = cls()
102-
for result in data.get("issues") or [] + data.get("hotspots") or []:
102+
for result in data.get("issues", []) + data.get("hotspots", []):
103103
if result["status"].lower() in ("open", "to_review"):
104104
result_set.add_result(SonarResult.from_result(result))
105105

tests/test_sonar_results.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from core_codemods.sonar.results import SonarResult
1+
from pathlib import Path
2+
3+
from core_codemods.sonar.results import SonarResult, SonarResultSet
4+
5+
SAMPLE_DIR = Path(__file__).parent / "samples"
26

37

48
def test_result_without_textrange():
@@ -29,3 +33,33 @@ def test_result_without_textrange():
2933
assert sonar_result.finding_id == "AZJnP4pZPJb5bI8DP25Y"
3034
assert sonar_result.locations == ()
3135
assert sonar_result.codeflows == ()
36+
37+
38+
def test_parse_issues_json():
39+
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_issues.json")
40+
assert len(results) == 34
41+
42+
43+
def test_parse_hotspots_json():
44+
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_hotspots.json")
45+
assert len(results) == 2
46+
47+
48+
def test_empty_issues(tmpdir, caplog):
49+
empty_json = tmpdir / "empty.json"
50+
empty_json.write_text('{"issues": []}', encoding="utf-8")
51+
52+
results = SonarResultSet.from_json(empty_json)
53+
54+
assert len(results) == 0
55+
assert "Could not parse sonar json" not in caplog.text
56+
57+
58+
def test_empty_hotspots(tmpdir, caplog):
59+
empty_json = tmpdir / "empty.json"
60+
empty_json.write_text('{"hotspots": []}', encoding="utf-8")
61+
62+
results = SonarResultSet.from_json(empty_json)
63+
64+
assert len(results) == 0
65+
assert "Could not parse sonar json" not in caplog.text

0 commit comments

Comments
 (0)