@@ -90,6 +90,9 @@ def handle_output(self, diff_report: Diff) -> None:
9090 plugin_mgr = PluginManager ({"slack" : slack_config })
9191 plugin_mgr .send (diff_report , config = self .config )
9292
93+ self .save_json_file (diff_report , getattr (self .config , "json_file" , None ))
94+ self .save_summary_file (diff_report , getattr (self .config , "summary_file" , None ))
95+ self .save_report_link_file (diff_report , getattr (self .config , "report_link_file" , None ))
9396 self .save_sbom_file (diff_report , self .config .sbom_file )
9497
9598 def return_exit_code (self , diff_report : Diff ) -> int :
@@ -107,50 +110,15 @@ def return_exit_code(self, diff_report: Diff) -> int:
107110
108111 def output_console_comments (self , diff_report : Diff , sbom_file_name : Optional [str ] = None ) -> None :
109112 """Outputs formatted console comments"""
110- selected_alerts = select_diff_alerts (diff_report , strict_blocking = self .config .strict_blocking )
111- has_new_alerts = len (selected_alerts ) > 0
112- has_unchanged_alerts = (
113- self .config .strict_blocking and
114- hasattr (diff_report , 'unchanged_alerts' ) and
115- len (diff_report .unchanged_alerts ) > 0
116- )
117-
118- if not has_new_alerts and not has_unchanged_alerts :
119- self .logger .info ("No issues found" )
120- return
121-
122- # Count blocking vs warning alerts
123- new_blocking = sum (1 for issue in diff_report .new_alerts if issue .error )
124- new_warning = sum (1 for issue in diff_report .new_alerts if issue .warn )
125-
126- unchanged_blocking = 0
127- unchanged_warning = 0
128- if has_unchanged_alerts :
129- unchanged_blocking = sum (1 for issue in diff_report .unchanged_alerts if issue .error )
130- unchanged_warning = sum (1 for issue in diff_report .unchanged_alerts if issue .warn )
131-
132- selected_diff = clone_diff_with_selected_alerts (diff_report , selected_alerts )
133- console_security_comment = Messages .create_console_security_alert_table (selected_diff )
134-
135- # Build status message
136- self .logger .info ("Security issues detected by Socket Security:" )
137- if new_blocking > 0 :
138- self .logger .info (f" - NEW blocking issues: { new_blocking } " )
139- if new_warning > 0 :
140- self .logger .info (f" - NEW warning issues: { new_warning } " )
141- if unchanged_blocking > 0 :
142- self .logger .info (f" - EXISTING blocking issues: { unchanged_blocking } (causing failure due to --strict-blocking)" )
143- if unchanged_warning > 0 :
144- self .logger .info (f" - EXISTING warning issues: { unchanged_warning } " )
145-
146- self .logger .info (f"Diff Url: { diff_report .diff_url } " )
147- self .logger .info (f"\n { console_security_comment } " )
113+ summary_text = self .build_summary_text (diff_report )
114+ for line in summary_text .splitlines ():
115+ self .logger .info (line )
116+ if not summary_text .strip ():
117+ self .logger .info ("" )
148118
149119 def output_console_json (self , diff_report : Diff , sbom_file_name : Optional [str ] = None ) -> None :
150120 """Outputs JSON formatted results"""
151- selected_alerts = select_diff_alerts (diff_report , strict_blocking = self .config .strict_blocking )
152- selected_diff = clone_diff_with_selected_alerts (diff_report , selected_alerts )
153- console_security_comment = Messages .create_security_comment_json (selected_diff )
121+ console_security_comment = self .build_json_report (diff_report )
154122 self .save_sbom_file (diff_report , sbom_file_name )
155123 self .logger .info (json .dumps (console_security_comment ))
156124
@@ -249,11 +217,96 @@ def save_sbom_file(self, diff_report: Diff, sbom_file_name: Optional[str] = None
249217 if not sbom_file_name or not diff_report .sbom :
250218 return
251219
252- sbom_path = Path (sbom_file_name )
253- sbom_path .parent .mkdir (parents = True , exist_ok = True )
220+ self .write_json_file (sbom_file_name , diff_report .sbom )
254221
255- with open (sbom_path , "w" ) as f :
256- json .dump (diff_report .sbom , f , indent = 2 )
222+ def build_summary_text (self , diff_report : Diff ) -> str :
223+ """Render the console summary text for stdout and file output."""
224+ selected_alerts = select_diff_alerts (diff_report , strict_blocking = self .config .strict_blocking )
225+ has_new_alerts = len (selected_alerts ) > 0
226+ has_unchanged_alerts = (
227+ self .config .strict_blocking and
228+ hasattr (diff_report , 'unchanged_alerts' ) and
229+ len (diff_report .unchanged_alerts ) > 0
230+ )
231+
232+ if not has_new_alerts and not has_unchanged_alerts :
233+ return "No issues found"
234+
235+ new_blocking = sum (1 for issue in diff_report .new_alerts if issue .error )
236+ new_warning = sum (1 for issue in diff_report .new_alerts if issue .warn )
237+
238+ unchanged_blocking = 0
239+ unchanged_warning = 0
240+ if has_unchanged_alerts :
241+ unchanged_blocking = sum (1 for issue in diff_report .unchanged_alerts if issue .error )
242+ unchanged_warning = sum (1 for issue in diff_report .unchanged_alerts if issue .warn )
243+
244+ selected_diff = clone_diff_with_selected_alerts (diff_report , selected_alerts )
245+ console_security_comment = Messages .create_console_security_alert_table (selected_diff )
246+
247+ lines = ["Security issues detected by Socket Security:" ]
248+ if new_blocking > 0 :
249+ lines .append (f" - NEW blocking issues: { new_blocking } " )
250+ if new_warning > 0 :
251+ lines .append (f" - NEW warning issues: { new_warning } " )
252+ if unchanged_blocking > 0 :
253+ lines .append (
254+ f" - EXISTING blocking issues: { unchanged_blocking } (causing failure due to --strict-blocking)"
255+ )
256+ if unchanged_warning > 0 :
257+ lines .append (f" - EXISTING warning issues: { unchanged_warning } " )
258+
259+ report_link = getattr (diff_report , "report_url" , "" ) or getattr (diff_report , "diff_url" , "" )
260+ lines .append (f"Diff Url: { report_link } " )
261+ lines .append ("" )
262+ lines .append (str (console_security_comment ))
263+ return "\n " .join (lines )
264+
265+ def build_json_report (self , diff_report : Diff ) -> dict :
266+ """Build the JSON report payload for stdout and file output."""
267+ selected_alerts = select_diff_alerts (diff_report , strict_blocking = self .config .strict_blocking )
268+ selected_diff = clone_diff_with_selected_alerts (diff_report , selected_alerts )
269+ report = Messages .create_security_comment_json (selected_diff )
270+ legal_flag = getattr (self .config , "legal" , False )
271+ repo = getattr (self .config , "repo" , None )
272+ branch = getattr (self .config , "branch" , None )
273+ commit_sha = getattr (self .config , "commit_sha" , None )
274+ report ["report_url" ] = getattr (diff_report , "report_url" , None )
275+ report ["repo" ] = repo if isinstance (repo , str ) or repo is None else None
276+ report ["branch" ] = branch if isinstance (branch , str ) or branch is None else None
277+ report ["commit_sha" ] = commit_sha if isinstance (commit_sha , str ) or commit_sha is None else None
278+ report ["legal_mode" ] = legal_flag if isinstance (legal_flag , bool ) else False
279+ return report
280+
281+ def save_json_file (self , diff_report : Diff , json_file_name : Optional [str ] = None ) -> None :
282+ if not json_file_name :
283+ return
284+ self .write_json_file (json_file_name , self .build_json_report (diff_report ))
285+
286+ def save_summary_file (self , diff_report : Diff , summary_file_name : Optional [str ] = None ) -> None :
287+ if not summary_file_name :
288+ return
289+ self .write_text_file (summary_file_name , self .build_summary_text (diff_report ) + "\n " )
290+
291+ def save_report_link_file (self , diff_report : Diff , report_link_file_name : Optional [str ] = None ) -> None :
292+ if not report_link_file_name :
293+ return
294+ report_link = getattr (diff_report , "report_url" , "" ) or getattr (diff_report , "diff_url" , "" )
295+ if not report_link :
296+ return
297+ self .write_text_file (report_link_file_name , report_link + "\n " )
298+
299+ def write_json_file (self , file_name : str , content : Any ) -> None :
300+ file_path = Path (file_name )
301+ file_path .parent .mkdir (parents = True , exist_ok = True )
302+ with open (file_path , "w" ) as f :
303+ json .dump (content , f , indent = 2 )
304+
305+ def write_text_file (self , file_name : str , content : str ) -> None :
306+ file_path = Path (file_name )
307+ file_path .parent .mkdir (parents = True , exist_ok = True )
308+ with open (file_path , "w" ) as f :
309+ f .write (content )
257310
258311 def output_gitlab_security (self , diff_report : Diff ) -> None :
259312 """
0 commit comments