Skip to content

Commit 4634765

Browse files
committed
CM-42882 - Fix SCA table printing (severity weights)
1 parent d292487 commit 4634765

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

cycode/cli/commands/scan/code_scanner.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def create_local_scan_result(
455455
documents_to_scan: List[Document],
456456
command_scan_type: str,
457457
scan_type: str,
458-
severity_threshold: str,
458+
severity_threshold: Optional[str],
459459
) -> LocalScanResult:
460460
document_detections = get_document_detections(scan_result, documents_to_scan)
461461
relevant_document_detections_list = exclude_irrelevant_document_detections(
@@ -627,7 +627,10 @@ def get_document_detections(
627627

628628

629629
def exclude_irrelevant_document_detections(
630-
document_detections_list: List[DocumentDetections], scan_type: str, command_scan_type: str, severity_threshold: str
630+
document_detections_list: List[DocumentDetections],
631+
scan_type: str,
632+
command_scan_type: str,
633+
severity_threshold: Optional[str],
631634
) -> List[DocumentDetections]:
632635
relevant_document_detections_list = []
633636
for document_detections in document_detections_list:
@@ -709,17 +712,18 @@ def try_get_git_remote_url(path: str) -> Optional[str]:
709712

710713

711714
def exclude_irrelevant_detections(
712-
detections: List[Detection], scan_type: str, command_scan_type: str, severity_threshold: str
715+
detections: List[Detection], scan_type: str, command_scan_type: str, severity_threshold: Optional[str]
713716
) -> List[Detection]:
714717
relevant_detections = _exclude_detections_by_exclusions_configuration(detections, scan_type)
715718
relevant_detections = _exclude_detections_by_scan_type(relevant_detections, scan_type, command_scan_type)
716-
return _exclude_detections_by_severity(relevant_detections, severity_threshold)
717719

720+
if severity_threshold:
721+
return _exclude_detections_by_severity(relevant_detections, severity_threshold)
722+
723+
return relevant_detections
718724

719-
def _exclude_detections_by_severity(detections: List[Detection], severity_threshold: str) -> List[Detection]:
720-
if severity_threshold is None:
721-
return detections
722725

726+
def _exclude_detections_by_severity(detections: List[Detection], severity_threshold: str) -> List[Detection]:
723727
relevant_detections = []
724728
for detection in detections:
725729
severity = detection.detection_details.get('advisory_severity')

cycode/cli/commands/scan/scan_command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import List
2+
from typing import List, Optional
33

44
import click
55

@@ -117,7 +117,7 @@ def scan_command(
117117
client_id: str,
118118
show_secret: bool,
119119
soft_fail: bool,
120-
severity_threshold: str,
120+
severity_threshold: Optional[str],
121121
sca_scan: List[str],
122122
monitor: bool,
123123
report: bool,

cycode/cli/models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def __repr__(self) -> str:
3333
return 'document:{0}, detections:{1}'.format(self.document, self.detections)
3434

3535

36+
SEVERITY_UNKNOWN_WEIGHT = -2
37+
38+
3639
class Severity(Enum):
3740
INFO = -1
3841
LOW = 0
@@ -42,18 +45,19 @@ class Severity(Enum):
4245
CRITICAL = 3
4346

4447
@staticmethod
45-
def try_get_value(name: str) -> any:
48+
def try_get_value(name: str) -> Optional[int]:
4649
name = name.upper()
4750
if name not in Severity.__members__:
4851
return None
4952

5053
return Severity[name].value
5154

5255
@staticmethod
53-
def get_member_weight(name: str) -> any:
56+
def get_member_weight(name: str) -> int:
5457
weight = Severity.try_get_value(name)
55-
if weight is None: # if License Compliance
56-
return -2
58+
if weight is None: # unknown severity
59+
return SEVERITY_UNKNOWN_WEIGHT
60+
5761
return weight
5862

5963

cycode/cli/printers/tables/sca_table_printer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import click
55

66
from cycode.cli.consts import LICENSE_COMPLIANCE_POLICY_ID, PACKAGE_VULNERABILITY_POLICY_ID
7-
from cycode.cli.models import Detection, Severity
7+
from cycode.cli.models import SEVERITY_UNKNOWN_WEIGHT, Detection, Severity
88
from cycode.cli.printers.tables.table import Table
99
from cycode.cli.printers.tables.table_models import ColumnInfoBuilder, ColumnWidths
1010
from cycode.cli.printers.tables.table_printer_base import TablePrinterBase
@@ -73,7 +73,10 @@ def __group_by(detections: List[Detection], details_field_name: str) -> Dict[str
7373
@staticmethod
7474
def __severity_sort_key(detection: Detection) -> int:
7575
severity = detection.detection_details.get('advisory_severity')
76-
return Severity.get_member_weight(severity)
76+
if severity:
77+
return Severity.get_member_weight(severity)
78+
79+
return SEVERITY_UNKNOWN_WEIGHT
7780

7881
def _sort_detections_by_severity(self, detections: List[Detection]) -> List[Detection]:
7982
return sorted(detections, key=self.__severity_sort_key, reverse=True)

0 commit comments

Comments
 (0)