Skip to content
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

CM-40908 - Implement NuGet restore support for SCA #252

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def execute_command(command: List[str], file_name: str, command_timeout: int) ->
return dependencies


class BaseRestoreMavenDependencies(ABC):
class BaseRestoreDependencies(ABC):
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int) -> None:
self.context = context
self.is_git_diff = is_git_diff
Expand Down Expand Up @@ -67,3 +67,4 @@ def get_command(self, manifest_file_path: str) -> List[str]:
@abstractmethod
def get_lock_file_name(self) -> str:
pass

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import click

from cycode.cli.files_collector.sca.maven.base_restore_maven_dependencies import BaseRestoreMavenDependencies
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
from cycode.cli.models import Document

BUILD_GRADLE_FILE_NAME = 'build.gradle'
BUILD_GRADLE_KTS_FILE_NAME = 'build.gradle.kts'
BUILD_GRADLE_DEP_TREE_FILE_NAME = 'gradle-dependencies-generated.txt'


class RestoreGradleDependencies(BaseRestoreMavenDependencies):
class RestoreGradleDependencies(BaseRestoreDependencies):
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int) -> None:
super().__init__(context, is_git_diff, command_timeout)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import click

from cycode.cli.files_collector.sca.maven.base_restore_maven_dependencies import (
BaseRestoreMavenDependencies,
from cycode.cli.files_collector.sca.base_restore_dependencies import (
BaseRestoreDependencies,
build_dep_tree_path,
execute_command,
)
Expand All @@ -16,7 +16,7 @@
MAVEN_DEP_TREE_FILE_NAME = 'bcde.mvndeps'


class RestoreMavenDependencies(BaseRestoreMavenDependencies):
class RestoreMavenDependencies(BaseRestoreDependencies):
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int) -> None:
super().__init__(context, is_git_diff, command_timeout)

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import shutil

Check failure on line 2 in cycode/cli/files_collector/sca/nuget/restore_nuget_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

cycode/cli/files_collector/sca/nuget/restore_nuget_dependencies.py:2:8: F401 `shutil` imported but unused
from typing import List, Optional

Check failure on line 3 in cycode/cli/files_collector/sca/nuget/restore_nuget_dependencies.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

cycode/cli/files_collector/sca/nuget/restore_nuget_dependencies.py:3:26: F401 `typing.Optional` imported but unused

import click

from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
from cycode.cli.models import Document

NUGET_PROJECT_FILE_EXTENSIONS = ['.csproj', '.vbproj']
NUGET_LOCK_FILE_NAME = 'packages.lock.json'


class RestoreNugetDependencies(BaseRestoreDependencies):
def __init__(self, context: click.Context, is_git_diff: bool, command_timeout: int) -> None:
super().__init__(context, is_git_diff, command_timeout)

def is_project(self, document: Document) -> bool:
return any(document.path.endswith(ext) for ext in NUGET_PROJECT_FILE_EXTENSIONS)

def get_command(self, manifest_file_path: str) -> List[str]:
return ['dotnet', 'restore', manifest_file_path, '--use-lock-file', '--verbosity', 'quiet']

def get_lock_file_name(self) -> str:
return NUGET_LOCK_FILE_NAME

def verify_restore_file_already_exist(self, restore_file_path: str) -> bool:
return os.path.isfile(restore_file_path)


20 changes: 12 additions & 8 deletions cycode/cli/files_collector/sca/sca_code_scanner.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import os
from typing import TYPE_CHECKING, Dict, List, Optional

import click

from cycode.cli import consts
from cycode.cli.files_collector.sca.maven.restore_gradle_dependencies import RestoreGradleDependencies
from cycode.cli.files_collector.sca.maven.restore_maven_dependencies import RestoreMavenDependencies
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
from cycode.cli.files_collector.sca.nuget.restore_nuget_dependencies import RestoreNugetDependencies
from cycode.cli.models import Document
from cycode.cli.utils.git_proxy import git_proxy
from cycode.cli.utils.path_utils import get_file_content, get_file_dir, join_paths
from cycode.cyclient import logger

if TYPE_CHECKING:

Check failure on line 16 in cycode/cli/files_collector/sca/sca_code_scanner.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

cycode/cli/files_collector/sca/sca_code_scanner.py:1:1: I001 Import block is un-sorted or un-formatted
from git import Repo

from cycode.cli.files_collector.sca.maven.base_restore_maven_dependencies import BaseRestoreMavenDependencies
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies

Check failure on line 19 in cycode/cli/files_collector/sca/sca_code_scanner.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F811)

cycode/cli/files_collector/sca/sca_code_scanner.py:19:74: F811 Redefinition of unused `BaseRestoreDependencies` from line 9

Check failure on line 19 in cycode/cli/files_collector/sca/sca_code_scanner.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (TCH004)

cycode/cli/files_collector/sca/sca_code_scanner.py:19:74: TCH004 Move import `cycode.cli.files_collector.sca.base_restore_dependencies.BaseRestoreDependencies` out of type-checking block. Import is used for more than type hinting.

BUILD_GRADLE_FILE_NAME = 'build.gradle'
BUILD_GRADLE_KTS_FILE_NAME = 'build.gradle.kts'
BUILD_GRADLE_DEP_TREE_FILE_NAME = 'gradle-dependencies-generated.txt'
BUILD_GRADLE_DEP_TREE_TIMEOUT = 180

BUILD_NUGET_DEP_TREE_TIMEOUT = 180

def perform_pre_commit_range_scan_actions(
path: str,
Expand Down Expand Up @@ -90,7 +89,7 @@
def try_restore_dependencies(
context: click.Context,
documents_to_add: Dict[str, Document],
restore_dependencies: 'BaseRestoreMavenDependencies',
restore_dependencies: 'BaseRestoreDependencies',
document: Document,
) -> None:
if restore_dependencies.is_project(document):
Expand All @@ -104,7 +103,11 @@
restore_dependencies_document.content = ''
else:
is_monitor_action = context.obj['monitor']
project_path = context.params['paths'][0]

project_path = context.params.get('paths', [None])
if project_path is None:
project_path = context.params['path'][0]
MarshalX marked this conversation as resolved.
Show resolved Hide resolved

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)

Expand All @@ -127,10 +130,11 @@
documents_to_scan.extend(list(documents_to_add.values()))


def restore_handlers(context: click.Context, is_git_diff: bool) -> List[RestoreGradleDependencies]:
def restore_handlers(context: click.Context, is_git_diff: bool) -> List[BaseRestoreDependencies]:
return [
RestoreGradleDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
RestoreMavenDependencies(context, is_git_diff, BUILD_GRADLE_DEP_TREE_TIMEOUT),
RestoreNugetDependencies(context, is_git_diff, BUILD_NUGET_DEP_TREE_TIMEOUT),
]


Expand Down
Loading