Skip to content

Commit 9b459f8

Browse files
committed
CM-46872 - Fix Maven dependencies restore for SCA
1 parent c861b40 commit 9b459f8

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

cycode/cli/files_collector/sca/base_restore_dependencies.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
5959
manifest_file_path = self.get_manifest_file_path(document)
6060
restore_file_path = build_dep_tree_path(document.absolute_path, self.get_lock_file_name())
6161
relative_restore_file_path = build_dep_tree_path(document.path, self.get_lock_file_name())
62-
working_directory_path = self.get_working_directory(document)
6362

6463
if not self.verify_restore_file_already_exist(restore_file_path):
6564
output = execute_commands(
66-
self.get_commands(manifest_file_path),
67-
self.command_timeout,
65+
commands=self.get_commands(manifest_file_path),
66+
timeout=self.command_timeout,
6867
output_file_path=restore_file_path if self.create_output_file_manually else None,
69-
working_directory=working_directory_path,
68+
working_directory=self.get_working_directory(document),
7069
)
7170
if output is None: # one of the commands failed
7271
return None

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from os import path
23
from typing import Optional
34

@@ -29,35 +30,40 @@ def get_commands(self, manifest_file_path: str) -> list[list[str]]:
2930
def get_lock_file_name(self) -> str:
3031
return join_paths('target', MAVEN_CYCLONE_DEP_TREE_FILE_NAME)
3132

33+
def get_working_directory(self, document: Document) -> Optional[str]:
34+
return os.path.dirname(document.absolute_path)
35+
3236
def try_restore_dependencies(self, document: Document) -> Optional[Document]:
33-
restore_dependencies_document = super().try_restore_dependencies(document)
3437
manifest_file_path = self.get_manifest_file_path(document)
3538
if document.content is None:
36-
restore_dependencies_document = self.restore_from_secondary_command(
37-
document, manifest_file_path, restore_dependencies_document
38-
)
39-
else:
40-
restore_dependencies_document.content = get_file_content(
41-
join_paths(get_file_dir(manifest_file_path), self.get_lock_file_name())
42-
)
39+
return self.restore_from_secondary_command(document, manifest_file_path)
40+
41+
restore_dependencies_document = super().try_restore_dependencies(document)
42+
if restore_dependencies_document is None:
43+
return None
44+
45+
restore_dependencies_document.content = get_file_content(
46+
join_paths(get_file_dir(manifest_file_path), self.get_lock_file_name())
47+
)
4348

4449
return restore_dependencies_document
4550

46-
def restore_from_secondary_command(
47-
self, document: Document, manifest_file_path: str, restore_dependencies_document: Optional[Document]
48-
) -> Optional[Document]:
49-
# TODO(MarshalX): does it even work? Ignored restore_dependencies_document arg
50-
secondary_restore_command = create_secondary_restore_commands(manifest_file_path)
51-
backup_restore_content = execute_commands(secondary_restore_command, self.command_timeout)
52-
restore_dependencies_document = Document(
53-
build_dep_tree_path(document.path, MAVEN_DEP_TREE_FILE_NAME), backup_restore_content, self.is_git_diff
51+
def restore_from_secondary_command(self, document: Document, manifest_file_path: str) -> Optional[Document]:
52+
restore_content = execute_commands(
53+
commands=create_secondary_restore_commands(manifest_file_path),
54+
timeout=self.command_timeout,
55+
working_directory=self.get_working_directory(document),
5456
)
55-
restore_dependencies = None
56-
if restore_dependencies_document.content is not None:
57-
restore_dependencies = restore_dependencies_document
58-
restore_dependencies.content = get_file_content(MAVEN_DEP_TREE_FILE_NAME)
57+
if restore_content is None:
58+
return None
5959

60-
return restore_dependencies
60+
restore_file_path = build_dep_tree_path(document.absolute_path, MAVEN_DEP_TREE_FILE_NAME)
61+
return Document(
62+
path=build_dep_tree_path(document.path, MAVEN_DEP_TREE_FILE_NAME),
63+
content=get_file_content(restore_file_path),
64+
is_git_diff_format=self.is_git_diff,
65+
absolute_path=restore_file_path,
66+
)
6167

6268

6369
def create_secondary_restore_commands(manifest_file_path: str) -> list[list[str]]:

cycode/cli/files_collector/sca/sca_code_scanner.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,16 @@ def get_project_file_ecosystem(document: Document) -> Optional[str]:
9292

9393
def try_restore_dependencies(
9494
ctx: typer.Context,
95-
documents_to_add: dict[str, Document],
9695
restore_dependencies: 'BaseRestoreDependencies',
9796
document: Document,
98-
) -> None:
97+
) -> Optional[Document]:
9998
if not restore_dependencies.is_project(document):
100-
return
99+
return None
101100

102101
restore_dependencies_document = restore_dependencies.restore(document)
103102
if restore_dependencies_document is None:
104103
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
105-
return
104+
return None
106105

107106
if restore_dependencies_document.content is None:
108107
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
@@ -114,10 +113,7 @@ def try_restore_dependencies(
114113
manifest_file_path = get_manifest_file_path(document, is_monitor_action, project_path)
115114
logger.debug('Succeeded to generate dependencies tree on path: %s', manifest_file_path)
116115

117-
if restore_dependencies_document.path in documents_to_add:
118-
logger.debug('Duplicate document on restore for path: %s', restore_dependencies_document.path)
119-
else:
120-
documents_to_add[restore_dependencies_document.path] = restore_dependencies_document
116+
return restore_dependencies_document
121117

122118

123119
def add_dependencies_tree_document(
@@ -128,7 +124,14 @@ def add_dependencies_tree_document(
128124

129125
for restore_dependencies in restore_dependencies_list:
130126
for document in documents_to_scan:
131-
try_restore_dependencies(ctx, documents_to_add, restore_dependencies, document)
127+
restore_dependencies_document = try_restore_dependencies(ctx, restore_dependencies, document)
128+
if restore_dependencies_document is None:
129+
continue
130+
131+
if restore_dependencies_document.path in documents_to_add:
132+
logger.debug('Duplicate document on restore for path: %s', restore_dependencies_document.path)
133+
else:
134+
documents_to_add[restore_dependencies_document.path] = restore_dependencies_document
132135

133136
# mutate original list using slice assignment
134137
documents_to_scan[:] = list(documents_to_add.values())

0 commit comments

Comments
 (0)