4949
5050def scan_sca_pre_commit (ctx : typer .Context ) -> None :
5151 scan_type = ctx .obj ['scan_type' ]
52- scan_parameters = get_default_scan_parameters (ctx )
52+ scan_parameters = get_scan_parameters (ctx )
5353 git_head_documents , pre_committed_documents = get_pre_commit_modified_documents (
5454 ctx .obj ['progress_bar' ], ScanProgressBarSection .PREPARE_LOCAL_FILES
5555 )
@@ -83,15 +83,14 @@ def scan_sca_commit_range(ctx: typer.Context, path: str, commit_range: str) -> N
8383 scan_commit_range_documents (ctx , from_commit_documents , to_commit_documents , scan_parameters = scan_parameters )
8484
8585
86- def scan_disk_files (ctx : typer .Context , paths : Tuple [str , ...]) -> None :
87- scan_parameters = get_scan_parameters (ctx , paths )
86+ def scan_disk_files (ctx : click .Context , paths : Tuple [str ]) -> None :
8887 scan_type = ctx .obj ['scan_type' ]
8988 progress_bar = ctx .obj ['progress_bar' ]
9089
9190 try :
9291 documents = get_relevant_documents (progress_bar , ScanProgressBarSection .PREPARE_LOCAL_FILES , scan_type , paths )
9392 perform_pre_scan_documents_actions (ctx , scan_type , documents )
94- scan_documents (ctx , documents , scan_parameters = scan_parameters )
93+ scan_documents (ctx , documents , get_scan_parameters ( ctx , paths ) )
9594 except Exception as e :
9695 handle_scan_exception (ctx , e )
9796
@@ -154,15 +153,13 @@ def _enrich_scan_result_with_data_from_detection_rules(
154153
155154
156155def _get_scan_documents_thread_func (
157- ctx : typer .Context , is_git_diff : bool , is_commit_range : bool , scan_parameters : dict
158- ) -> Tuple [ Callable [[List [Document ]], Tuple [str , CliError , LocalScanResult ]], str ]:
156+ ctx : typer .Context , is_git_diff : bool , is_commit_range : bool , scan_parameters : dict
157+ ) -> Callable [[List [Document ]], Tuple [str , CliError , LocalScanResult ]]:
159158 cycode_client = ctx .obj ['client' ]
160159 scan_type = ctx .obj ['scan_type' ]
161160 severity_threshold = ctx .obj ['severity_threshold' ]
162161 sync_option = ctx .obj ['sync' ]
163162 command_scan_type = ctx .info_name
164- aggregation_id = str (_generate_unique_id ())
165- scan_parameters ['aggregation_id' ] = aggregation_id
166163
167164 def _scan_batch_thread_func (batch : List [Document ]) -> Tuple [str , CliError , LocalScanResult ]:
168165 local_scan_result = error = error_message = None
@@ -231,7 +228,7 @@ def _scan_batch_thread_func(batch: List[Document]) -> Tuple[str, CliError, Local
231228
232229 return scan_id , error , local_scan_result
233230
234- return _scan_batch_thread_func , aggregation_id
231+ return _scan_batch_thread_func
235232
236233
237234def scan_commit_range (
@@ -291,20 +288,19 @@ def scan_commit_range(
291288 logger .debug ('List of commit ids to scan, %s' , {'commit_ids' : commit_ids_to_scan })
292289 logger .debug ('Starting to scan commit range (it may take a few minutes)' )
293290
294- scan_documents (ctx , documents_to_scan , is_git_diff = True , is_commit_range = True )
291+ scan_documents (
292+ ctx , documents_to_scan , get_scan_parameters (context , (path ,)), is_git_diff = True , is_commit_range = True
293+ )
295294 return None
296295
297296
298297def scan_documents (
299298 ctx : typer .Context ,
300299 documents_to_scan : List [Document ],
300+ scan_parameters : dict ,
301301 is_git_diff : bool = False ,
302302 is_commit_range : bool = False ,
303- scan_parameters : Optional [dict ] = None ,
304303) -> None :
305- if not scan_parameters :
306- scan_parameters = get_default_scan_parameters (ctx )
307-
308304 scan_type = ctx .obj ['scan_type' ]
309305 progress_bar = ctx .obj ['progress_bar' ]
310306
@@ -319,19 +315,15 @@ def scan_documents(
319315 )
320316 return
321317
322- scan_batch_thread_func , aggregation_id = _get_scan_documents_thread_func (
323- ctx , is_git_diff , is_commit_range , scan_parameters
324- )
318+ scan_batch_thread_func = _get_scan_documents_thread_func (context , is_git_diff , is_commit_range , scan_parameters )
325319 errors , local_scan_results = run_parallel_batched_scan (
326320 scan_batch_thread_func , scan_type , documents_to_scan , progress_bar = progress_bar
327321 )
328322
329- if len (local_scan_results ) > 1 :
330- # if we used more than one batch, we need to fetch aggregate report url
331- aggregation_report_url = _try_get_aggregation_report_url_if_needed (
332- scan_parameters , ctx .obj ['client' ], scan_type
333- )
334- set_aggregation_report_url (ctx , aggregation_report_url )
323+ aggregation_report_url = _try_get_aggregation_report_url_if_needed (
324+ scan_parameters , context .obj ['client' ], scan_type
325+ )
326+ _set_aggregation_report_url (context , aggregation_report_url )
335327
336328 progress_bar .set_section_length (ScanProgressBarSection .GENERATE_REPORT , 1 )
337329 progress_bar .update (ScanProgressBarSection .GENERATE_REPORT )
@@ -341,25 +333,6 @@ def scan_documents(
341333 print_results (ctx , local_scan_results , errors )
342334
343335
344- def set_aggregation_report_url (ctx : typer .Context , aggregation_report_url : Optional [str ] = None ) -> None :
345- ctx .obj ['aggregation_report_url' ] = aggregation_report_url
346-
347-
348- def _try_get_aggregation_report_url_if_needed (
349- scan_parameters : dict , cycode_client : 'ScanClient' , scan_type : str
350- ) -> Optional [str ]:
351- aggregation_id = scan_parameters .get ('aggregation_id' )
352- if not scan_parameters .get ('report' ):
353- return None
354- if aggregation_id is None :
355- return None
356- try :
357- report_url_response = cycode_client .get_scan_aggregation_report_url (aggregation_id , scan_type )
358- return report_url_response .report_url
359- except Exception as e :
360- logger .debug ('Failed to get aggregation report url: %s' , str (e ))
361-
362-
363336def scan_commit_range_documents (
364337 ctx : typer .Context ,
365338 from_documents_to_scan : List [Document ],
@@ -384,7 +357,7 @@ def scan_commit_range_documents(
384357 try :
385358 progress_bar .set_section_length (ScanProgressBarSection .SCAN , 1 )
386359
387- scan_result = init_default_scan_result (cycode_client , scan_id , scan_type )
360+ scan_result = init_default_scan_result (scan_id )
388361 if should_scan_documents (from_documents_to_scan , to_documents_to_scan ):
389362 logger .debug ('Preparing from-commit zip' )
390363 from_commit_zipped_documents = zip_documents (scan_type , from_documents_to_scan )
@@ -522,7 +495,7 @@ def perform_scan_async(
522495 cycode_client ,
523496 scan_async_result .scan_id ,
524497 scan_type ,
525- scan_parameters . get ( 'report' ) ,
498+ scan_parameters ,
526499 )
527500
528501
@@ -557,16 +530,14 @@ def perform_commit_range_scan_async(
557530 logger .debug (
558531 'Async commit range scan request has been triggered successfully, %s' , {'scan_id' : scan_async_result .scan_id }
559532 )
560- return poll_scan_results (
561- cycode_client , scan_async_result .scan_id , scan_type , scan_parameters .get ('report' ), timeout
562- )
533+ return poll_scan_results (cycode_client , scan_async_result .scan_id , scan_type , scan_parameters , timeout )
563534
564535
565536def poll_scan_results (
566537 cycode_client : 'ScanClient' ,
567538 scan_id : str ,
568539 scan_type : str ,
569- should_get_report : bool = False ,
540+ scan_parameters : dict ,
570541 polling_timeout : Optional [int ] = None ,
571542) -> ZippedFileScanResult :
572543 if polling_timeout is None :
@@ -583,7 +554,7 @@ def poll_scan_results(
583554 print_debug_scan_details (scan_details )
584555
585556 if scan_details .scan_status == consts .SCAN_STATUS_COMPLETED :
586- return _get_scan_result (cycode_client , scan_type , scan_id , scan_details , should_get_report )
557+ return _get_scan_result (cycode_client , scan_type , scan_id , scan_details , scan_parameters )
587558
588559 if scan_details .scan_status == consts .SCAN_STATUS_ERROR :
589560 raise custom_exceptions .ScanAsyncError (
@@ -675,18 +646,19 @@ def parse_pre_receive_input() -> str:
675646 return pre_receive_input .splitlines ()[0 ]
676647
677648
678- def get_default_scan_parameters (ctx : typer .Context ) -> dict :
649+ def _get_default_scan_parameters (ctx : click .Context ) -> dict :
679650 return {
680651 'monitor' : ctx .obj .get ('monitor' ),
681652 'report' : ctx .obj .get ('report' ),
682653 'package_vulnerabilities' : ctx .obj .get ('package-vulnerabilities' ),
683654 'license_compliance' : ctx .obj .get ('license-compliance' ),
684655 'command_type' : ctx .info_name ,
656+ 'aggregation_id' : str (_generate_unique_id ()),
685657 }
686658
687659
688- def get_scan_parameters (ctx : typer .Context , paths : Tuple [str , ...] ) -> dict :
689- scan_parameters = get_default_scan_parameters (ctx )
660+ def get_scan_parameters (ctx : typer .Context , paths : Optional [ Tuple [str ]] = None ) -> dict :
661+ scan_parameters = _get_default_scan_parameters (ctx )
690662
691663 if not paths :
692664 return scan_parameters
@@ -894,36 +866,51 @@ def _get_scan_result(
894866 scan_type : str ,
895867 scan_id : str ,
896868 scan_details : 'ScanDetailsResponse' ,
897- should_get_report : bool = False ,
869+ scan_parameters : dict ,
898870) -> ZippedFileScanResult :
899871 if not scan_details .detections_count :
900- return init_default_scan_result (cycode_client , scan_id , scan_type , should_get_report )
872+ return init_default_scan_result (scan_id )
901873
902874 scan_raw_detections = cycode_client .get_scan_raw_detections (scan_type , scan_id )
903875
904876 return ZippedFileScanResult (
905877 did_detect = True ,
906878 detections_per_file = _map_detections_per_file_and_commit_id (scan_type , scan_raw_detections ),
907879 scan_id = scan_id ,
908- report_url = _try_get_report_url_if_needed (cycode_client , should_get_report , scan_id , scan_type ),
880+ report_url = _try_get_any_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters ),
909881 )
910882
911883
912- def init_default_scan_result (
913- cycode_client : 'ScanClient' , scan_id : str , scan_type : str , should_get_report : bool = False
914- ) -> ZippedFileScanResult :
884+ def init_default_scan_result (scan_id : str ) -> ZippedFileScanResult :
915885 return ZippedFileScanResult (
916886 did_detect = False ,
917887 detections_per_file = [],
918888 scan_id = scan_id ,
919- report_url = _try_get_report_url_if_needed (cycode_client , should_get_report , scan_id , scan_type ),
920889 )
921890
922891
892+ def _try_get_any_report_url_if_needed (
893+ cycode_client : 'ScanClient' ,
894+ scan_id : str ,
895+ scan_type : str ,
896+ scan_parameters : dict ,
897+ ) -> Optional [str ]:
898+ """Tries to get aggregation report URL if needed, otherwise tries to get report URL."""
899+ aggregation_report_url = None
900+ if scan_parameters :
901+ _try_get_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters )
902+ aggregation_report_url = _try_get_aggregation_report_url_if_needed (scan_parameters , cycode_client , scan_type )
903+
904+ if aggregation_report_url :
905+ return aggregation_report_url
906+
907+ return _try_get_report_url_if_needed (cycode_client , scan_id , scan_type , scan_parameters )
908+
909+
923910def _try_get_report_url_if_needed (
924- cycode_client : 'ScanClient' , should_get_report : bool , scan_id : str , scan_type : str
911+ cycode_client : 'ScanClient' , scan_id : str , scan_type : str , scan_parameters : dict
925912) -> Optional [str ]:
926- if not should_get_report :
913+ if not scan_parameters . get ( 'report' , False ) :
927914 return None
928915
929916 try :
@@ -933,6 +920,27 @@ def _try_get_report_url_if_needed(
933920 logger .debug ('Failed to get report URL' , exc_info = e )
934921
935922
923+ def _set_aggregation_report_url (context : click .Context , aggregation_report_url : Optional [str ] = None ) -> None :
924+ context .obj ['aggregation_report_url' ] = aggregation_report_url
925+
926+
927+ def _try_get_aggregation_report_url_if_needed (
928+ scan_parameters : dict , cycode_client : 'ScanClient' , scan_type : str
929+ ) -> Optional [str ]:
930+ if not scan_parameters .get ('report' , False ):
931+ return None
932+
933+ aggregation_id = scan_parameters .get ('aggregation_id' )
934+ if aggregation_id is None :
935+ return None
936+
937+ try :
938+ report_url_response = cycode_client .get_scan_aggregation_report_url (aggregation_id , scan_type )
939+ return report_url_response .report_url
940+ except Exception as e :
941+ logger .debug ('Failed to get aggregation report url: %s' , str (e ))
942+
943+
936944def _map_detections_per_file_and_commit_id (scan_type : str , raw_detections : List [dict ]) -> List [DetectionsPerFile ]:
937945 """Converts list of detections (async flow) to list of DetectionsPerFile objects (sync flow).
938946
0 commit comments