Alerting: Remove duplicate Slack notification title (#58107)

Move mentions to a markdown-formatted pretext field
to prevent issues mixing blocks and legacy-attachment content.
This commit is contained in:
Will Jordan
2022-11-04 17:09:24 +01:00
committed by GitHub
parent 4d2bf41efb
commit d581b368bd
3 changed files with 12 additions and 38 deletions
@@ -132,7 +132,7 @@ type slackMessage struct {
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Attachments []attachment `json:"attachments"`
Blocks []map[string]interface{} `json:"blocks"`
Blocks []map[string]interface{} `json:"blocks,omitempty"`
}
// attachment is used to display a richly-formatted message block.
@@ -147,6 +147,8 @@ type attachment struct {
FooterIcon string `json:"footer_icon"`
Color string `json:"color,omitempty"`
Ts int64 `json:"ts,omitempty"`
Pretext string `json:"pretext,omitempty"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
}
// Notify sends an alert notification to Slack.
@@ -261,7 +263,6 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A
req := &slackMessage{
Channel: tmpl(sn.settings.Recipient),
Text: tmpl(sn.settings.Title),
Username: tmpl(sn.settings.Username),
IconEmoji: tmpl(sn.settings.IconEmoji),
IconURL: tmpl(sn.settings.IconURL),
@@ -315,15 +316,9 @@ func (sn *SlackNotifier) buildSlackMessage(ctx context.Context, alrts []*types.A
}
if mentionsBuilder.Len() > 0 {
req.Blocks = []map[string]interface{}{
{
"type": "section",
"text": map[string]interface{}{
"type": "mrkdwn",
"text": mentionsBuilder.String(),
},
},
}
// Use markdown-formatted pretext for any mentions.
req.Attachments[0].MrkdwnIn = []string{"pretext"}
req.Attachments[0].Pretext = mentionsBuilder.String()
}
return req, nil
@@ -64,7 +64,6 @@ func TestSlackNotifier(t *testing.T) {
},
expMsg: &slackMessage{
Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
@@ -100,7 +99,6 @@ func TestSlackNotifier(t *testing.T) {
},
expMsg: &slackMessage{
Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
@@ -136,7 +134,6 @@ func TestSlackNotifier(t *testing.T) {
},
expMsg: &slackMessage{
Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
@@ -180,7 +177,6 @@ func TestSlackNotifier(t *testing.T) {
},
expMsg: &slackMessage{
Channel: "#testchannel",
Text: "2 firing, 0 resolved",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
@@ -229,7 +225,6 @@ func TestSlackNotifier(t *testing.T) {
},
expMsg: &slackMessage{
Channel: "#testchannel",
Text: "[FIRING:1] (val1)",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{