Skip to content

Commit

Permalink
Fix sonar issues and hotspots parsing (#962)
Browse files Browse the repository at this point in the history
* Fix bug when handling empty Sonar issues JSON

* Fix deprecation warning: pydantic configdict
  • Loading branch information
drdavella authored Jan 2, 2025
1 parent 8d0af71 commit 7b2d930
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/codemodder/codetf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pathlib import Path
from typing import TYPE_CHECKING, Optional

from pydantic import BaseModel, model_validator
from pydantic import BaseModel, ConfigDict, model_validator

from codemodder import __version__
from codemodder.logging import logger
Expand Down Expand Up @@ -138,16 +138,14 @@ class Rule(BaseModel):
name: str
url: Optional[str] = None

class Config:
frozen = True
model_config = ConfigDict(frozen=True)


class Finding(BaseModel):
id: Optional[str] = None
rule: Rule

class Config:
frozen = True
model_config = ConfigDict(frozen=True)

def to_unfixed_finding(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/core_codemods/sonar/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def from_json(cls, json_file: str | Path) -> Self:
data = json.load(file)

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

Expand Down
36 changes: 35 additions & 1 deletion tests/test_sonar_results.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from core_codemods.sonar.results import SonarResult
from pathlib import Path

from core_codemods.sonar.results import SonarResult, SonarResultSet

SAMPLE_DIR = Path(__file__).parent / "samples"


def test_result_without_textrange():
Expand Down Expand Up @@ -29,3 +33,33 @@ def test_result_without_textrange():
assert sonar_result.finding_id == "AZJnP4pZPJb5bI8DP25Y"
assert sonar_result.locations == ()
assert sonar_result.codeflows == ()


def test_parse_issues_json():
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_issues.json")
assert len(results) == 34


def test_parse_hotspots_json():
results = SonarResultSet.from_json(SAMPLE_DIR / "sonar_hotspots.json")
assert len(results) == 2


def test_empty_issues(tmpdir, caplog):
empty_json = tmpdir / "empty.json"
empty_json.write_text('{"issues": []}', encoding="utf-8")

results = SonarResultSet.from_json(empty_json)

assert len(results) == 0
assert "Could not parse sonar json" not in caplog.text


def test_empty_hotspots(tmpdir, caplog):
empty_json = tmpdir / "empty.json"
empty_json.write_text('{"hotspots": []}', encoding="utf-8")

results = SonarResultSet.from_json(empty_json)

assert len(results) == 0
assert "Could not parse sonar json" not in caplog.text

0 comments on commit 7b2d930

Please sign in to comment.