Skip to content

feat(slack): optional blokkit payload for Slack notifications - #5414

Draft
TheMeier wants to merge 1 commit into
prometheus:mainfrom
TheMeier:issues/2217
Draft

feat(slack): optional blokkit payload for Slack notifications#5414
TheMeier wants to merge 1 commit into
prometheus:mainfrom
TheMeier:issues/2217

Conversation

@TheMeier

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Please check all the applicable boxes.

  • Please list all open issue(s) discussed with maintainers related to this change
  • Is this a new Receiver integration?
  • Is this a bugfix?
    • I have added tests that can reproduce the bug which pass with this bugfix applied
  • Is this a new feature?
    • I have added tests that test the new feature's functionality
  • Does this change affect performance?
    • I have provided benchmarks comparison that shows performance is improved or is not degraded
      • You can use benchstat to compare benchmarks
    • I have added new benchmarks if required or requested by maintainers
  • Is this a breaking change?
    • My changes do not break the existing cluster messages
    • My changes do not break the existing api
  • I have added/updated the required documentation
  • I have signed-off my commits
  • I will follow best practices for contributing to this project

Which user-facing changes does this PR introduce?

ENHANCEMENT SLACK: Add optional, configurable block-kit payload

@TheMeier

Copy link
Copy Markdown
Contributor Author

This is the approach to acheive option 2 from this comment #2217 (comment) using other means. It relies on template.DeepCopyWithTemplate which is problematic, but the situation with that can maybe be improved in the future.

Signed-off-by: Christoph Maser <christoph.maser+github@gmail.com>
@TheMeier
TheMeier marked this pull request as ready for review August 10, 2026 07:05
@TheMeier
TheMeier requested a review from a team as a code owner August 10, 2026 07:05
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Slack receivers can now select templated Block Kit payloads or retain plain attachment payloads. The change adds configuration fields, payload composition, message update handling, documentation, and tests.

Changes

Slack Block Kit support

Layer / File(s) Summary
Block Kit configuration contract
config/notifiers.go, docs/configuration.md
Adds use_block_kit and block_kit_payload configuration fields, with documentation and an example.
Block Kit payload composition
notify/slack/message_blockkit.go, notify/slack/message_blockkit_test.go
Deep-copies and renders Block Kit templates, validates object fields, and tests JSON output.
Notification payload routing and updates
notify/slack/message_plain.go, notify/slack/slack.go, notify/slack/slack_test.go
Builds plain requests, selects the configured payload type, updates message fields, encodes the selected payload, and tests valid and invalid Block Kit notifications.

Mattermost template formatting

Layer / File(s) Summary
Default template spacing
template/default.tmpl
Adds a blank line before the Mattermost template definition closes.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Alertmanager
  participant SlackNotifier
  participant BlockKitComposer
  participant SlackAPI
  Alertmanager->>SlackNotifier: send notification
  SlackNotifier->>BlockKitComposer: render configured blocks
  BlockKitComposer-->>SlackNotifier: return channel, text, and blocks
  SlackNotifier->>SlackAPI: encode and submit selected payload
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (3 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes the issue and release note, but all applicable checklist items remain unchecked, including tests, documentation, sign-off, and compatibility declarations. Check each applicable item and confirm the added tests, documentation, sign-off, and compatibility status before merging.
Out of Scope Changes check ⚠️ Warning The change to template/default.tmpl adds a Mattermost template blank line and is unrelated to Slack Block Kit support in #2217. Remove the unrelated Mattermost template change or link it to a separate issue and pull request.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Slack Block Kit payload feature, although “blokkit” is misspelled.
Linked Issues check ✅ Passed The changes implement configurable, templated Slack Block Kit payloads, including blocks, fields, actions, documentation, and tests required by #2217.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@config/notifiers.go`:
- Around line 348-349: Rename the exported fields BlocKitEnabeld and
BlocKitPayload to BlockKitEnabled and BlockKitPayload in the notifier
configuration, then update every notifier and test reference to use the
corrected names while preserving their existing YAML and JSON tags.

In `@notify/slack/message_blockkit.go`:
- Around line 27-35: Update the rendering flow around DeepCopyWithTemplate so
template substitutions are rendered without reparsing rendered string values as
YAML, preserving Block Kit text fields as strings. Keep the existing block
structure and template behavior intact, and add coverage for rendered text
containing colon-space and scalar-looking values.

In `@notify/slack/slack.go`:
- Around line 86-99: Update the payload construction flow around
composePlainRequest and composeBlockKitPayload to render only the selected
format: call composePlainRequest only in the plain branch, and check tmplTextErr
after rendering either payload. In Block Kit mode, include errors from the
top-level channel and message_text template rendering before returning the
request; preserve the existing payload and channelForError assignments for valid
templates.

In `@template/default.tmpl`:
- Around line 236-238: Remove the added blank lines after the closing template
block in template/default.tmpl, leaving the file unchanged and preserving the
existing mattermost.default.text template content.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2fce32da-a53c-4609-aa5b-b92f8735a31b

📥 Commits

Reviewing files that changed from the base of the PR and between 8d7515a and d46eea4.

📒 Files selected for processing (8)
  • config/notifiers.go
  • docs/configuration.md
  • notify/slack/message_blockkit.go
  • notify/slack/message_blockkit_test.go
  • notify/slack/message_plain.go
  • notify/slack/slack.go
  • notify/slack/slack_test.go
  • template/default.tmpl

Comment thread config/notifiers.go
Comment on lines +348 to +349
BlocKitEnabeld *bool `yaml:"use_block_kit,omitempty" json:"use_block_kit,omitempty"`
BlocKitPayload any `yaml:"block_kit_payload,omitempty" json:"block_kit_payload,omitempty"`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Correct the exported Block Kit field names.

BlocKitEnabeld and BlocKitPayload expose misspelled Go API names. Rename them to BlockKitEnabled and BlockKitPayload before release. Update all notifier and test references.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config/notifiers.go` around lines 348 - 349, Rename the exported fields
BlocKitEnabeld and BlocKitPayload to BlockKitEnabled and BlockKitPayload in the
notifier configuration, then update every notifier and test reference to use the
corrected names while preserving their existing YAML and JSON tags.

Comment on lines +27 to +35
renderedBlocks, err := template.DeepCopyWithTemplate(blocksTmpl, tmplTextFunc)
if err != nil {
return nil, err
}

return map[string]any{
"channel": channel,
"text": text,
"blocks": renderedBlocks,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Keep rendered Block Kit values as strings.

DeepCopyWithTemplate reparses rendered string values. A valid Slack text value such as summary: disk full becomes a map, which violates the Block Kit schema. The documented quote workaround does not make normal templated text safe.

Render template substitutions without YAML-decoding the rendered result. Add coverage for colon-space and scalar-looking rendered text.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@notify/slack/message_blockkit.go` around lines 27 - 35, Update the rendering
flow around DeepCopyWithTemplate so template substitutions are rendered without
reparsing rendered string values as YAML, preserving Block Kit text fields as
strings. Keep the existing block structure and template behavior intact, and add
coverage for rendered text containing colon-space and scalar-looking values.

Comment thread notify/slack/slack.go
Comment on lines +86 to 99
useBlockKit := n.conf.BlocKitEnabeld != nil && *n.conf.BlocKitEnabeld
req := composePlainRequest(n.conf, tmplText, logger)
payload := any(req)
channelForError := req.Channel
var bkPayload map[string]any

if useBlockKit {
bkPayload, err = composeBlockKitPayload(n.conf.BlocKitPayload, tmplText(n.conf.Channel), tmplText(n.conf.MessageText), tmplText, &tmplTextErr)
if err != nil {
return false, fmt.Errorf("failed to render block kit payload: %w", err)
}
payload = bkPayload
channelForError = tmplText(n.conf.Channel)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Render only the selected payload format and check template errors.

When useBlockKit is true, Line 87 still renders every legacy attachment field. An invalid unused legacy template can fail a valid Block Kit notification. In plain mode, tmplTextErr is never checked before the request is encoded.

Compose composePlainRequest only in the plain branch. Check tmplTextErr after rendering the selected payload, including top-level Block Kit channel and message_text values.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@notify/slack/slack.go` around lines 86 - 99, Update the payload construction
flow around composePlainRequest and composeBlockKitPayload to render only the
selected format: call composePlainRequest only in the plain branch, and check
tmplTextErr after rendering either payload. In Block Kit mode, include errors
from the top-level channel and message_text template rendering before returning
the request; preserve the existing payload and channelForError assignments for
valid templates.

Comment thread template/default.tmpl
Comment on lines +236 to +238
{{ end }}


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Remove this unrelated template change.

This PR adds configurable Slack Block Kit payloads and does not introduce a new default notification template. Lines 237-238 only add blank source lines after mattermost.default.text; remove them to keep template/default.tmpl unchanged.

As per coding guidelines, update template/default.tmpl only when introducing new default notification templates.

Proposed cleanup
 {{ end }}
-
-
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{{ end }}
{{ end }}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@template/default.tmpl` around lines 236 - 238, Remove the added blank lines
after the closing template block in template/default.tmpl, leaving the file
unchanged and preserving the existing mattermost.default.text template content.

Source: Coding guidelines

@TheMeier
TheMeier marked this pull request as draft August 11, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Block Kit with Slack notifications

1 participant