11import argparse
22import json
33from socketsecurity .core import Core , __version__
4- from socketsecurity .core .classes import FullScanParams , Diff , Package , Alert
4+ from socketsecurity .core .classes import FullScanParams , Diff , Package , Issue
55from socketsecurity .core .messages import Messages
66from socketsecurity .core .scm_comments import Comments
77from socketsecurity .core .git_interface import Git
1212
1313logging .basicConfig (level = logging .INFO )
1414log = logging .getLogger ("socketcli" )
15+ blocking_disabled = False
1516
1617parser = argparse .ArgumentParser (
1718 prog = "socketcli" ,
142143 default = False
143144)
144145
146+ parser .add_argument (
147+ '--disable-blocking' ,
148+ help = 'Disables failing checks and will only exit with an exit code of 0' ,
149+ action = 'store_true' ,
150+ default = False
151+ )
152+
145153
146154def output_console_comments (diff_report : Diff , sbom_file_name : str = None ) -> None :
147155 console_security_comment = Messages .create_console_security_alert_table (diff_report )
148156 save_sbom_file (diff_report , sbom_file_name )
157+ log .info (f"Socket Full Scan ID: { diff_report .id } " )
149158 if not report_pass (diff_report ):
150159 log .info ("Security issues detected by Socket Security" )
151160 msg = f"\n { console_security_comment } "
152161 log .info (msg )
153- sys .exit (1 )
162+ if not blocking_disabled :
163+ sys .exit (1 )
154164 else :
155165 log .info ("No New Security issues detected by Socket Security" )
156166
@@ -159,15 +169,15 @@ def output_console_json(diff_report: Diff, sbom_file_name: str = None) -> None:
159169 console_security_comment = Messages .create_security_comment_json (diff_report )
160170 save_sbom_file (diff_report , sbom_file_name )
161171 print (json .dumps (console_security_comment ))
162- if not report_pass (diff_report ):
172+ if not report_pass (diff_report ) and not blocking_disabled :
163173 sys .exit (1 )
164174
165175
166176def report_pass (diff_report : Diff ) -> bool :
167177 report_passed = True
168178 if len (diff_report .new_alerts ) > 0 :
169179 for alert in diff_report .new_alerts :
170- alert : Alert
180+ alert : Issue
171181 if report_passed and alert .error :
172182 report_passed = False
173183 break
@@ -184,11 +194,17 @@ def cli():
184194 main_code ()
185195 except KeyboardInterrupt :
186196 log .info ("Keyboard Interrupt detected, exiting" )
187- sys .exit (2 )
197+ if not blocking_disabled :
198+ sys .exit (2 )
199+ else :
200+ sys .exit (0 )
188201 except Exception as error :
189202 log .error ("Unexpected error when running the cli" )
190203 log .error (error )
191- sys .exit (3 )
204+ if not blocking_disabled :
205+ sys .exit (3 )
206+ else :
207+ sys .exit (0 )
192208
193209
194210def main_code ():
@@ -214,6 +230,10 @@ def main_code():
214230 disable_overview = arguments .disable_overview
215231 disable_security_issue = arguments .disable_security_issue
216232 ignore_commit_files = arguments .ignore_commit_files
233+ disable_blocking = arguments .disable_blocking
234+ if disable_blocking :
235+ global blocking_disabled
236+ blocking_disabled = True
217237 files = arguments .files
218238 log .info (f"Starting Socket Security Scan version { __version__ } " )
219239 api_token = os .getenv ("SOCKET_SECURITY_API_KEY" ) or arguments .api_token
@@ -244,6 +264,7 @@ def main_code():
244264 is_repo = True
245265 except InvalidGitRepositoryError :
246266 is_repo = False
267+ ignore_commit_files = True
247268 pass
248269 except NoSuchPathError :
249270 raise Exception (f"Unable to find path { target_path } " )
@@ -265,12 +286,15 @@ def main_code():
265286 if scm is not None :
266287 default_branch = scm .is_default_branch
267288
268- if is_repo and files is not None and len (files ) == 0 and not ignore_commit_files :
269- no_change = True
270- else :
271- no_change = False
272289 base_api_url = os .getenv ("BASE_API_URL" ) or None
273290 core = Core (token = api_token , request_timeout = 6000 , base_api_url = base_api_url )
291+ no_change = True
292+ if ignore_commit_files :
293+ no_change = False
294+ elif is_repo and files is not None and len (files ) > 0 :
295+ if len (core .match_supported_files (target_path , files )) > 0 :
296+ no_change = False
297+
274298 set_as_pending_head = False
275299 if default_branch :
276300 set_as_pending_head = True
@@ -295,7 +319,9 @@ def main_code():
295319 log .info ("Push initiated flow" )
296320 diff : Diff
297321 diff = core .create_new_diff (target_path , params , workspace = target_path , new_files = files , no_change = no_change )
298- if scm .check_event_type () == "diff" :
322+ if no_change :
323+ log .info ("No dependency changes" )
324+ elif scm .check_event_type () == "diff" :
299325 log .info ("Starting comment logic for PR/MR event" )
300326 log .debug (f"Getting comments for Repo { scm .repository } for PR { scm .pr_number } " )
301327 comments = scm .get_comments_for_pr (repo , str (pr_number ))
@@ -307,14 +333,24 @@ def main_code():
307333 security_comment = Messages .security_comment_template (diff )
308334 new_security_comment = True
309335 new_overview_comment = True
336+ update_old_security_comment = (
337+ security_comment is None or
338+ security_comment == "" or
339+ (len (comments ) != 0 and comments .get ("security" ) is not None )
340+ )
341+ update_old_overview_comment = (
342+ overview_comment is None or
343+ overview_comment == "" or
344+ (len (comments ) != 0 and comments .get ("overview" ) is not None )
345+ )
310346 if len (diff .new_alerts ) == 0 or disable_security_issue :
311- if security_comment is None or security_comment == "" :
347+ if not update_old_security_comment :
312348 new_security_comment = False
313349 log .debug ("No new alerts or security issue comment disabled" )
314350 else :
315351 log .debug ("Updated security comment with no new alerts" )
316352 if (len (diff .new_packages ) == 0 and len (diff .removed_packages ) == 0 ) or disable_overview :
317- if overview_comment is None or overview_comment == "" :
353+ if not update_old_overview_comment :
318354 new_overview_comment = False
319355 log .debug ("No new/removed packages or Dependency Overview comment disabled" )
320356 else :
0 commit comments