From 9464ffa86b0c0140460fcb9afb269b7335f05369 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 2 Feb 2022 14:48:27 +0100 Subject: [PATCH] Fix Discord Webhook URL for invalid template (#44763) (#44772) This commit fixes an issue where an invalid template for Discord would change the Webhook URL to "" and cause "unsupported protocol scheme" errors. --- pkg/services/ngalert/notifier/channels/discord.go | 1 + pkg/services/ngalert/notifier/channels/discord_test.go | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/pkg/services/ngalert/notifier/channels/discord.go b/pkg/services/ngalert/notifier/channels/discord.go index 83900e964e7..f4a83e044c2 100644 --- a/pkg/services/ngalert/notifier/channels/discord.go +++ b/pkg/services/ngalert/notifier/channels/discord.go @@ -101,6 +101,7 @@ func (d DiscordNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, u := tmpl(d.WebhookURL) if tmplErr != nil { d.log.Warn("failed to template Discord message", "err", tmplErr.Error()) + return false, tmplErr } body, err := json.Marshal(bodyJSON) diff --git a/pkg/services/ngalert/notifier/channels/discord_test.go b/pkg/services/ngalert/notifier/channels/discord_test.go index b14be659713..82a55e74a03 100644 --- a/pkg/services/ngalert/notifier/channels/discord_test.go +++ b/pkg/services/ngalert/notifier/channels/discord_test.go @@ -3,6 +3,7 @@ package channels import ( "context" "encoding/json" + "errors" "net/url" "testing" @@ -100,6 +101,14 @@ func TestDiscordNotifier(t *testing.T) { settings: `{}`, expInitError: `failed to validate receiver "discord_testing" of type "discord": could not find webhook url property in settings`, }, + { + name: "Invalid template returns error", + settings: `{ + "url": "http://localhost", + "message": "{{ template \"invalid.template\" }}" + }`, + expMsgError: errors.New("template: :1:12: executing \"\" at <{{template \"invalid.template\"}}>: template \"invalid.template\" not defined"), + }, { name: "Default config with one alert, use default discord username", settings: `{