55from .base import Plugin
66from socketsecurity .core .classes import Diff
77from socketsecurity .core .messages import Messages
8+ from socketsecurity .core .alert_selection import (
9+ clone_diff_with_selected_alerts ,
10+ filter_alerts_by_reachability ,
11+ select_diff_alerts ,
12+ )
813from socketsecurity .core .helper .socket_facts_loader import (
914 load_socket_facts ,
1015 get_components_with_vulnerabilities ,
@@ -68,13 +73,14 @@ def _send_webhook_alerts(self, diff, config: CliConfig):
6873
6974 # Get repo name from config
7075 repo_name = config .repo or ""
76+ diff_alert_source = select_diff_alerts (diff , strict_blocking = config .strict_blocking )
7177
7278 # Handle reachability data if --reach is enabled
7379 if config .reach :
7480 self ._send_reachability_alerts (valid_webhooks , webhook_configs , repo_name , config , diff )
7581
7682 # Handle diff alerts (if any)
77- if not diff . new_alerts :
83+ if not diff_alert_source :
7884 logger .debug ("No new diff alerts to notify via Slack." )
7985 else :
8086 # Send to each configured webhook with filtering
@@ -86,7 +92,7 @@ def _send_webhook_alerts(self, diff, config: CliConfig):
8692 # Filter alerts based on webhook config
8793 # When --reach is used, reachability_alerts_only applies to diff alerts
8894 filtered_alerts = self ._filter_alerts (
89- diff . new_alerts ,
95+ diff_alert_source ,
9096 webhook_config ,
9197 repo_name ,
9298 config ,
@@ -99,13 +105,7 @@ def _send_webhook_alerts(self, diff, config: CliConfig):
99105 continue
100106
101107 # Create a temporary diff object with filtered alerts for message creation
102- filtered_diff = Diff (
103- new_alerts = filtered_alerts ,
104- diff_url = getattr (diff , "diff_url" , "" ),
105- new_packages = getattr (diff , "new_packages" , []),
106- removed_packages = getattr (diff , "removed_packages" , []),
107- packages = getattr (diff , "packages" , {})
108- )
108+ filtered_diff = clone_diff_with_selected_alerts (diff , filtered_alerts )
109109
110110 message = self .create_slack_blocks_from_diff (filtered_diff , config )
111111
@@ -114,7 +114,7 @@ def _send_webhook_alerts(self, diff, config: CliConfig):
114114 if config .enable_debug :
115115 logger .debug (f"Slack webhook URL: { url } " )
116116 logger .debug (f"Slack webhook name: { name } " )
117- logger .debug (f"Total diff alerts: { len (diff . new_alerts )} , Filtered alerts: { len (filtered_alerts )} " )
117+ logger .debug (f"Total diff alerts: { len (diff_alert_source )} , Filtered alerts: { len (filtered_alerts )} " )
118118 logger .debug (f"Message blocks count: { len (message )} " )
119119
120120 response = requests .post (
@@ -153,7 +153,8 @@ def _send_bot_alerts(self, diff, config: CliConfig):
153153 logger .debug ("Alert levels: %s" , self .config .get ("levels" ))
154154 logger .debug (f"Number of bot_configs: { len (bot_configs )} " )
155155 logger .debug (f"config.reach: { config .reach } " )
156- logger .debug (f"len(diff.new_alerts): { len (diff .new_alerts ) if diff .new_alerts else 0 } " )
156+ diff_alert_source = select_diff_alerts (diff , strict_blocking = config .strict_blocking )
157+ logger .debug (f"len(diff alert source): { len (diff_alert_source ) if diff_alert_source else 0 } " )
157158
158159 # Get repo name from config
159160 repo_name = config .repo or ""
@@ -163,7 +164,7 @@ def _send_bot_alerts(self, diff, config: CliConfig):
163164 self ._send_bot_reachability_alerts (bot_configs , bot_token , repo_name , config , diff )
164165
165166 # Handle diff alerts (if any)
166- if not diff . new_alerts :
167+ if not diff_alert_source :
167168 logger .debug ("No new diff alerts to notify via Slack." )
168169 else :
169170 # Send to each configured bot_config with filtering
@@ -178,7 +179,7 @@ def _send_bot_alerts(self, diff, config: CliConfig):
178179 # Filter alerts based on bot config
179180 # When --reach is used, reachability_alerts_only applies to diff alerts
180181 filtered_alerts = self ._filter_alerts (
181- diff . new_alerts ,
182+ diff_alert_source ,
182183 bot_config ,
183184 repo_name ,
184185 config ,
@@ -191,18 +192,12 @@ def _send_bot_alerts(self, diff, config: CliConfig):
191192 continue
192193
193194 # Create a temporary diff object with filtered alerts for message creation
194- filtered_diff = Diff (
195- new_alerts = filtered_alerts ,
196- diff_url = getattr (diff , "diff_url" , "" ),
197- new_packages = getattr (diff , "new_packages" , []),
198- removed_packages = getattr (diff , "removed_packages" , []),
199- packages = getattr (diff , "packages" , {})
200- )
195+ filtered_diff = clone_diff_with_selected_alerts (diff , filtered_alerts )
201196
202197 message = self .create_slack_blocks_from_diff (filtered_diff , config )
203198
204199 if config .enable_debug :
205- logger .debug (f"Bot config '{ name } ': Total diff alerts: { len (diff . new_alerts )} , Filtered alerts: { len (filtered_alerts )} " )
200+ logger .debug (f"Bot config '{ name } ': Total diff alerts: { len (diff_alert_source )} , Filtered alerts: { len (filtered_alerts )} " )
206201 logger .debug (f"Message blocks count: { len (message )} " )
207202
208203 # Send to each channel in the bot_config
@@ -387,6 +382,18 @@ def _filter_alerts(
387382 f"repos={ repos_filter } , alert_types={ alert_types } , "
388383 f"severities={ severities } , reachability_only={ reachability_only } , "
389384 f"apply_reachability_only={ apply_reachability_only_filter } " )
385+
386+ reachable_alert_identity_set = None
387+ if apply_reachability_only_filter and reachability_only :
388+ reachable_alerts = filter_alerts_by_reachability (
389+ alerts ,
390+ "reachable" ,
391+ config .target_path ,
392+ config .reach_output_file ,
393+ logger = logger ,
394+ fallback_to_blocking_for_reachable = True ,
395+ )
396+ reachable_alert_identity_set = {id (a ) for a in reachable_alerts }
390397
391398 for alert in alerts :
392399 # For reachability data, only apply severities filter
@@ -405,9 +412,7 @@ def _filter_alerts(
405412 continue
406413
407414 # Filter by reachability_alerts_only (only when --reach is used)
408- if apply_reachability_only_filter and reachability_only :
409- # Only include alerts that have error=True (blocking issues)
410- if not getattr (alert , "error" , False ):
415+ if reachable_alert_identity_set is not None and id (alert ) not in reachable_alert_identity_set :
411416 continue
412417
413418 # Filter by alert_types (overrides severity, empty list = no filtering)
0 commit comments