45
45
46
46
def scan_sca_pre_commit (context : click .Context ) -> None :
47
47
scan_type = context .obj ['scan_type' ]
48
- scan_parameters = get_default_scan_parameters (context )
48
+ scan_parameters = get_scan_parameters (context )
49
49
git_head_documents , pre_committed_documents = get_pre_commit_modified_documents (
50
50
context .obj ['progress_bar' ], ScanProgressBarSection .PREPARE_LOCAL_FILES
51
51
)
@@ -80,14 +80,13 @@ def scan_sca_commit_range(context: click.Context, path: str, commit_range: str)
80
80
81
81
82
82
def scan_disk_files (context : click .Context , paths : Tuple [str ]) -> None :
83
- scan_parameters = get_scan_parameters (context , paths )
84
83
scan_type = context .obj ['scan_type' ]
85
84
progress_bar = context .obj ['progress_bar' ]
86
85
87
86
try :
88
87
documents = get_relevant_documents (progress_bar , ScanProgressBarSection .PREPARE_LOCAL_FILES , scan_type , paths )
89
88
perform_pre_scan_documents_actions (context , scan_type , documents )
90
- scan_documents (context , documents , scan_parameters = scan_parameters )
89
+ scan_documents (context , documents , get_scan_parameters ( context , paths ) )
91
90
except Exception as e :
92
91
handle_scan_exception (context , e )
93
92
@@ -151,14 +150,12 @@ def _enrich_scan_result_with_data_from_detection_rules(
151
150
152
151
def _get_scan_documents_thread_func (
153
152
context : click .Context , is_git_diff : bool , is_commit_range : bool , scan_parameters : dict
154
- ) -> Tuple [ Callable [[List [Document ]], Tuple [str , CliError , LocalScanResult ]], str ]:
153
+ ) -> Callable [[List [Document ]], Tuple [str , CliError , LocalScanResult ]]:
155
154
cycode_client = context .obj ['client' ]
156
155
scan_type = context .obj ['scan_type' ]
157
156
severity_threshold = context .obj ['severity_threshold' ]
158
157
sync_option = context .obj ['sync' ]
159
158
command_scan_type = context .info_name
160
- aggregation_id = str (_generate_unique_id ())
161
- scan_parameters ['aggregation_id' ] = aggregation_id
162
159
163
160
def _scan_batch_thread_func (batch : List [Document ]) -> Tuple [str , CliError , LocalScanResult ]:
164
161
local_scan_result = error = error_message = None
@@ -227,7 +224,7 @@ def _scan_batch_thread_func(batch: List[Document]) -> Tuple[str, CliError, Local
227
224
228
225
return scan_id , error , local_scan_result
229
226
230
- return _scan_batch_thread_func , aggregation_id
227
+ return _scan_batch_thread_func
231
228
232
229
233
230
def scan_commit_range (
@@ -287,20 +284,19 @@ def scan_commit_range(
287
284
logger .debug ('List of commit ids to scan, %s' , {'commit_ids' : commit_ids_to_scan })
288
285
logger .debug ('Starting to scan commit range (it may take a few minutes)' )
289
286
290
- scan_documents (context , documents_to_scan , is_git_diff = True , is_commit_range = True )
287
+ scan_documents (
288
+ context , documents_to_scan , get_scan_parameters (context , (path ,)), is_git_diff = True , is_commit_range = True
289
+ )
291
290
return None
292
291
293
292
294
293
def scan_documents (
295
294
context : click .Context ,
296
295
documents_to_scan : List [Document ],
296
+ scan_parameters : dict ,
297
297
is_git_diff : bool = False ,
298
298
is_commit_range : bool = False ,
299
- scan_parameters : Optional [dict ] = None ,
300
299
) -> None :
301
- if not scan_parameters :
302
- scan_parameters = get_default_scan_parameters (context )
303
-
304
300
scan_type = context .obj ['scan_type' ]
305
301
progress_bar = context .obj ['progress_bar' ]
306
302
@@ -315,19 +311,15 @@ def scan_documents(
315
311
)
316
312
return
317
313
318
- scan_batch_thread_func , aggregation_id = _get_scan_documents_thread_func (
319
- context , is_git_diff , is_commit_range , scan_parameters
320
- )
314
+ scan_batch_thread_func = _get_scan_documents_thread_func (context , is_git_diff , is_commit_range , scan_parameters )
321
315
errors , local_scan_results = run_parallel_batched_scan (
322
316
scan_batch_thread_func , scan_type , documents_to_scan , progress_bar = progress_bar
323
317
)
324
318
325
- if len (local_scan_results ) > 1 :
326
- # if we used more than one batch, we need to fetch aggregate report url
327
- aggregation_report_url = _try_get_aggregation_report_url_if_needed (
328
- scan_parameters , context .obj ['client' ], scan_type
329
- )
330
- set_aggregation_report_url (context , aggregation_report_url )
319
+ aggregation_report_url = _try_get_aggregation_report_url_if_needed (
320
+ scan_parameters , context .obj ['client' ], scan_type
321
+ )
322
+ _set_aggregation_report_url (context , aggregation_report_url )
331
323
332
324
progress_bar .set_section_length (ScanProgressBarSection .GENERATE_REPORT , 1 )
333
325
progress_bar .update (ScanProgressBarSection .GENERATE_REPORT )
@@ -337,25 +329,6 @@ def scan_documents(
337
329
print_results (context , local_scan_results , errors )
338
330
339
331
340
- def set_aggregation_report_url (context : click .Context , aggregation_report_url : Optional [str ] = None ) -> None :
341
- context .obj ['aggregation_report_url' ] = aggregation_report_url
342
-
343
-
344
- def _try_get_aggregation_report_url_if_needed (
345
- scan_parameters : dict , cycode_client : 'ScanClient' , scan_type : str
346
- ) -> Optional [str ]:
347
- aggregation_id = scan_parameters .get ('aggregation_id' )
348
- if not scan_parameters .get ('report' ):
349
- return None
350
- if aggregation_id is None :
351
- return None
352
- try :
353
- report_url_response = cycode_client .get_scan_aggregation_report_url (aggregation_id , scan_type )
354
- return report_url_response .report_url
355
- except Exception as e :
356
- logger .debug ('Failed to get aggregation report url: %s' , str (e ))
357
-
358
-
359
332
def scan_commit_range_documents (
360
333
context : click .Context ,
361
334
from_documents_to_scan : List [Document ],
@@ -380,7 +353,7 @@ def scan_commit_range_documents(
380
353
try :
381
354
progress_bar .set_section_length (ScanProgressBarSection .SCAN , 1 )
382
355
383
- scan_result = init_default_scan_result (cycode_client , scan_id , scan_type )
356
+ scan_result = init_default_scan_result (scan_id )
384
357
if should_scan_documents (from_documents_to_scan , to_documents_to_scan ):
385
358
logger .debug ('Preparing from-commit zip' )
386
359
from_commit_zipped_documents = zip_documents (scan_type , from_documents_to_scan )
@@ -518,7 +491,7 @@ def perform_scan_async(
518
491
cycode_client ,
519
492
scan_async_result .scan_id ,
520
493
scan_type ,
521
- scan_parameters . get ( 'report' ) ,
494
+ scan_parameters ,
522
495
)
523
496
524
497
@@ -553,16 +526,14 @@ def perform_commit_range_scan_async(
553
526
logger .debug (
554
527
'Async commit range scan request has been triggered successfully, %s' , {'scan_id' : scan_async_result .scan_id }
555
528
)
556
- return poll_scan_results (
557
- cycode_client , scan_async_result .scan_id , scan_type , scan_parameters .get ('report' ), timeout
558
- )
529
+ return poll_scan_results (cycode_client , scan_async_result .scan_id , scan_type , scan_parameters , timeout )
559
530
560
531
561
532
def poll_scan_results (
562
533
cycode_client : 'ScanClient' ,
563
534
scan_id : str ,
564
535
scan_type : str ,
565
- should_get_report : bool = False ,
536
+ scan_parameters : dict ,
566
537
polling_timeout : Optional [int ] = None ,
567
538
) -> ZippedFileScanResult :
568
539
if polling_timeout is None :
@@ -579,7 +550,7 @@ def poll_scan_results(
579
550
print_debug_scan_details (scan_details )
580
551
581
552
if scan_details .scan_status == consts .SCAN_STATUS_COMPLETED :
582
- return _get_scan_result (cycode_client , scan_type , scan_id , scan_details , should_get_report )
553
+ return _get_scan_result (cycode_client , scan_type , scan_id , scan_details , scan_parameters )
583
554
584
555
if scan_details .scan_status == consts .SCAN_STATUS_ERROR :
585
556
raise custom_exceptions .ScanAsyncError (
@@ -671,18 +642,19 @@ def parse_pre_receive_input() -> str:
671
642
return pre_receive_input .splitlines ()[0 ]
672
643
673
644
674
- def get_default_scan_parameters (context : click .Context ) -> dict :
645
+ def _get_default_scan_parameters (context : click .Context ) -> dict :
675
646
return {
676
647
'monitor' : context .obj .get ('monitor' ),
677
648
'report' : context .obj .get ('report' ),
678
649
'package_vulnerabilities' : context .obj .get ('package-vulnerabilities' ),
679
650
'license_compliance' : context .obj .get ('license-compliance' ),
680
651
'command_type' : context .info_name ,
652
+ 'aggregation_id' : str (_generate_unique_id ()),
681
653
}
682
654
683
655
684
- def get_scan_parameters (context : click .Context , paths : Tuple [str ]) -> dict :
685
- scan_parameters = get_default_scan_parameters (context )
656
+ def get_scan_parameters (context : click .Context , paths : Optional [ Tuple [str ]] = None ) -> dict :
657
+ scan_parameters = _get_default_scan_parameters (context )
686
658
687
659
if not paths :
688
660
return scan_parameters
@@ -890,36 +862,51 @@ def _get_scan_result(
890
862
scan_type : str ,
891
863
scan_id : str ,
892
864
scan_details : 'ScanDetailsResponse' ,
893
- should_get_report : bool = False ,
865
+ scan_parameters : dict ,
894
866
) -> ZippedFileScanResult :
895
867
if not scan_details .detections_count :
896
- return init_default_scan_result (cycode_client , scan_id , scan_type , should_get_report )
868
+ return init_default_scan_result (scan_id )
897
869
898
870
scan_raw_detections = cycode_client .get_scan_raw_detections (scan_type , scan_id )
899
871
900
872
return ZippedFileScanResult (
901
873
did_detect = True ,
902
874
detections_per_file = _map_detections_per_file_and_commit_id (scan_type , scan_raw_detections ),
903
875
scan_id = scan_id ,
904
- report_url = _try_get_report_url_if_needed (cycode_client , should_get_report , scan_id , scan_type ),
876
+ report_url = _try_get_any_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters ),
905
877
)
906
878
907
879
908
- def init_default_scan_result (
909
- cycode_client : 'ScanClient' , scan_id : str , scan_type : str , should_get_report : bool = False
910
- ) -> ZippedFileScanResult :
880
+ def init_default_scan_result (scan_id : str ) -> ZippedFileScanResult :
911
881
return ZippedFileScanResult (
912
882
did_detect = False ,
913
883
detections_per_file = [],
914
884
scan_id = scan_id ,
915
- report_url = _try_get_report_url_if_needed (cycode_client , should_get_report , scan_id , scan_type ),
916
885
)
917
886
918
887
888
+ def _try_get_any_report_url_if_needed (
889
+ cycode_client : 'ScanClient' ,
890
+ scan_id : str ,
891
+ scan_type : str ,
892
+ scan_parameters : dict ,
893
+ ) -> Optional [str ]:
894
+ """Tries to get aggregation report URL if needed, otherwise tries to get report URL."""
895
+ aggregation_report_url = None
896
+ if scan_parameters :
897
+ _try_get_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters )
898
+ aggregation_report_url = _try_get_aggregation_report_url_if_needed (scan_parameters , cycode_client , scan_type )
899
+
900
+ if aggregation_report_url :
901
+ return aggregation_report_url
902
+
903
+ return _try_get_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters )
904
+
905
+
919
906
def _try_get_report_url_if_needed (
920
- cycode_client : 'ScanClient' , should_get_report : bool , scan_id : str , scan_type : str
907
+ cycode_client : 'ScanClient' , scan_id : str , scan_type : str , scan_parameters : dict
921
908
) -> Optional [str ]:
922
- if not should_get_report :
909
+ if not scan_parameters . get ( 'report' , False ) :
923
910
return None
924
911
925
912
try :
@@ -929,6 +916,27 @@ def _try_get_report_url_if_needed(
929
916
logger .debug ('Failed to get report URL' , exc_info = e )
930
917
931
918
919
+ def _set_aggregation_report_url (context : click .Context , aggregation_report_url : Optional [str ] = None ) -> None :
920
+ context .obj ['aggregation_report_url' ] = aggregation_report_url
921
+
922
+
923
+ def _try_get_aggregation_report_url_if_needed (
924
+ scan_parameters : dict , cycode_client : 'ScanClient' , scan_type : str
925
+ ) -> Optional [str ]:
926
+ if not scan_parameters .get ('report' , False ):
927
+ return None
928
+
929
+ aggregation_id = scan_parameters .get ('aggregation_id' )
930
+ if aggregation_id is None :
931
+ return None
932
+
933
+ try :
934
+ report_url_response = cycode_client .get_scan_aggregation_report_url (aggregation_id , scan_type )
935
+ return report_url_response .report_url
936
+ except Exception as e :
937
+ logger .debug ('Failed to get aggregation report url: %s' , str (e ))
938
+
939
+
932
940
def _map_detections_per_file_and_commit_id (scan_type : str , raw_detections : List [dict ]) -> List [DetectionsPerFile ]:
933
941
"""Converts list of detections (async flow) to list of DetectionsPerFile objects (sync flow).
934
942
0 commit comments