Fix Discord Webhook URL for invalid template (#44763)

This commit fixes an issue where an invalid template for Discord would change the Webhook URL to "" and cause "unsupported protocol scheme" errors.
This commit is contained in:
George Robinson 2022-02-02 13:28:41 +00:00 committed by GitHub
parent b78082f426
commit 924deda589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -103,6 +103,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)

View File

@ -3,6 +3,7 @@ package channels
import (
"context"
"encoding/json"
"errors"
"net/url"
"testing"
@ -99,6 +100,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: `{