Skip to content

Commit 18961ee

Browse files
authored
CM-17670 - Fix Gradle dependencies location in monitor mode (#49)
* Fix gradle dependencies location in monitor mode * Minor refactoring * Minor CR Changes
1 parent aba8c74 commit 18961ee

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

cli/code_scanner.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def scan_repository(context: click.Context, path, branch, monitor):
4343
""" Scan git repository including its history """
4444
try:
4545
logger.debug('Starting repository scan process, %s', {'path': path, 'branch': branch})
46+
context.obj["monitor"] = monitor
4647
scan_type = context.obj["scan_type"]
4748
if monitor and scan_type != SCA_SCAN_TYPE:
4849
raise click.ClickException(f"Monitor flag is currently supported for SCA scan type only")
@@ -53,6 +54,7 @@ def scan_repository(context: click.Context, path, branch, monitor):
5354
for obj
5455
in get_git_repository_tree_file_entries(path, branch)]
5556
documents_to_scan = exclude_irrelevant_documents_to_scan(context, documents_to_scan)
57+
perform_pre_scan_documents_actions(context, scan_type, documents_to_scan, False)
5658
logger.debug('Found all relevant files for scanning %s', {'path': path, 'branch': branch})
5759
return scan_documents(context, documents_to_scan, is_git_diff=False,
5860
scan_parameters=get_scan_parameters(path, monitor))
@@ -136,13 +138,15 @@ def pre_commit_scan(context: click.Context, ignored_args: List[str]):
136138

137139

138140
def scan_disk_files(context: click.Context, paths: List[str]):
141+
scan_type = context.obj['scan_type']
139142
is_git_diff = False
140143
documents: List[Document] = []
141144
for path in paths:
142145
with open(path, "r", encoding="utf-8") as f:
143146
content = f.read()
144147
documents.append(Document(path, content, is_git_diff))
145148

149+
perform_pre_scan_documents_actions(context, scan_type, documents, is_git_diff)
146150
return scan_documents(context, documents, is_git_diff=is_git_diff)
147151

148152

@@ -159,7 +163,6 @@ def scan_documents(context: click.Context, documents_to_scan: List[Document],
159163
zipped_documents = InMemoryZip()
160164

161165
try:
162-
perform_pre_scan_documents_actions(scan_type, documents_to_scan, is_git_diff)
163166
zipped_documents = zip_documents_to_scan(scan_type, zipped_documents, documents_to_scan)
164167
scan_result = perform_scan(cycode_client, zipped_documents, scan_type, scan_id, is_git_diff, is_commit_range,
165168
scan_parameters)
@@ -188,9 +191,10 @@ def scan_documents(context: click.Context, documents_to_scan: List[Document],
188191
all_detections_count, len(documents_to_scan), zip_file_size, scan_command_type, error_message)
189192

190193

191-
def perform_pre_scan_documents_actions(scan_type: str, documents_to_scan: List[Document], is_git_diff: bool = False):
192-
if scan_type == 'sca':
193-
sca_code_scanner.run_pre_scan_actions(documents_to_scan, is_git_diff)
194+
def perform_pre_scan_documents_actions(context: click.Context, scan_type: str, documents_to_scan: List[Document],
195+
is_git_diff: bool = False):
196+
if scan_type == SCA_SCAN_TYPE:
197+
sca_code_scanner.run_pre_scan_actions(context, documents_to_scan, is_git_diff)
194198

195199

196200
def zip_documents_to_scan(scan_type: str, zip: InMemoryZip, documents: List[Document]):

cli/helpers/sca_code_scanner.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from cli.utils.shell_executor import shell
1+
import click
22
from typing import List, Optional
3+
from cli.utils.shell_executor import shell
34
from cli.models import Document
45
from cli.utils.path_utils import get_file_dir, join_paths
56
from cyclient import logger
@@ -10,11 +11,14 @@
1011
BUILD_GRADLE_DEP_TREE_TIMEOUT = 180
1112

1213

13-
def run_pre_scan_actions(documents_to_scan: List[Document], is_git_diff: bool = False):
14+
def run_pre_scan_actions(context: click.Context, documents_to_scan: List[Document], is_git_diff: bool = False):
15+
is_monitor_action = context.obj.get('monitor')
16+
project_path = context.params.get('path')
1417
documents_to_add: List[Document] = []
1518
for document in documents_to_scan:
1619
if is_gradle_project(document):
17-
gradle_dependencies_tree = try_generate_dependencies_tree(document.path)
20+
gradle_dependencies_tree = try_generate_dependencies_tree(
21+
get_manifest_file_path(document, is_monitor_action, project_path))
1822
if gradle_dependencies_tree is None:
1923
logger.warning('Error occurred while trying to generate gradle dependencies tree. %s',
2024
{'filename': document.path})
@@ -27,6 +31,10 @@ def run_pre_scan_actions(documents_to_scan: List[Document], is_git_diff: bool =
2731
documents_to_scan.extend(documents_to_add)
2832

2933

34+
def get_manifest_file_path(document, is_monitor_action, project_path):
35+
return join_paths(project_path, document.path) if is_monitor_action else document.path
36+
37+
3038
def try_generate_dependencies_tree(filename: str) -> Optional[str]:
3139
command = ['gradle', 'dependencies', '-b', filename, '-q', '--console', 'plain']
3240
try:

0 commit comments

Comments
 (0)