From eaa075b30f2521b35a2cb1151664226b308bf05b Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Wed, 8 Jan 2025 22:22:40 -0500 Subject: [PATCH] Add --sonar-json CLI flag; deprecate existing sonar flags --- src/codemodder/cli.py | 5 +++++ src/codemodder/codemodder.py | 12 ++++++++++++ tests/test_sonar_results.py | 12 ++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/codemodder/cli.py b/src/codemodder/cli.py index 5b3f47dc..1f51ad62 100644 --- a/src/codemodder/cli.py +++ b/src/codemodder/cli.py @@ -174,6 +174,11 @@ def parse_args(argv, codemod_registry: CodemodRegistry): action=CsvListAction, help="Comma-separated set of path(s) to Sonar hotspots JSON file(s) to feed to the codemods", ) + parser.add_argument( + "--sonar-json", + action=CsvListAction, + help="Comma-separated set of path(s) to Sonar JSON file(s) to feed to the codemods", + ) parser.add_argument( "--defectdojo-findings-json", action=CsvListAction, diff --git a/src/codemodder/codemodder.py b/src/codemodder/codemodder.py index c1d88a37..06566b73 100644 --- a/src/codemodder/codemodder.py +++ b/src/codemodder/codemodder.py @@ -239,8 +239,20 @@ def _run_cli(original_args) -> int: logger.error(err) return 1 + if argv.sonar_issues_json: + print( + "NOTE: --sonar-issues-json is deprecated, use --sonar-json instead", + file=sys.stderr, + ) + if argv.sonar_hotspots_json: + print( + "NOTE: --sonar-hotspots-json is deprecated, use --sonar-json instead", + file=sys.stderr, + ) + tool_result_files_map["sonar"].extend(argv.sonar_issues_json or []) tool_result_files_map["sonar"].extend(argv.sonar_hotspots_json or []) + tool_result_files_map["sonar"].extend(argv.sonar_json or []) tool_result_files_map["defectdojo"].extend(argv.defectdojo_findings_json or []) logger.info("command: %s %s", Path(sys.argv[0]).name, " ".join(original_args)) diff --git a/tests/test_sonar_results.py b/tests/test_sonar_results.py index b3e4c6a6..cb59e5a5 100644 --- a/tests/test_sonar_results.py +++ b/tests/test_sonar_results.py @@ -1,3 +1,4 @@ +import json from pathlib import Path from core_codemods.sonar.results import SonarResult, SonarResultSet @@ -45,6 +46,17 @@ def test_parse_hotspots_json(): assert len(results) == 2 +def test_combined_json(tmpdir): + issues = json.loads(SAMPLE_DIR.joinpath("sonar_issues.json").read_text()) + hotspots = json.loads(SAMPLE_DIR.joinpath("sonar_hotspots.json").read_text()) + Path(tmpdir).joinpath("combined.json").write_text( + json.dumps({"issues": issues["issues"] + hotspots["hotspots"]}) + ) + + results = SonarResultSet.from_json(Path(tmpdir).joinpath("combined.json")) + assert len(results) == 36 + + def test_empty_issues(tmpdir, caplog): empty_json = tmpdir / "empty.json" empty_json.write_text('{"issues": []}', encoding="utf-8")