Skip to content

Commit d616b9e

Browse files
committed
CM-44581 gradle - support restore projects and by selecting specific project
1 parent e81868c commit d616b9e

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

cycode/cli/commands/scan/scan_command.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from cycode.cli.consts import (
1414
ISSUE_DETECTED_STATUS_CODE,
1515
NO_ISSUES_STATUS_CODE,
16-
SCA_SKIP_RESTORE_DEPENDENCIES_FLAG, SCA_GRADLE_ALL_SUB_PROJECTS_FLAG,
16+
SCA_GRADLE_ALL_SUB_PROJECTS_FLAG,
17+
SCA_SKIP_RESTORE_DEPENDENCIES_FLAG,
1718
)
1819
from cycode.cli.models import Severity
1920
from cycode.cli.sentry import add_breadcrumb
@@ -114,7 +115,8 @@
114115
f'--{SCA_GRADLE_ALL_SUB_PROJECTS_FLAG}',
115116
is_flag=True,
116117
default=False,
117-
help='When specified, Cycode will run gradle restore command for all sub projects. Should run from root project directory ONLY!',
118+
help='When specified, Cycode will run gradle restore command for all sub projects. '
119+
'Should run from root project directory ONLY!',
118120
type=bool,
119121
required=False,
120122
)
@@ -132,7 +134,7 @@ def scan_command(
132134
report: bool,
133135
no_restore: bool,
134136
sync: bool,
135-
gradle_all_sub_projects: bool
137+
gradle_all_sub_projects: bool,
136138
) -> int:
137139
"""Scans for Secrets, IaC, SCA or SAST violations."""
138140
add_breadcrumb('scan')

cycode/cli/files_collector/sca/maven/restore_gradle_dependencies.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import re
3-
from typing import List, Set, Optional
3+
from typing import List, Optional, Set
44

55
import click
66

@@ -19,21 +19,26 @@
1919

2020

2121
class RestoreGradleDependencies(BaseRestoreDependencies):
22-
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int,
23-
projects: Set[str] = set()) -> None:
22+
def __init__(
23+
self, context: click.Context, is_git_diff: bool, command_timeout: int, projects: Optional[Set[str]] = None
24+
) -> None:
2425
super().__init__(context, is_git_diff, command_timeout, create_output_file_manually=True)
25-
self.projects = projects
26-
self.projects = self.get_all_projects() if self.is_gradle_sub_projects() else set()
26+
if projects is None:
27+
projects = set()
28+
self.projects = self.get_all_projects() if self.is_gradle_sub_projects() else projects
2729

28-
def is_gradle_sub_projects(self):
30+
def is_gradle_sub_projects(self) -> bool:
2931
return self.context.obj.get(SCA_GRADLE_ALL_SUB_PROJECTS_FLAG)
3032

3133
def is_project(self, document: Document) -> bool:
3234
return document.path.endswith(BUILD_GRADLE_FILE_NAME) or document.path.endswith(BUILD_GRADLE_KTS_FILE_NAME)
3335

3436
def get_commands(self, manifest_file_path: str) -> List[List[str]]:
35-
return self.get_commands_for_sub_projects(manifest_file_path) if self.is_gradle_sub_projects() else [
36-
['gradle', 'dependencies', '-b', manifest_file_path, '-q', '--console', 'plain']]
37+
return (
38+
self.get_commands_for_sub_projects(manifest_file_path)
39+
if self.is_gradle_sub_projects()
40+
else [['gradle', 'dependencies', '-b', manifest_file_path, '-q', '--console', 'plain']]
41+
)
3742

3843
def get_lock_file_name(self) -> str:
3944
return BUILD_GRADLE_DEP_TREE_FILE_NAME
@@ -44,9 +49,12 @@ def verify_restore_file_already_exist(self, restore_file_path: str) -> bool:
4449
def get_working_directory(self, document: Document) -> Optional[str]:
4550
return get_path_from_context(self.context) if self.is_gradle_sub_projects() else None
4651

47-
def get_all_projects(self) -> List[str]:
48-
projects_output = shell(command=BUILD_GRADLE_ALL_PROJECTS_COMMAND, timeout=BUILD_GRADLE_ALL_PROJECTS_TIMEOUT,
49-
working_directory=get_path_from_context(self.context))
52+
def get_all_projects(self) -> Set[str]:
53+
projects_output = shell(
54+
command=BUILD_GRADLE_ALL_PROJECTS_COMMAND,
55+
timeout=BUILD_GRADLE_ALL_PROJECTS_TIMEOUT,
56+
working_directory=get_path_from_context(self.context),
57+
)
5058

5159
projects = re.findall(ALL_PROJECTS_REGEX, projects_output)
5260

@@ -55,5 +63,8 @@ def get_all_projects(self) -> List[str]:
5563
def get_commands_for_sub_projects(self, manifest_file_path: str) -> List[List[str]]:
5664
project_name = os.path.basename(os.path.dirname(manifest_file_path))
5765
project_name = f':{project_name}'
58-
return [['gradle', f'{project_name}:dependencies', '-q', '--console',
59-
'plain']] if project_name in self.projects else []
66+
return (
67+
[['gradle', f'{project_name}:dependencies', '-q', '--console', 'plain']]
68+
if project_name in self.projects
69+
else []
70+
)

cycode/cli/utils/scan_batch.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ def run_parallel_batched_scan(
5151
) -> Tuple[Dict[str, 'CliError'], List['LocalScanResult']]:
5252
max_size = consts.SCAN_BATCH_MAX_SIZE_IN_BYTES.get(scan_type, consts.DEFAULT_SCAN_BATCH_MAX_SIZE_IN_BYTES)
5353

54-
if scan_type == consts.SCA_SCAN_TYPE:
55-
batches = [documents]
56-
else:
57-
batches = split_documents_into_batches(documents, max_size)
54+
batches = [documents] if scan_type == consts.SCA_SCAN_TYPE else split_documents_into_batches(documents, max_size)
5855

5956
progress_bar.set_section_length(ScanProgressBarSection.SCAN, len(batches)) # * 3
6057
# TODO(MarshalX): we should multiply the count of batches in SCAN section because each batch has 3 steps:

0 commit comments

Comments
 (0)