66import  traceback 
77from  platform  import  platform 
88from  sys  import  getsizeof 
9- from  typing  import  TYPE_CHECKING , Callable , Dict , List , Optional , Tuple 
9+ from  typing  import  TYPE_CHECKING , Callable , Dict , Iterator ,  List , Optional , Tuple ,  Union 
1010from  uuid  import  UUID , uuid4 
1111
1212import  click 
4040from  cycode .cyclient .models  import  Detection , DetectionSchema , DetectionsPerFile , ZippedFileScanResult 
4141
4242if  TYPE_CHECKING :
43+     from  git  import  Blob , Diff 
44+     from  git .objects .base  import  IndexObjUnion 
45+     from  git .objects .tree  import  TraversedTreeTup 
46+ 
4347    from  cycode .cli .utils .progress_bar  import  BaseProgressBar 
4448    from  cycode .cyclient .models  import  ScanDetailsResponse 
4549    from  cycode .cyclient .scan_client  import  ScanClient 
5862    required = False , 
5963) 
6064@click .pass_context  
61- def  scan_repository (context : click .Context , path : str , branch : str ):
65+ def  scan_repository (context : click .Context , path : str , branch : str )  ->   None :
6266    try :
6367        logger .debug ('Starting repository scan process, %s' , {'path' : path , 'branch' : branch })
6468
@@ -74,11 +78,11 @@ def scan_repository(context: click.Context, path: str, branch: str):
7478
7579        documents_to_scan  =  []
7680        for  file  in  file_entries :
81+             # FIXME(MarshalX): probably file could be tree or submodule too. we expect blob only 
7782            progress_bar .update (ProgressBarSection .PREPARE_LOCAL_FILES )
7883
79-             path  =  file .path  if  monitor  else  get_path_by_os (os .path .join (path , file .path ))
80- 
81-             documents_to_scan .append (Document (path , file .data_stream .read ().decode ('UTF-8' , errors = 'replace' )))
84+             file_path  =  file .path  if  monitor  else  get_path_by_os (os .path .join (path , file .path ))
85+             documents_to_scan .append (Document (file_path , file .data_stream .read ().decode ('UTF-8' , errors = 'replace' )))
8286
8387        documents_to_scan  =  exclude_irrelevant_documents_to_scan (context , documents_to_scan )
8488
@@ -103,15 +107,17 @@ def scan_repository(context: click.Context, path: str, branch: str):
103107    required = False , 
104108) 
105109@click .pass_context  
106- def  scan_repository_commit_history (context : click .Context , path : str , commit_range : str ):
110+ def  scan_repository_commit_history (context : click .Context , path : str , commit_range : str )  ->   None :
107111    try :
108112        logger .debug ('Starting commit history scan process, %s' , {'path' : path , 'commit_range' : commit_range })
109-         return   scan_commit_range (context , path = path , commit_range = commit_range )
113+         scan_commit_range (context , path = path , commit_range = commit_range )
110114    except  Exception  as  e :
111115        _handle_exception (context , e )
112116
113117
114- def  scan_commit_range (context : click .Context , path : str , commit_range : str , max_commits_count : Optional [int ] =  None ):
118+ def  scan_commit_range (
119+     context : click .Context , path : str , commit_range : str , max_commits_count : Optional [int ] =  None 
120+ ) ->  None :
115121    scan_type  =  context .obj ['scan_type' ]
116122    progress_bar  =  context .obj ['progress_bar' ]
117123
@@ -166,38 +172,40 @@ def scan_commit_range(context: click.Context, path: str, commit_range: str, max_
166172    logger .debug ('List of commit ids to scan, %s' , {'commit_ids' : commit_ids_to_scan })
167173    logger .debug ('Starting to scan commit range (It may take a few minutes)' )
168174
169-     return  scan_documents (context , documents_to_scan , is_git_diff = True , is_commit_range = True )
175+     scan_documents (context , documents_to_scan , is_git_diff = True , is_commit_range = True )
176+     return  None 
170177
171178
172179@click .command ( 
173180    short_help = 'Execute scan in a CI environment which relies on the '  
174181    'CYCODE_TOKEN and CYCODE_REPO_LOCATION environment variables'  
175182) 
176183@click .pass_context  
177- def  scan_ci (context : click .Context ):
178-     return   scan_commit_range (context , path = os .getcwd (), commit_range = get_commit_range ())
184+ def  scan_ci (context : click .Context )  ->   None :
185+     scan_commit_range (context , path = os .getcwd (), commit_range = get_commit_range ())
179186
180187
181188@click .command (short_help = 'Scan the files in the path supplied in the command' ) 
182189@click .argument ('path' , nargs = 1 , type = click .STRING , required = True ) 
183190@click .pass_context  
184- def  scan_path (context : click .Context , path ) :
191+ def  scan_path (context : click .Context , path :  str )  ->   None :
185192    logger .debug ('Starting path scan process, %s' , {'path' : path })
186193    files_to_scan  =  get_relevant_files_in_path (path = path , exclude_patterns = ['**/.git/**' , '**/.cycode/**' ])
187194    files_to_scan  =  exclude_irrelevant_files (context , files_to_scan )
188195    logger .debug ('Found all relevant files for scanning %s' , {'path' : path , 'file_to_scan_count' : len (files_to_scan )})
189-     return   scan_disk_files (context , path , files_to_scan )
196+     scan_disk_files (context , path , files_to_scan )
190197
191198
192199@click .command (short_help = 'Use this command to scan the content that was not committed yet' ) 
193200@click .argument ('ignored_args' , nargs = - 1 , type = click .UNPROCESSED ) 
194201@click .pass_context  
195- def  pre_commit_scan (context : click .Context , ignored_args : List [str ]):
202+ def  pre_commit_scan (context : click .Context , ignored_args : List [str ])  ->   None :
196203    scan_type  =  context .obj ['scan_type' ]
197204    progress_bar  =  context .obj ['progress_bar' ]
198205
199206    if  scan_type  ==  consts .SCA_SCAN_TYPE :
200-         return  scan_sca_pre_commit (context )
207+         scan_sca_pre_commit (context )
208+         return 
201209
202210    diff_files  =  Repo (os .getcwd ()).index .diff ('HEAD' , create_patch = True , R = True )
203211
@@ -209,13 +217,13 @@ def pre_commit_scan(context: click.Context, ignored_args: List[str]):
209217        documents_to_scan .append (Document (get_path_by_os (get_diff_file_path (file )), get_diff_file_content (file )))
210218
211219    documents_to_scan  =  exclude_irrelevant_documents_to_scan (context , documents_to_scan )
212-     return   scan_documents (context , documents_to_scan , is_git_diff = True )
220+     scan_documents (context , documents_to_scan , is_git_diff = True )
213221
214222
215223@click .command (short_help = 'Use this command to scan commits on the server side before pushing them to the repository' ) 
216224@click .argument ('ignored_args' , nargs = - 1 , type = click .UNPROCESSED ) 
217225@click .pass_context  
218- def  pre_receive_scan (context : click .Context , ignored_args : List [str ]):
226+ def  pre_receive_scan (context : click .Context , ignored_args : List [str ])  ->   None :
219227    try :
220228        scan_type  =  context .obj ['scan_type' ]
221229        if  scan_type  !=  consts .SECRET_SCAN_TYPE :
@@ -253,13 +261,13 @@ def pre_receive_scan(context: click.Context, ignored_args: List[str]):
253261        _handle_exception (context , e )
254262
255263
256- def  scan_sca_pre_commit (context : click .Context ):
264+ def  scan_sca_pre_commit (context : click .Context )  ->   None :
257265    scan_parameters  =  get_default_scan_parameters (context )
258266    git_head_documents , pre_committed_documents  =  get_pre_commit_modified_documents (context .obj ['progress_bar' ])
259267    git_head_documents  =  exclude_irrelevant_documents_to_scan (context , git_head_documents )
260268    pre_committed_documents  =  exclude_irrelevant_documents_to_scan (context , pre_committed_documents )
261269    sca_code_scanner .perform_pre_hook_range_scan_actions (git_head_documents , pre_committed_documents )
262-     return   scan_commit_range_documents (
270+     scan_commit_range_documents (
263271        context ,
264272        git_head_documents ,
265273        pre_committed_documents ,
@@ -268,7 +276,7 @@ def scan_sca_pre_commit(context: click.Context):
268276    )
269277
270278
271- def  scan_sca_commit_range (context : click .Context , path : str , commit_range : str ):
279+ def  scan_sca_commit_range (context : click .Context , path : str , commit_range : str )  ->   None :
272280    progress_bar  =  context .obj ['progress_bar' ]
273281
274282    scan_parameters  =  get_scan_parameters (context , path )
@@ -282,12 +290,10 @@ def scan_sca_commit_range(context: click.Context, path: str, commit_range: str):
282290        path , from_commit_documents , from_commit_rev , to_commit_documents , to_commit_rev 
283291    )
284292
285-     return  scan_commit_range_documents (
286-         context , from_commit_documents , to_commit_documents , scan_parameters = scan_parameters 
287-     )
293+     scan_commit_range_documents (context , from_commit_documents , to_commit_documents , scan_parameters = scan_parameters )
288294
289295
290- def  scan_disk_files (context : click .Context , path : str , files_to_scan : List [str ]):
296+ def  scan_disk_files (context : click .Context , path : str , files_to_scan : List [str ])  ->   None :
291297    scan_parameters  =  get_scan_parameters (context , path )
292298    scan_type  =  context .obj ['scan_type' ]
293299    progress_bar  =  context .obj ['progress_bar' ]
@@ -307,7 +313,7 @@ def scan_disk_files(context: click.Context, path: str, files_to_scan: List[str])
307313                continue 
308314
309315    perform_pre_scan_documents_actions (context , scan_type , documents , is_git_diff )
310-     return   scan_documents (context , documents , is_git_diff = is_git_diff , scan_parameters = scan_parameters )
316+     scan_documents (context , documents , is_git_diff = is_git_diff , scan_parameters = scan_parameters )
311317
312318
313319def  set_issue_detected_by_scan_results (context : click .Context , scan_results : List [LocalScanResult ]) ->  None :
@@ -757,19 +763,21 @@ def get_oldest_unupdated_commit_for_branch(commit: str) -> Optional[str]:
757763    return  commits [0 ]
758764
759765
760- def  get_diff_file_path (file ) :
766+ def  get_diff_file_path (file :  'Diff' )  ->   Optional [ str ] :
761767    return  file .b_path  if  file .b_path  else  file .a_path 
762768
763769
764- def  get_diff_file_content (file ) :
770+ def  get_diff_file_content (file :  'Diff' )  ->   str :
765771    return  file .diff .decode ('UTF-8' , errors = 'replace' )
766772
767773
768- def  should_process_git_object (obj , _ : int ) ->  bool :
774+ def  should_process_git_object (obj :  'Blob' , _ : int ) ->  bool :
769775    return  obj .type  ==  'blob'  and  obj .size  >  0 
770776
771777
772- def  get_git_repository_tree_file_entries (path : str , branch : str ):
778+ def  get_git_repository_tree_file_entries (
779+     path : str , branch : str 
780+ ) ->  Union [Iterator ['IndexObjUnion' ], Iterator ['TraversedTreeTup' ]]:
773781    return  Repo (path ).tree (branch ).traverse (predicate = should_process_git_object )
774782
775783
@@ -867,7 +875,7 @@ def _exclude_detections_by_scan_type(
867875    return  detections 
868876
869877
870- def  exclude_detections_in_deleted_lines (detections ) ->  List :
878+ def  exclude_detections_in_deleted_lines (detections :  List [ Detection ] ) ->  List [ Detection ] :
871879    return  [detection  for  detection  in  detections  if  detection .detection_details .get ('line_type' ) !=  'Removed' ]
872880
873881
@@ -969,7 +977,7 @@ def _should_exclude_detection(detection: Detection, exclusions: Dict) -> bool:
969977    return  False 
970978
971979
972- def  _is_detection_sha_configured_in_exclusions (detection , exclusions : List [str ]) ->  bool :
980+ def  _is_detection_sha_configured_in_exclusions (detection :  Detection , exclusions : List [str ]) ->  bool :
973981    detection_sha  =  detection .detection_details .get ('sha512' , '' )
974982    return  detection_sha  in  exclusions 
975983
0 commit comments