66from datetime import datetime , timedelta
77from itertools import chain
88from typing import Any , Dict , List , Optional , Set , Union
9- import json
109import certifi
1110import humanize
1211from dateutil import tz
@@ -311,6 +310,7 @@ def __send_blocks_to_slack(
311310 status : FindingStatus ,
312311 channel : str ,
313312 thread_ts : str = None ,
313+ output_blocks : List [SlackBlock ] = [],
314314 ) -> str :
315315 file_blocks = add_pngs_for_all_svgs ([b for b in report_blocks if isinstance (b , FileBlock )])
316316 if not sink_params .send_svg :
@@ -328,7 +328,6 @@ def __send_blocks_to_slack(
328328 if error_msg :
329329 other_blocks .append (MarkdownBlock (error_msg ))
330330
331- output_blocks = []
332331 for block in other_blocks :
333332 output_blocks .extend (self .__to_slack (block , sink_params .name ))
334333 attachment_blocks = []
@@ -435,7 +434,7 @@ def __create_finding_header_preview(
435434 self , finding : Finding , status : FindingStatus , platform_enabled : bool , include_investigate_link : bool ,
436435 sink_params : SlackSinkPreviewParams = None
437436 ) -> List [SlackBlock ]:
438- title = finding .title .removeprefix ("[RESOLVED] " )
437+ title = finding .title .removeprefix ("[RESOLVED] " ) if finding . title else ""
439438
440439 title , mention = self .extract_mentions (title )
441440
@@ -772,7 +771,7 @@ def __send_finding_to_slack_preview(
772771 platform_enabled : bool ,
773772 thread_ts : str = None ,
774773 ) -> str :
775- blocks : List [SlackBlock ] = []
774+ blocks : List [BaseBlock ] = []
776775 attachment_blocks : List [BaseBlock ] = []
777776
778777 slack_channel = ChannelTransformer .template (
@@ -792,9 +791,8 @@ def __send_finding_to_slack_preview(
792791 FindingStatus .RESOLVED if finding .title .startswith ("[RESOLVED]" ) else FindingStatus .FIRING
793792 )
794793
795- if finding .title :
796- blocks .extend (self .__create_finding_header_preview (finding , status , platform_enabled ,
797- sink_params .investigate_link , sink_params ))
794+ slack_blocks : List [SlackBlock ] = self .__create_finding_header_preview (finding , status , platform_enabled ,
795+ sink_params .investigate_link , sink_params )
798796
799797 if not sink_params .hide_buttons :
800798 links_block : LinksBlock = self .__create_links (
@@ -819,33 +817,22 @@ def __send_finding_to_slack_preview(
819817 blocks .append (MarkdownBlock (f"{ Emojis .K8Notification .value } *Notification:* { finding .description } " ))
820818 unfurl = True
821819
822- if sink_params .hide_enrichments :
823- return self .__send_blocks_to_slack (
824- blocks ,
825- attachment_blocks ,
826- finding .title ,
827- sink_params ,
828- unfurl ,
829- status ,
830- slack_channel ,
831- thread_ts = thread_ts ,
832- )
833-
834- for enrichment in finding .enrichments :
835- if enrichment .annotations .get (EnrichmentAnnotation .SCAN , False ):
836- enrichment .blocks = [Transformer .scanReportBlock_to_fileblock (b ) for b in enrichment .blocks ]
820+ if not sink_params .hide_enrichments :
821+ for enrichment in finding .enrichments :
822+ if enrichment .annotations .get (EnrichmentAnnotation .SCAN , False ):
823+ enrichment .blocks = [Transformer .scanReportBlock_to_fileblock (b ) for b in enrichment .blocks ]
837824
838- # if one of the enrichment specified unfurl=False, this slack message will contain unfurl=False
839- unfurl = bool (unfurl and enrichment .annotations .get (SlackAnnotations .UNFURL , True ))
840- if enrichment .annotations .get (SlackAnnotations .ATTACHMENT ):
841- attachment_blocks .extend (enrichment .blocks )
842- else :
843- blocks .extend (enrichment .blocks )
825+ # if one of the enrichment specified unfurl=False, this slack message will contain unfurl=False
826+ unfurl = bool (unfurl and enrichment .annotations .get (SlackAnnotations .UNFURL , True ))
827+ if enrichment .annotations .get (SlackAnnotations .ATTACHMENT ):
828+ attachment_blocks .extend (enrichment .blocks )
829+ else :
830+ blocks .extend (enrichment .blocks )
844831
845- blocks .append (DividerBlock ())
832+ blocks .append (DividerBlock ())
846833
847- if len (attachment_blocks ):
848- attachment_blocks .append (DividerBlock ())
834+ if len (attachment_blocks ):
835+ attachment_blocks .append (DividerBlock ())
849836
850837 return self .__send_blocks_to_slack (
851838 blocks ,
@@ -856,6 +843,7 @@ def __send_finding_to_slack_preview(
856843 status ,
857844 slack_channel ,
858845 thread_ts = thread_ts ,
846+ output_blocks = slack_blocks ,
859847 )
860848
861849 def send_or_update_summary_message (
0 commit comments