Skip to content

Commit b1dda3b

Browse files
committed
refactoring, removing useless changes
1 parent fcad848 commit b1dda3b

10 files changed

Lines changed: 34 additions & 190 deletions

File tree

src/robusta/core/reporting/base.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ def __init__(
149149

150150
def __str__(self):
151151
return f"annotations: {self.annotations} Enrichment: {self.blocks} "
152-
153-
def to_dict(self):
154-
return {
155-
"blocks": [block.dict() for block in self.blocks],
156-
"annotations": self.annotations,
157-
"enrichment_type": self.enrichment_type,
158-
"title": self.title,
159-
}
160152

161153

162154
class FilterableScopeMatcher(BaseScopeMatcher):
@@ -412,31 +404,3 @@ def __calculate_fingerprint(subject: FindingSubject, source: FindingSource, aggr
412404
# if not, generate with logic similar to alertmanager
413405
s = f"{subject.subject_type},{subject.name},{subject.namespace},{subject.node},{source.value}{aggregation_key}"
414406
return hashlib.sha256(s.encode()).hexdigest()
415-
416-
def to_json(self):
417-
return {
418-
"title": self.title,
419-
"aggregation_key": self.aggregation_key,
420-
"severity": self.severity.name,
421-
"source": self.source.name,
422-
"description": self.description,
423-
"subject": {
424-
"name": self.subject.name,
425-
"subject_type": self.subject.subject_type.value,
426-
"namespace": self.subject.namespace,
427-
"node": self.subject.node,
428-
"container": self.subject.container,
429-
"labels": self.subject.labels,
430-
"annotations": self.subject.annotations,
431-
},
432-
"finding_type": self.finding_type.name,
433-
"failure": self.failure,
434-
"creation_date": self.creation_date,
435-
"fingerprint": self.fingerprint,
436-
"starts_at": self.starts_at,
437-
"ends_at": self.ends_at,
438-
"add_silence_url": self.add_silence_url,
439-
"silence_labels": self.silence_labels,
440-
"enrichments": [enrichment.to_dict() for enrichment in self.enrichments],
441-
"links": [link.dict() for link in self.links],
442-
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams, SlackSinkConfigWrapper
22
from robusta.core.sinks.slack.slack_sink import SlackSink
33

4+
# to prevent circular imports in SlackSender, SlackSinkParams and SlackSinkPreviewParams
45
__all__ = ["SlackSink", "SlackSinkParams", "SlackSinkConfigWrapper"]

src/robusta/core/sinks/slack/preview/slack_sink_preview_params.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
11
from robusta.core.sinks.sink_base_params import SinkBaseParams
22
from robusta.core.sinks.sink_config import SinkConfigBase
33
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams
4-
from enum import Enum
5-
from typing import Optional, Dict, Any
6-
7-
8-
class SlackTemplateStyle(str, Enum):
9-
DEFAULT = "default"
10-
LEGACY = "legacy"
4+
from typing import Optional, Dict
5+
from pydantic import validator
116

127

138
class SlackSinkPreviewParams(SlackSinkParams):
9+
#TODO: improve the SlackSinkPreviewParams so the slack_custom_templates can be defined once globally and
10+
# only a template name needs to be passed to each channel in the config
1411
slack_custom_templates: Optional[Dict[str, str]] = None # Template name -> custom template content
15-
template_name: Optional[str] = None
1612

17-
def get_effective_template_name(self) -> str:
13+
@validator('slack_custom_templates')
14+
def check_one_item(cls, v):
15+
if v is not None and len(v) != 1:
16+
raise ValueError("slack_custom_templates must contain exactly one key-value pair")
17+
return v
18+
19+
def get_template_name(self) -> str:
1820
"""
19-
Returns the template name to use for this sink. If template_name is set, use it.
20-
Otherwise, use 'legacy.j2' if template_style is legacy, else 'header.j2'.
21+
Returns the template name to use for this sink. If slack_custom_templates is set, use the first one.
22+
Otherwise, use 'header.j2'.
2123
"""
22-
if self.slack_custom_templates and len(self.slack_custom_templates) == 1:
24+
if self.slack_custom_templates:
2325
return next(iter(self.slack_custom_templates))
2426
return "header.j2"
2527

2628
def get_custom_template(self) -> Optional[str]:
27-
"""Get the custom template for the current template style"""
29+
"""Get the custom template if defined"""
2830
if not self.slack_custom_templates:
2931
return None
3032

31-
template_name = self.get_effective_template_name()
32-
if template_name not in self.slack_custom_templates:
33-
return None
34-
35-
return self.slack_custom_templates[template_name]
33+
return next(iter(self.slack_custom_templates.values()))
3634

3735

3836
class SlackSinkPreviewConfigWrapper(SinkConfigBase):

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from robusta.core.sinks.sink_base_params import SinkBaseParams
22
from robusta.core.sinks.sink_config import SinkConfigBase
33
from robusta.core.sinks.common import ChannelTransformer
4+
45
from typing import Optional
56
from pydantic import validator
67

src/robusta/core/sinks/slack/templates/README.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/robusta/core/sinks/slack/templates/legacy.j2

Lines changed: 0 additions & 31 deletions
This file was deleted.

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,16 @@ def render_to_blocks(self, template: Template, context: Dict[str, Any]) -> List[
7878
return []
7979

8080
def render_custom_template_to_blocks(self, custom_template: str, context: Dict[str, Any]) -> List[Dict[str, Any]]:
81-
"""
82-
Render a custom Jinja template string to Slack blocks.
83-
Args:
84-
custom_template: Jinja template string
85-
context: Dictionary of variables to pass to the template
86-
Returns:
87-
List of Slack block objects (dictionaries)
88-
"""
8981
try:
9082
template = Template(custom_template)
9183
return self.render_to_blocks(template, context)
9284
except Exception as e:
9385
logging.error(f"Error rendering custom template: {e}")
94-
return []
86+
return self.render_default_template_to_blocks(context)
9587

96-
def render_file_template_to_blocks(self, template_name: str, context: Dict[str, Any]) -> List[Dict[str, Any]]:
97-
"""
98-
Render a file-based Jinja template to Slack blocks.
99-
Args:
100-
template_name: The name of the file-based template (e.g., "header.j2")
101-
context: Dictionary of variables to pass to the template
102-
Returns:
103-
List of Slack block objects (dictionaries)
104-
"""
105-
template = self.get_template(template_name)
88+
def render_default_template_to_blocks(self, context: Dict[str, Any]) -> List[Dict[str, Any]]:
89+
DEFAULT_TEMPLATE_NAME="header.j2"
90+
template = self.get_template(DEFAULT_TEMPLATE_NAME)
10691
return self.render_to_blocks(template, context)
10792

10893

src/robusta/integrations/slack/sender.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def __create_finding_header_preview(
509509
}
510510

511511
# Determine the template name to use
512-
template_name = sink_params.get_effective_template_name() if sink_params else "header.j2"
512+
template_name = sink_params.get_template_name() if sink_params else "header.j2"
513513

514514
# Get the custom template for this template name, if any
515515
custom_template = sink_params.get_custom_template() if sink_params else None
@@ -518,7 +518,7 @@ def __create_finding_header_preview(
518518
if custom_template:
519519
return template_loader.render_custom_template_to_blocks(custom_template, template_context)
520520
else:
521-
return template_loader.render_file_template_to_blocks(template_name, template_context)
521+
return template_loader.render_default_template_to_blocks(template_context)
522522

523523
def __create_finding_header(
524524
self, finding: Finding, status: FindingStatus, platform_enabled: bool, include_investigate_link: bool
@@ -676,12 +676,15 @@ def send_finding_to_slack(
676676
thread_ts: str = None,
677677
) -> str:
678678
if self.is_preview:
679-
return self.__send_finding_to_slack_preview(
680-
finding=finding,
681-
sink_params=sink_params,
682-
platform_enabled=platform_enabled,
683-
thread_ts=thread_ts
684-
)
679+
try:
680+
return self.__send_finding_to_slack_preview(
681+
finding=finding,
682+
sink_params=sink_params,
683+
platform_enabled=platform_enabled,
684+
thread_ts=thread_ts
685+
)
686+
except Exception:
687+
logging.exception("Failed to render slack preview template, defaulting to legacy slack output")
685688
return self.__send_finding_to_slack(
686689
finding=finding,
687690
sink_params=sink_params,
@@ -793,14 +796,11 @@ def __send_finding_to_slack_preview(
793796
FindingStatus.RESOLVED if finding.title.startswith("[RESOLVED]") else FindingStatus.FIRING
794797
)
795798

796-
# Get JIRA-style header blocks
797799
if finding.title:
798800
header_blocks = self.__create_finding_header_preview(finding, status, platform_enabled,
799801
sink_params.investigate_link, sink_params)
800802

801-
# Description handling - moved above the buttons
802803
if finding.description:
803-
# Always show description immediately after title
804804
description_text = finding.description
805805
blocks.append(MarkdownBlock(description_text))
806806

tests/manual_tests/test_slack_integration_manual.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from robusta.core.model.base_params import ResourceInfo
2727
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams
28-
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewParams, SlackTemplateStyle
28+
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewParams
2929
from robusta.core.playbooks.internal.ai_integration import ask_holmes
3030

3131
# Configure logging - set to DEBUG to see full block details

0 commit comments

Comments
 (0)