Skip to content

Commit abe1c74

Browse files
committed
saving progress
1 parent 4e6b055 commit abe1c74

8 files changed

Lines changed: 640 additions & 19 deletions

File tree

src/robusta/core/sinks/sink_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from robusta.core.sinks.sink_base import SinkBase
2323
from robusta.core.sinks.sink_config import SinkConfigBase
2424
from robusta.core.sinks.slack import SlackSink, SlackSinkConfigWrapper
25+
from robusta.core.sinks.slack.preview import SlackSinkPreview, SlackSinkPreviewConfigWrapper
2526
from robusta.core.sinks.telegram import TelegramSink, TelegramSinkConfigWrapper
2627
from robusta.core.sinks.victorops import VictoropsConfigWrapper, VictoropsSink
2728
from robusta.core.sinks.webex import WebexSink, WebexSinkConfigWrapper
@@ -35,6 +36,7 @@
3536
class SinkFactory:
3637
__sink_config_mapping: Dict[Type[SinkConfigBase], Type[SinkBase]] = {
3738
SlackSinkConfigWrapper: SlackSink,
39+
SlackSinkPreviewConfigWrapper: SlackSinkPreview,
3840
RocketchatSinkConfigWrapper: RocketchatSink,
3941
RobustaSinkConfigWrapper: RobustaSink,
4042
MsTeamsSinkConfigWrapper: MsTeamsSink,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from robusta.core.sinks.slack.preview.slack_sink_preview import SlackSinkPreview
2+
from robusta.core.sinks.slack.preview.slack_sink_params_preview import SlackSinkPreviewConfigWrapper, SlackSinkPreviewParams
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewConfigWrapper, SlackSinkPreviewParams
2+
from robusta.core.sinks.slack.slack_sink import SlackSink
3+
from robusta.integrations import slack as slack_module
4+
5+
6+
class SlackSinkPreview(SlackSink):
7+
params: SlackSinkPreviewParams
8+
9+
def __init__(self, sink_config: SlackSinkPreviewConfigWrapper, registry):
10+
self.slack_sender = slack_module.SlackSender(
11+
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel, is_preview=True
12+
)
13+
super().__init__(sink_config.slack_sink, registry, self.slack_sender)
14+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from robusta.core.sinks.sink_base_params import SinkBaseParams
2+
from robusta.core.sinks.sink_config import SinkConfigBase
3+
from robusta.core.sinks.slack import SlackSinkParams
4+
from enum import Enum
5+
from typing import Optional, Dict
6+
7+
8+
class SlackTemplateStyle(str, Enum):
9+
DEFAULT = "default"
10+
LEGACY = "legacy"
11+
12+
13+
class SlackSinkPreviewParams(SlackSinkParams):
14+
template_style: SlackTemplateStyle = SlackTemplateStyle.DEFAULT # Use "legacy" for old-style formatting
15+
slack_custom_templates: Optional[Dict[str, str]] = None # Template name -> custom template content
16+
template_name: Optional[str] = None
17+
18+
def get_effective_template_name(self) -> str:
19+
"""
20+
Returns the template name to use for this sink. If template_name is set, use it.
21+
Otherwise, use 'legacy.j2' if template_style is legacy, else 'header.j2'.
22+
"""
23+
if self.template_name:
24+
return self.template_name
25+
if self.template_style == SlackTemplateStyle.LEGACY:
26+
return "legacy.j2"
27+
return "header.j2"
28+
29+
def get_custom_template(self) -> Optional[str]:
30+
"""
31+
Returns the custom template string for the effective template name, if it exists.
32+
"""
33+
template_name = self.get_effective_template_name()
34+
if self.slack_custom_templates and template_name in self.slack_custom_templates:
35+
return self.slack_custom_templates[template_name]
36+
return None
37+
38+
39+
class SlackSinkPreviewConfigWrapper(SinkConfigBase):
40+
slack_sink: SlackSinkPreviewParams
41+
42+
def get_params(self) -> SinkBaseParams:
43+
return self.slack_sink

src/robusta/core/sinks/slack/slack_sink.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
class SlackSink(SinkBase):
1111
params: SlackSinkParams
1212

13-
def __init__(self, sink_config: SlackSinkConfigWrapper, registry):
13+
def __init__(self, sink_config: SlackSinkConfigWrapper, registry, slack_sender=None):
1414
super().__init__(sink_config.slack_sink, registry)
1515
self.slack_channel = sink_config.slack_sink.slack_channel
1616
self.api_key = sink_config.slack_sink.api_key
17-
self.slack_sender = slack_module.SlackSender(
18-
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel
19-
)
17+
if slack_sender:
18+
self.slack_sender = slack_sender
19+
else:
20+
self.slack_sender = slack_module.SlackSender(
21+
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel
22+
)
2023
self.registry.subscribe("replace_callback_with_string", self)
2124

2225
def handle_event(self, event_name: str, **kwargs):

src/robusta/core/sinks/slack/templates/template_loader.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,37 @@ def get_template(self, template_name: str) -> Template:
5555

5656
return self._templates[template_name]
5757

58-
def render_to_blocks(self, template_name: str, context: Dict[str, Any]) -> List[Dict[str, Any]]:
58+
def render_to_blocks(self, template: Template, context: Dict[str, Any]) -> List[Dict[str, Any]]:
5959
"""
60-
Render a template using the provided context and parse the result as JSON to get Slack blocks.
61-
60+
Render a Jinja2 Template object using the provided context and parse the result as JSON to get Slack blocks.
6261
Args:
63-
template_name: The name of the template file
62+
template: A Jinja2 Template object
6463
context: Dictionary of variables to pass to the template
65-
6664
Returns:
6765
List of Slack block objects (dictionaries)
6866
"""
69-
template = self.get_template(template_name)
70-
7167
try:
7268
rendered = template.render(**context)
73-
74-
# Split by newlines to get multiple blocks and parse each as JSON
75-
blocks = []
7669
blocks = []
7770
for block_str in rendered.strip().split("\n\n"):
7871
if not block_str.strip():
7972
continue
80-
8173
try:
8274
block_str_fixed = escape_raw_newlines_in_json_strings(block_str)
75+
# Try to parse as JSON, but if it fails, log and skip
8376
block = json.loads(block_str_fixed)
8477
blocks.append(block)
85-
8678
except json.JSONDecodeError as e:
8779
logging.exception(f"Error parsing JSON from template output: {e}")
8880
logging.warning(f"Problematic JSON (repr): {repr(block_str)}")
89-
81+
continue # Skip this block and continue
82+
except Exception as e:
83+
logging.error(f"Unexpected error parsing block: {e}")
84+
logging.warning(f"Problematic JSON (repr): {repr(block_str)}")
85+
continue
9086
return blocks
9187
except Exception as e:
92-
logging.error(f"Error rendering template {template_name}: {e}")
88+
logging.error(f"Error rendering template: {e}")
9389
return []
9490

9591
def render_custom_template_to_blocks(self, custom_template: str, context: Dict[str, Any]) -> List[Dict[str, Any]]:

src/robusta/integrations/slack/sender.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@
6565
class SlackSender:
6666
verified_api_tokens: Set[str] = set()
6767
channel_name_to_id = {}
68+
is_preview = False
6869

69-
def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing_key: str, slack_channel: str):
70+
def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing_key: str, slack_channel: str, is_preview: bool = False):
7071
"""
7172
Connect to Slack and verify that the Slack token is valid.
7273
Return True on success, False on failure
@@ -87,6 +88,7 @@ def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing
8788
self.signing_key = signing_key
8889
self.account_id = account_id
8990
self.cluster_name = cluster_name
91+
self.is_preview = is_preview
9092

9193
if slack_token not in self.verified_api_tokens:
9294
try:

0 commit comments

Comments
 (0)