Skip to content

Commit

Permalink
Possible fix for grafana#129 - Check if mention - configvalues are co…
Browse files Browse the repository at this point in the history
…ntaining non-empty values

only add to builder when they are not empty.
  • Loading branch information
DGuhr committed Feb 17, 2024
1 parent 92f64f0 commit ea0d440
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions receivers/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,18 @@ func (sn *Notifier) createSlackMessage(ctx context.Context, alerts []*types.Aler
if len(sn.settings.MentionGroups) > 0 {
appendSpace()
for _, g := range sn.settings.MentionGroups {
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", tmpl(g)))
if strings.TrimSpace(g) != "" {
mentionsBuilder.WriteString(fmt.Sprintf("<!subteam^%s>", tmpl(g)))
}
}
}

if len(sn.settings.MentionUsers) > 0 {
appendSpace()
for _, u := range sn.settings.MentionUsers {
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", tmpl(u)))
if strings.TrimSpace(u) != "" {
mentionsBuilder.WriteString(fmt.Sprintf("<@%s>", tmpl(u)))
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions receivers/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,47 @@ func TestNotify_PostMessage(t *testing.T) {
},
},
},
}, {
name: "Message is sent without mentions when only empty values are in mention config",
settings: Config{
EndpointURL: "https://example.com/api",
URL: "https://example.com/api",
Token: "1234",
Recipient: "#test",
Text: templates.DefaultMessageEmbed,
Title: templates.DefaultMessageTitleEmbed,
Username: "Grafana",
IconEmoji: ":emoji:",
IconURL: "",
MentionChannel: "",
MentionUsers: []string{""},
MentionGroups: []string{"", " ", " "},
},
alerts: []*types.Alert{{
Alert: model.Alert{
Labels: model.LabelSet{"alertname": "alert1", "lbl1": "val1"},
Annotations: model.LabelSet{"ann1": "annv1"},
},
}},
expectedMessage: &slackMessage{
Channel: "#test",
Username: "Grafana",
IconEmoji: ":emoji:",
Attachments: []attachment{
{
Title: "[FIRING:1] (val1)",
TitleLink: "http://localhost/alerting/list",
Text: "**Firing**\n\nValue: [no value]\nLabels:\n - alertname = alert1\n - lbl1 = val1\nAnnotations:\n - ann1 = annv1\nSilence: http://localhost/alerting/silence/new?alertmanager=grafana&matcher=alertname%3Dalert1&matcher=lbl1%3Dval1\n",
Fallback: "[FIRING:1] (val1)",
Fields: nil,
Footer: "Grafana v" + appVersion,
FooterIcon: "https://grafana.com/static/assets/img/fav32.png",
Color: "#D63232",
Pretext: "",
MrkdwnIn: []string(nil),
},
},
},
}}

for _, test := range tests {
Expand Down

0 comments on commit ea0d440

Please sign in to comment.