Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cycode/cli/apps/scan/scan_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _get_file_name_from_detection(scan_type: str, raw_detection: dict) -> str:
if scan_type == consts.SECRET_SCAN_TYPE:
return _get_secret_file_name_from_detection(raw_detection)

return raw_detection['detection_details']['file_name']
return raw_detection['detection_details']['file_path']


def _get_secret_file_name_from_detection(raw_detection: dict) -> str:
Expand Down
2 changes: 1 addition & 1 deletion cycode/cli/printers/tables/sca_table_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _enrich_table_with_values(table: Table, detection: Detection) -> None:
table.add_cell(SEVERITY_COLUMN, 'N/A')

table.add_cell(REPOSITORY_COLUMN, detection_details.get('repository_name'))
table.add_file_path_cell(CODE_PROJECT_COLUMN, detection_details.get('file_name'))
table.add_file_path_cell(CODE_PROJECT_COLUMN, detection_details.get('file_path'))
table.add_cell(ECOSYSTEM_COLUMN, detection_details.get('ecosystem'))
table.add_cell(PACKAGE_COLUMN, detection_details.get('package_name'))

Expand Down
2 changes: 1 addition & 1 deletion cycode/cli/printers/utils/detection_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ def get_detection_file_path(scan_type: str, detection: 'Detection') -> Path:

return Path(file_path)

return Path(detection.detection_details.get('file_name', ''))
return Path(detection.detection_details.get('file_path', ''))
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def sort_and_group_detections(detections: list['Detection']) -> tuple[list['Dete

grouped_by_repository = __group_by(sorted_detections, 'repository_name')
for repository_group in grouped_by_repository.values():
grouped_by_code_project = __group_by(repository_group, 'file_name')
grouped_by_code_project = __group_by(repository_group, 'file_path')
for code_project_group in grouped_by_code_project.values():
grouped_by_package = __group_by(code_project_group, 'package_name')
for package_group in grouped_by_package.values():
Expand Down
47 changes: 47 additions & 0 deletions tests/cli/commands/scan/test_scan_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os

from cycode.cli.apps.scan.scan_result import _get_file_name_from_detection
from cycode.cli.consts import IAC_SCAN_TYPE, SAST_SCAN_TYPE, SCA_SCAN_TYPE, SECRET_SCAN_TYPE


def test_get_file_name_from_detection_sca_uses_file_path() -> None:
raw_detection = {
'detection_details': {
'file_name': 'package.json',
'file_path': '/repo/path/package.json',
},
}
result = _get_file_name_from_detection(SCA_SCAN_TYPE, raw_detection)
assert result == '/repo/path/package.json'


def test_get_file_name_from_detection_iac_uses_file_path() -> None:
raw_detection = {
'detection_details': {
'file_name': 'main.tf',
'file_path': '/repo/infra/main.tf',
},
}
result = _get_file_name_from_detection(IAC_SCAN_TYPE, raw_detection)
assert result == '/repo/infra/main.tf'


def test_get_file_name_from_detection_sast_uses_file_path() -> None:
raw_detection = {
'detection_details': {
'file_path': '/repo/src/app.py',
},
}
result = _get_file_name_from_detection(SAST_SCAN_TYPE, raw_detection)
assert result == '/repo/src/app.py'


def test_get_file_name_from_detection_secret_uses_file_path_and_file_name() -> None:
raw_detection = {
'detection_details': {
'file_path': '/repo/src',
'file_name': '.env',
},
}
result = _get_file_name_from_detection(SECRET_SCAN_TYPE, raw_detection)
assert result == os.path.join('/repo/src', '.env')
41 changes: 41 additions & 0 deletions tests/cli/printers/utils/test_detection_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pathlib import Path
from unittest.mock import MagicMock

from cycode.cli.consts import IAC_SCAN_TYPE, SAST_SCAN_TYPE, SCA_SCAN_TYPE, SECRET_SCAN_TYPE
from cycode.cli.printers.utils.detection_data import get_detection_file_path


def _make_detection(**details: str) -> MagicMock:
detection = MagicMock()
detection.detection_details = dict(details)
return detection


def test_get_detection_file_path_sca_uses_file_path() -> None:
detection = _make_detection(file_name='package.json', file_path='/repo/path/package.json')
result = get_detection_file_path(SCA_SCAN_TYPE, detection)
assert result == Path('/repo/path/package.json')


def test_get_detection_file_path_iac_uses_file_path() -> None:
detection = _make_detection(file_name='main.tf', file_path='/repo/infra/main.tf')
result = get_detection_file_path(IAC_SCAN_TYPE, detection)
assert result == Path('/repo/infra/main.tf')


def test_get_detection_file_path_sca_fallback_empty() -> None:
detection = _make_detection()
result = get_detection_file_path(SCA_SCAN_TYPE, detection)
assert result == Path('')


def test_get_detection_file_path_secret() -> None:
detection = _make_detection(file_path='/repo/src', file_name='.env')
result = get_detection_file_path(SECRET_SCAN_TYPE, detection)
assert result == Path('/repo/src/.env')


def test_get_detection_file_path_sast() -> None:
detection = _make_detection(file_path='repo/src/app.py')
result = get_detection_file_path(SAST_SCAN_TYPE, detection)
assert result == Path('/repo/src/app.py')