From 7b2d930309445d42bc873320091eb52c4b0936ef Mon Sep 17 00:00:00 2001 From: Dan D'Avella Date: Thu, 2 Jan 2025 10:16:46 -0500 Subject: [PATCH] Fix sonar issues and hotspots parsing (#962) * Fix bug when handling empty Sonar issues JSON * Fix deprecation warning: pydantic configdict --- src/codemodder/codetf.py | 8 +++---- src/core_codemods/sonar/results.py | 2 +- tests/test_sonar_results.py | 36 +++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/codemodder/codetf.py b/src/codemodder/codetf.py index 7a9f3a63..779d319b 100644 --- a/src/codemodder/codetf.py +++ b/src/codemodder/codetf.py @@ -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 @@ -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, diff --git a/src/core_codemods/sonar/results.py b/src/core_codemods/sonar/results.py index 316ce944..d6f34811 100644 --- a/src/core_codemods/sonar/results.py +++ b/src/core_codemods/sonar/results.py @@ -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)) diff --git a/tests/test_sonar_results.py b/tests/test_sonar_results.py index 482f81ad..b3e4c6a6 100644 --- a/tests/test_sonar_results.py +++ b/tests/test_sonar_results.py @@ -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(): @@ -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