Skip to content

Commit 3c1f709

Browse files
committed
refactoring sender
1 parent a73bf41 commit 3c1f709

1 file changed

Lines changed: 29 additions & 99 deletions

File tree

src/robusta/integrations/slack/sender.py

Lines changed: 29 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,6 @@ def __send_finding_to_slack_preview(
777777
) -> str:
778778
blocks: List[BaseBlock] = []
779779
attachment_blocks: List[BaseBlock] = []
780-
header_blocks: List[SlackBlock] = [] # JIRA-style header blocks
781780

782781
slack_channel = ChannelTransformer.template(
783782
sink_params.channel_override,
@@ -797,8 +796,8 @@ def __send_finding_to_slack_preview(
797796
)
798797

799798
if finding.title:
800-
header_blocks = self.__create_finding_header_preview(finding, status, platform_enabled,
801-
sink_params.investigate_link, sink_params)
799+
blocks.extend(self.__create_finding_header_preview(finding, status, platform_enabled,
800+
sink_params.investigate_link, sink_params))
802801

803802
if finding.description:
804803
description_text = finding.description
@@ -812,113 +811,44 @@ def __send_finding_to_slack_preview(
812811
if HOLMES_ENABLED and HOLMES_ASK_SLACK_BUTTON_ENABLED:
813812
blocks.append(self.__create_holmes_callback(finding))
814813

815-
unfurl = True
816-
all_file_blocks = [] # Collect all file blocks to be handled specially
814+
blocks.append(MarkdownBlock(text=f"*Source:* `{self.cluster_name}`"))
815+
if finding.description:
816+
if finding.source == FindingSource.PROMETHEUS:
817+
blocks.append(MarkdownBlock(f"{Emojis.Alert.value} *Alert:* {finding.description}"))
818+
elif finding.source == FindingSource.KUBERNETES_API_SERVER:
819+
blocks.append(
820+
MarkdownBlock(f"{Emojis.K8Notification.value} *K8s event detected:* {finding.description}")
821+
)
822+
else:
823+
blocks.append(MarkdownBlock(f"{Emojis.K8Notification.value} *Notification:* {finding.description}"))
817824

825+
unfurl = True
818826
for enrichment in finding.enrichments:
819827
if enrichment.annotations.get(EnrichmentAnnotation.SCAN, False):
820828
enrichment.blocks = [Transformer.scanReportBlock_to_fileblock(b) for b in enrichment.blocks]
821829

822830
# if one of the enrichment specified unfurl=False, this slack message will contain unfurl=False
823831
unfurl = bool(unfurl and enrichment.annotations.get(SlackAnnotations.UNFURL, True))
824-
825-
# Separate file blocks from normal blocks
826-
file_blocks_in_enrichment = [b for b in enrichment.blocks if isinstance(b, FileBlock)]
827-
non_file_blocks = [b for b in enrichment.blocks if not isinstance(b, FileBlock)]
828-
829-
# Collect file blocks
830-
all_file_blocks.extend(file_blocks_in_enrichment)
831-
832-
# Put non-file blocks in the attachment
833-
attachment_blocks.extend(non_file_blocks)
834-
835-
# Add file blocks to the main blocks for proper handling
836-
blocks.extend(all_file_blocks)
837-
838-
# No divider in the main blocks
839-
840-
# We need to create a minimal version of __send_blocks_to_slack
841-
# that can handle both our header blocks and regular blocks
842-
file_blocks = add_pngs_for_all_svgs([b for b in blocks if isinstance(b, FileBlock)])
843-
if not sink_params.send_svg:
844-
file_blocks = [b for b in file_blocks if not b.filename.endswith(".svg")]
845-
846-
other_blocks = [b for b in blocks if not isinstance(b, FileBlock)]
847-
848-
# wide tables aren't displayed properly on slack. looks better in a text file
849-
file_blocks.extend(Transformer.tableblock_to_fileblocks(other_blocks, SLACK_TABLE_COLUMNS_LIMIT))
850-
file_blocks.extend(Transformer.tableblock_to_fileblocks(attachment_blocks, SLACK_TABLE_COLUMNS_LIMIT))
851-
852-
message, error_msg = self.prepare_slack_text(
853-
finding.title, max_log_file_limit_kb=sink_params.max_log_file_limit_kb, files=file_blocks
854-
)
855-
856-
# Convert BaseBlocks to Slack blocks
857-
output_blocks = []
858-
for block in other_blocks:
859-
output_blocks.extend(self.__to_slack(block, sink_params.name))
860-
861-
attachment_slack_blocks = []
862-
for block in attachment_blocks:
863-
attachment_slack_blocks.extend(self.__to_slack(block, sink_params.name))
864-
865-
# Combine with header blocks
866-
all_blocks = header_blocks + output_blocks
867-
868-
try:
869-
if thread_ts:
870-
kwargs = {"thread_ts": thread_ts}
832+
if enrichment.annotations.get(SlackAnnotations.ATTACHMENT):
833+
attachment_blocks.extend(enrichment.blocks)
871834
else:
872-
kwargs = {}
873-
874-
# Create a single attachment with all blocks and a divider at the end
875-
attachments = []
876-
877-
# Add divider to the end of attachment blocks if there are any
878-
all_attachment_blocks = attachment_slack_blocks.copy() if attachment_slack_blocks else []
879-
880-
# Always add a divider at the end
881-
all_attachment_blocks.append({"type": "divider"})
882-
883-
# Create a single attachment with the status color
884-
attachments = [{
885-
"color": status.to_color_hex(),
886-
"blocks": all_attachment_blocks
887-
}]
888-
889-
# Detailed logging of blocks before sending to Slack
890-
logging.debug(
891-
f"SENDING TO SLACK - FULL BLOCKS DETAIL:\n"
892-
f"CHANNEL: {slack_channel}\n"
893-
f"TITLE: {finding.title}\n"
894-
f"HEADER BLOCKS: {json.dumps(header_blocks, indent=2)}\n"
895-
f"MAIN BLOCKS: {json.dumps(output_blocks, indent=2)}\n"
896-
f"ALL BLOCKS: {json.dumps(all_blocks, indent=2)}\n"
897-
f"ATTACHMENT BLOCKS: {json.dumps(all_attachment_blocks, indent=2)}\n"
898-
f"ATTACHMENTS: {json.dumps(attachments, indent=2)}\n"
899-
)
835+
blocks.extend(enrichment.blocks)
900836

901-
# Send the message with our JIRA-style headers and attachments
902-
resp = self.slack_client.chat_postMessage(
903-
channel=slack_channel,
904-
text=message,
905-
blocks=all_blocks,
906-
display_as_bot=True,
907-
attachments=attachments,
908-
unfurl_links=unfurl,
909-
unfurl_media=unfurl,
910-
**kwargs,
911-
)
837+
blocks.append(DividerBlock())
912838

913-
# Store channel id for future use
914-
self.channel_name_to_id[slack_channel] = resp["channel"]
915-
return resp["ts"]
839+
if len(attachment_blocks):
840+
attachment_blocks.append(DividerBlock())
916841

917-
except Exception as e:
918-
logging.error(
919-
f"error sending message to slack\ne={e}\ntext={message}\nchannel={slack_channel}"
920-
)
921-
return ""
842+
return self.__send_blocks_to_slack(
843+
blocks,
844+
attachment_blocks,
845+
finding.title,
846+
sink_params,
847+
unfurl,
848+
status,
849+
slack_channel,
850+
thread_ts=thread_ts,
851+
)
922852

923853
def send_or_update_summary_message(
924854
self,

0 commit comments

Comments
 (0)