Skip to content

Commit ac26e55

Browse files
authored
CM-46872 - Fix Maven dependencies restore for SCA (#312)
1 parent c861b40 commit ac26e55

File tree

6 files changed

+39
-50
lines changed

6 files changed

+39
-50
lines changed

cycode/cli/files_collector/sca/base_restore_dependencies.py

Lines changed: 4 additions & 5 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
@@ -75,7 +74,7 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
7574
return Document(relative_restore_file_path, restore_file_content, self.is_git_diff)
7675

7776
def get_working_directory(self, document: Document) -> Optional[str]:
78-
return None
77+
return os.path.dirname(document.absolute_path)
7978

8079
@staticmethod
8180
def verify_restore_file_already_exist(restore_file_path: str) -> bool:

cycode/cli/files_collector/sca/go/restore_go_dependencies.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,3 @@ def get_commands(self, manifest_file_path: str) -> list[list[str]]:
4343

4444
def get_lock_file_name(self) -> str:
4545
return GO_RESTORE_FILE_NAME
46-
47-
def get_working_directory(self, document: Document) -> Optional[str]:
48-
return os.path.dirname(document.absolute_path)

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,36 @@ def get_lock_file_name(self) -> str:
3030
return join_paths('target', MAVEN_CYCLONE_DEP_TREE_FILE_NAME)
3131

3232
def try_restore_dependencies(self, document: Document) -> Optional[Document]:
33-
restore_dependencies_document = super().try_restore_dependencies(document)
3433
manifest_file_path = self.get_manifest_file_path(document)
3534
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-
)
35+
return self.restore_from_secondary_command(document, manifest_file_path)
36+
37+
restore_dependencies_document = super().try_restore_dependencies(document)
38+
if restore_dependencies_document is None:
39+
return None
40+
41+
restore_dependencies_document.content = get_file_content(
42+
join_paths(get_file_dir(manifest_file_path), self.get_lock_file_name())
43+
)
4344

4445
return restore_dependencies_document
4546

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
47+
def restore_from_secondary_command(self, document: Document, manifest_file_path: str) -> Optional[Document]:
48+
restore_content = execute_commands(
49+
commands=create_secondary_restore_commands(manifest_file_path),
50+
timeout=self.command_timeout,
51+
working_directory=self.get_working_directory(document),
5452
)
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)
53+
if restore_content is None:
54+
return None
5955

60-
return restore_dependencies
56+
restore_file_path = build_dep_tree_path(document.absolute_path, MAVEN_DEP_TREE_FILE_NAME)
57+
return Document(
58+
path=build_dep_tree_path(document.path, MAVEN_DEP_TREE_FILE_NAME),
59+
content=get_file_content(restore_file_path),
60+
is_git_diff_format=self.is_git_diff,
61+
absolute_path=restore_file_path,
62+
)
6163

6264

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

cycode/cli/files_collector/sca/ruby/restore_ruby_dependencies.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
from typing import Optional
3-
41
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
52
from cycode.cli.models import Document
63

@@ -17,6 +14,3 @@ def get_commands(self, manifest_file_path: str) -> list[list[str]]:
1714

1815
def get_lock_file_name(self) -> str:
1916
return RUBY_LOCK_FILE_NAME
20-
21-
def get_working_directory(self, document: Document) -> Optional[str]:
22-
return os.path.dirname(document.absolute_path)

cycode/cli/files_collector/sca/sbt/restore_sbt_dependencies.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
from typing import Optional
3-
41
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
52
from cycode.cli.models import Document
63

@@ -17,6 +14,3 @@ def get_commands(self, manifest_file_path: str) -> list[list[str]]:
1714

1815
def get_lock_file_name(self) -> str:
1916
return SBT_LOCK_FILE_NAME
20-
21-
def get_working_directory(self, document: Document) -> Optional[str]:
22-
return os.path.dirname(document.absolute_path)

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)