Skip to content

Commit 8bdd1f2

Browse files
committed
Replace ASCII table with Slack table block format in sender.py
Replace the old __to_slack_table that rendered tables as ASCII text (via tabulate/markdown) with native Slack table blocks. This uses the Slack Block Kit "table" type with rich_text cells, giving proper column alignment and bold headers natively in Slack. - Remove the special 2-column bullet format workaround - Add __normalize_row_columns to ensure each row has exactly the right number of columns (padding or merging as needed) - Build header row with bold styling and data rows from render_rows() https://claude.ai/code/session_012sATNqRz1ryNcxJeLXxaxk
1 parent e1d8841 commit 8bdd1f2

1 file changed

Lines changed: 70 additions & 14 deletions

File tree

src/robusta/integrations/slack/sender.py

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,79 @@ def __to_slack_markdown(self, block: MarkdownBlock) -> List[SlackBlock]:
182182
}
183183
]
184184

185-
def __to_slack_table(self, block: TableBlock):
186-
# temp workaround untill new blocks will be added to support these.
187-
if len(block.headers) == 2:
188-
table_rows: List[str] = []
189-
for row in block.rows:
190-
if "-------" in str(row[1]): # special care for table subheader
191-
subheader: str = row[0]
192-
table_rows.append(f"--- {subheader.capitalize()} ---")
193-
continue
185+
@staticmethod
186+
def __normalize_row_columns(row: list, column_count: int) -> List[str]:
187+
"""Normalize a row to have exactly column_count columns."""
188+
if column_count <= 0:
189+
return []
190+
columns = [str(c) for c in row]
191+
if len(columns) == column_count:
192+
return columns
193+
elif len(columns) < column_count:
194+
return columns + [""] * (column_count - len(columns))
195+
else:
196+
return columns[: column_count - 1] + [
197+
" | ".join(columns[column_count - 1 :])
198+
]
194199

195-
table_rows.append(f"● {row[0]} `{row[1]}`")
200+
def __to_slack_table(self, block: TableBlock) -> List[SlackBlock]:
201+
if not block.headers:
202+
return self.__to_slack_markdown(block.to_markdown())
203+
204+
column_count = len(block.headers)
196205

197-
table_str = "\n".join(table_rows)
198-
table_str = f"{block.table_name} \n{table_str}"
199-
return self.__to_slack_markdown(MarkdownBlock(table_str))
206+
rows = []
207+
# Build header row with bold text
208+
header_blocks = []
209+
for header in block.headers:
210+
header_blocks.append(
211+
{
212+
"type": "rich_text",
213+
"elements": [
214+
{
215+
"type": "rich_text_section",
216+
"elements": [
217+
{
218+
"type": "text",
219+
"text": str(header),
220+
"style": {"bold": True},
221+
}
222+
],
223+
}
224+
],
225+
},
226+
)
227+
rows.append(header_blocks)
228+
229+
# Build data rows
230+
for row in block.render_rows():
231+
columns = self.__normalize_row_columns(row, column_count)
232+
row_blocks = []
233+
for column in columns:
234+
row_blocks.append(
235+
{
236+
"type": "rich_text",
237+
"elements": [
238+
{
239+
"type": "rich_text_section",
240+
"elements": [
241+
{
242+
"type": "text",
243+
"text": column,
244+
}
245+
],
246+
}
247+
],
248+
},
249+
)
250+
rows.append(row_blocks)
200251

201-
return self.__to_slack_markdown(block.to_markdown())
252+
return [
253+
{
254+
"type": "table",
255+
"rows": rows,
256+
}
257+
]
202258

203259
def __to_slack(self, block: BaseBlock, sink_name: str) -> List[SlackBlock]:
204260
if isinstance(block, MarkdownBlock):

0 commit comments

Comments
 (0)