Skip to content

Commit 38c8ce4

Browse files
committed
Refactored getting channel and thread for slack
1 parent c0200f0 commit 38c8ce4

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/robusta/core/playbooks/internal/ai_integration.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,14 @@ def ask_holmes(event: ExecutionBaseEvent, params: AIInvestigateParams):
112112
finding.add_enrichment(
113113
[HolmesResultsBlock(holmes_result=holmes_result)], enrichment_type=EnrichmentType.ai_analysis
114114
)
115-
runner_context = getattr(params, "robusta_context", None) # Safely get the context dict
115+
runner_context = getattr(params, "robusta_context", None)
116116
if runner_context and "thread_ts" in runner_context:
117117
original_thread_ts = runner_context.get("thread_ts")
118118
original_channel_id = runner_context.get("channel_id")
119119
if original_thread_ts:
120120
finding.robusta_context["thread_ts"] = original_thread_ts
121121
finding.robusta_context["channel_id"] = original_channel_id
122-
logging.info(f"Added message_ts={original_thread_ts} to finding {finding.id} annotations.")
123-
else:
124-
logging.warning(f"message_ts found in robusta_context for finding {finding.id} but it is empty.")
125-
else:
126-
logging.debug(f"No message_ts found in robusta_context for finding {finding.id}. Context: {runner_context}")
122+
127123
event.add_finding(finding)
128124

129125
except Exception as e:

src/robusta/core/reporting/callbacks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create_for_func(
4343
sinks=[sink],
4444
origin="callback",
4545
)
46-
logging.info(f"Created action request body: {body}")
46+
4747
return ExternalActionRequest(
4848
body=body,
4949
signature=sign_action_request(body, signing_key),

src/robusta/integrations/slack/sender.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def __get_action_block_for_choices(self, sink: str, choices: Dict[str, CallbackC
115115
).json(),
116116
}
117117
)
118-
logging.info(f"Created action block: {buttons}")
119118

120119
return [{"type": "actions", "elements": buttons}]
121120

@@ -569,36 +568,48 @@ def send_holmes_analysis(
569568

570569
except Exception:
571570
logging.exception(f"error sending message to slack. {title}")
571+
572+
def _resolve_slack_thread(
573+
self,
574+
finding: Finding,
575+
sink_params: SlackSinkParams,
576+
thread_ts: Optional[str] = None,
577+
) -> tuple[str, Optional[str]]:
578+
579+
channel = ChannelTransformer.template(
580+
sink_params.channel_override,
581+
sink_params.slack_channel,
582+
self.cluster_name,
583+
finding.subject.labels,
584+
finding.subject.annotations,
585+
)
586+
587+
ctx = getattr(finding, "robusta_context", {}) or {}
588+
thread_override = ctx.get("thread_ts")
589+
channel_override = ctx.get("channel_id")
590+
591+
return (
592+
channel_override or channel,
593+
thread_override or thread_ts,
594+
)
572595

573596
def send_finding_to_slack(
574597
self,
575598
finding: Finding,
576599
sink_params: SlackSinkParams,
577600
platform_enabled: bool,
578-
thread_ts: str = None,
601+
thread_ts: Optional[str] = None,
579602
) -> str:
580603
blocks: List[BaseBlock] = []
581604
attachment_blocks: List[BaseBlock] = []
582605

583-
slack_channel = ChannelTransformer.template(
584-
sink_params.channel_override,
585-
sink_params.slack_channel,
586-
self.cluster_name,
587-
finding.subject.labels,
588-
finding.subject.annotations,
606+
channel_id, thread_ts = self._resolve_slack_thread(
607+
finding, sink_params, thread_ts
589608
)
590-
robusta_context = getattr(finding, "robusta_context", None)
591-
effective_thread_ts = thread_ts # Default to the one passed in (e.g., from grouping)
592-
effective_channel_id = slack_channel
593-
if robusta_context:
594-
annotation_ts = robusta_context.get("thread_ts")
595-
channel_id = robusta_context.get("channel_id")
596-
if annotation_ts: # Make sure it's not None or empty
597-
effective_thread_ts = annotation_ts # Prioritize the annotation!
598-
effective_channel_id = channel_id
609+
599610
if finding.finding_type == FindingType.AI_ANALYSIS:
600611
# holmes analysis message needs special handling
601-
self.send_holmes_analysis(finding, effective_channel_id, platform_enabled, effective_thread_ts)
612+
self.send_holmes_analysis(finding, channel_id, platform_enabled, thread_ts)
602613
return "" # [arik] Looks like the return value here is not used, needs to be removed
603614

604615
status: FindingStatus = (

0 commit comments

Comments
 (0)