Skip to content

CM-46872 - Fix Maven dependencies restore for SCA #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cycode/cli/files_collector/sca/base_restore_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
manifest_file_path = self.get_manifest_file_path(document)
restore_file_path = build_dep_tree_path(document.absolute_path, self.get_lock_file_name())
relative_restore_file_path = build_dep_tree_path(document.path, self.get_lock_file_name())
working_directory_path = self.get_working_directory(document)

if not self.verify_restore_file_already_exist(restore_file_path):
output = execute_commands(
self.get_commands(manifest_file_path),
self.command_timeout,
commands=self.get_commands(manifest_file_path),
timeout=self.command_timeout,
output_file_path=restore_file_path if self.create_output_file_manually else None,
working_directory=working_directory_path,
working_directory=self.get_working_directory(document),
)
if output is None: # one of the commands failed
return None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from os import path
from typing import Optional

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

def get_working_directory(self, document: Document) -> Optional[str]:
return os.path.dirname(document.absolute_path)

def try_restore_dependencies(self, document: Document) -> Optional[Document]:
restore_dependencies_document = super().try_restore_dependencies(document)
manifest_file_path = self.get_manifest_file_path(document)
if document.content is None:
restore_dependencies_document = self.restore_from_secondary_command(
document, manifest_file_path, restore_dependencies_document
)
else:
restore_dependencies_document.content = get_file_content(
join_paths(get_file_dir(manifest_file_path), self.get_lock_file_name())
)
return self.restore_from_secondary_command(document, manifest_file_path)

restore_dependencies_document = super().try_restore_dependencies(document)
if restore_dependencies_document is None:
return None

restore_dependencies_document.content = get_file_content(
join_paths(get_file_dir(manifest_file_path), self.get_lock_file_name())
)

return restore_dependencies_document

def restore_from_secondary_command(
self, document: Document, manifest_file_path: str, restore_dependencies_document: Optional[Document]
) -> Optional[Document]:
# TODO(MarshalX): does it even work? Ignored restore_dependencies_document arg
secondary_restore_command = create_secondary_restore_commands(manifest_file_path)
backup_restore_content = execute_commands(secondary_restore_command, self.command_timeout)
restore_dependencies_document = Document(
build_dep_tree_path(document.path, MAVEN_DEP_TREE_FILE_NAME), backup_restore_content, self.is_git_diff
def restore_from_secondary_command(self, document: Document, manifest_file_path: str) -> Optional[Document]:
restore_content = execute_commands(
commands=create_secondary_restore_commands(manifest_file_path),
timeout=self.command_timeout,
working_directory=self.get_working_directory(document),
)
restore_dependencies = None
if restore_dependencies_document.content is not None:
restore_dependencies = restore_dependencies_document
restore_dependencies.content = get_file_content(MAVEN_DEP_TREE_FILE_NAME)
if restore_content is None:
return None

return restore_dependencies
restore_file_path = build_dep_tree_path(document.absolute_path, MAVEN_DEP_TREE_FILE_NAME)
return Document(
path=build_dep_tree_path(document.path, MAVEN_DEP_TREE_FILE_NAME),
content=get_file_content(restore_file_path),
is_git_diff_format=self.is_git_diff,
absolute_path=restore_file_path,
)


def create_secondary_restore_commands(manifest_file_path: str) -> list[list[str]]:
Expand Down
21 changes: 12 additions & 9 deletions cycode/cli/files_collector/sca/sca_code_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ def get_project_file_ecosystem(document: Document) -> Optional[str]:

def try_restore_dependencies(
ctx: typer.Context,
documents_to_add: dict[str, Document],
restore_dependencies: 'BaseRestoreDependencies',
document: Document,
) -> None:
) -> Optional[Document]:
if not restore_dependencies.is_project(document):
return
return None

restore_dependencies_document = restore_dependencies.restore(document)
if restore_dependencies_document is None:
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
return
return None

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

if restore_dependencies_document.path in documents_to_add:
logger.debug('Duplicate document on restore for path: %s', restore_dependencies_document.path)
else:
documents_to_add[restore_dependencies_document.path] = restore_dependencies_document
return restore_dependencies_document


def add_dependencies_tree_document(
Expand All @@ -128,7 +124,14 @@ def add_dependencies_tree_document(

for restore_dependencies in restore_dependencies_list:
for document in documents_to_scan:
try_restore_dependencies(ctx, documents_to_add, restore_dependencies, document)
restore_dependencies_document = try_restore_dependencies(ctx, restore_dependencies, document)
if restore_dependencies_document is None:
continue

if restore_dependencies_document.path in documents_to_add:
logger.debug('Duplicate document on restore for path: %s', restore_dependencies_document.path)
else:
documents_to_add[restore_dependencies_document.path] = restore_dependencies_document

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