From 91fed9b5d0461d9cde955455984bcdc2922d96cd Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 4 Dec 2023 18:22:31 +0530 Subject: [PATCH] Add CODE_PATH handling --- run_community_analyzer.py | 10 +++++++--- tests/test_community_analyzer.py | 6 ++++-- tests/testutils.py | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/run_community_analyzer.py b/run_community_analyzer.py index 430476ff..04abedf0 100644 --- a/run_community_analyzer.py +++ b/run_community_analyzer.py @@ -30,7 +30,7 @@ def get_issue_map(analyzer_name: str) -> str: return os.path.join(analyzers_dir, analyzer_name, "utils", "issue_map.json") -def get_files_to_analyze() -> set[str]: +def get_files_to_analyze(code_path: str) -> set[str]: """ Read the analysis config to get the list of files to analyze. Always raise issues only in these files. @@ -45,11 +45,15 @@ def get_files_to_analyze() -> set[str]: analysis_config = json.load(file) logger.info("Files in analysis config: %s", analysis_config["files"]) - return set(analysis_config["files"]) + return { + os.path.relpath(analysis_file, code_path) + for analysis_file in analysis_config["files"] + } def main(argv: list[str] | None = None) -> None: """Runs the CLI.""" + code_path = os.getenv("CODE_PATH", "/code") toolbox_path = os.getenv("TOOLBOX_PATH", "/toolbox") output_path = os.path.join(toolbox_path, "analysis_results.json") artifacts_path = os.getenv("ARTIFACTS_PATH", "/artifacts") @@ -64,7 +68,7 @@ def main(argv: list[str] | None = None) -> None: analyzer_name = args.analyzer issue_map_path = get_issue_map(analyzer_name) - modified_files = get_files_to_analyze() + modified_files = get_files_to_analyze(code_path) run_sarif_parser( artifacts_path, output_path, issue_map_path, modified_files=modified_files ) diff --git a/tests/test_community_analyzer.py b/tests/test_community_analyzer.py index 24486e52..a5b3f997 100644 --- a/tests/test_community_analyzer.py +++ b/tests/test_community_analyzer.py @@ -184,10 +184,12 @@ def test_community_analyzer(tmp_path: Path) -> None: """Test for `run_community_analyzer.main()`, to test `issue_map.json` parsing.""" + code_path = "/code" toolbox_path = tmp_path.as_posix() artifacts_path = os.path.join(os.path.dirname(__file__), "test_artifacts") analysis_config_path = os.path.join(toolbox_path, "analysis_config.json") - modified_files = extract_filepaths_from_deepsource_json(expected_result) + modified_files = extract_filepaths_from_deepsource_json(code_path, expected_result) + os.environ["CODE_PATH"] = code_path os.environ["TOOLBOX_PATH"] = toolbox_path os.environ["ARTIFACTS_PATH"] = artifacts_path @@ -214,7 +216,7 @@ def test_community_analyzer(tmp_path: Path) -> None: # Note: There are 7 issues in this file in our report fixture. # See `expected_result`. modified_files = [ - "charts/runner/templates/tests/test-connection.yaml", + os.path.join(code_path, "charts/runner/templates/tests/test-connection.yaml"), ] with temp_analysis_config(analysis_config_path, modified_files): run_community_analyzer.main(["--analyzer=kube-linter"]) diff --git a/tests/testutils.py b/tests/testutils.py index 28b85271..d915fcf8 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -19,12 +19,12 @@ def extract_filepaths_from_sarif(sarif: dict[str, Any]) -> list[str]: def extract_filepaths_from_deepsource_json( - deepsource_json: dict[str, Any] + code_path: str, deepsource_json: dict[str, Any] ) -> list[str]: """Extracts filepaths from a DeepSource JSON file.""" filepaths = [] for issue in deepsource_json["issues"]: - filepaths.append(issue["location"]["path"]) + filepaths.append(os.path.join(code_path, issue["location"]["path"])) return filepaths