Skip to content

Commit

Permalink
Add CODE_PATH handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tushar-deepsource committed Dec 4, 2023
1 parent 44b5076 commit 91fed9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
10 changes: 7 additions & 3 deletions run_community_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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
)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_community_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"])
Expand Down
4 changes: 2 additions & 2 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 91fed9b

Please sign in to comment.