Skip to content

Commit 2f2759b

Browse files
authored
CM-42882 - Fix SCA table printing (severity weights) (#273)
1 parent d292487 commit 2f2759b

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

cycode/cli/commands/scan/code_scanner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: str,
631634
) -> List[DocumentDetections]:
632635
relevant_document_detections_list = []
633636
for document_detections in document_detections_list:
@@ -717,9 +720,6 @@ def exclude_irrelevant_detections(
717720

718721

719722
def _exclude_detections_by_severity(detections: List[Detection], severity_threshold: str) -> List[Detection]:
720-
if severity_threshold is None:
721-
return detections
722-
723723
relevant_detections = []
724724
for detection in detections:
725725
severity = detection.detection_details.get('advisory_severity')

cycode/cli/commands/scan/scan_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
)
6666
@click.option(
6767
'--severity-threshold',
68-
default=None,
68+
default=Severity.INFO.name,
6969
help='Show violations only for the specified level or higher.',
7070
type=click.Choice([e.name for e in Severity]),
7171
required=False,

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)