From 642add43eeb4be26fc7183b3e1aa3bd4125fd6c5 Mon Sep 17 00:00:00 2001 From: Diorcet Yann Date: Fri, 15 Mar 2024 15:52:37 +0100 Subject: [PATCH] Add Template field in email receiver --- receivers/email/config.go | 6 ++++++ receivers/email/config_test.go | 20 ++++++++++++-------- receivers/email/email.go | 2 +- receivers/email/email_test.go | 5 +++-- templates/default_template.go | 1 + 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/receivers/email/config.go b/receivers/email/config.go index 7f7b7f7b..9c4171a8 100644 --- a/receivers/email/config.go +++ b/receivers/email/config.go @@ -14,6 +14,7 @@ type Config struct { Addresses []string Message string Subject string + Template string } func NewConfig(jsonData json.RawMessage) (Config, error) { @@ -22,6 +23,7 @@ func NewConfig(jsonData json.RawMessage) (Config, error) { Addresses string `json:"addresses,omitempty"` Message string `json:"message,omitempty"` Subject string `json:"subject,omitempty"` + Template string `json:"template,omitempty"` } var settings emailSettingsRaw @@ -38,12 +40,16 @@ func NewConfig(jsonData json.RawMessage) (Config, error) { if settings.Subject == "" { settings.Subject = templates.DefaultMessageTitleEmbed } + if settings.Template == "" { + settings.Template = templates.DefaultTemplate + } return Config{ SingleEmail: settings.SingleEmail, Message: settings.Message, Subject: settings.Subject, Addresses: addresses, + Template: settings.Template, }, nil } diff --git a/receivers/email/config_test.go b/receivers/email/config_test.go index b03e3589..6497268e 100644 --- a/receivers/email/config_test.go +++ b/receivers/email/config_test.go @@ -39,8 +39,9 @@ func TestNewConfig(t *testing.T) { Addresses: []string{ "test@grafana.com", }, - Message: "", - Subject: templates.DefaultMessageTitleEmbed, + Message: "", + Subject: templates.DefaultMessageTitleEmbed, + Template: templates.DefaultTemplate, }, }, { @@ -54,8 +55,9 @@ func TestNewConfig(t *testing.T) { "test3@grafana.com", "test4@granafa.com", }, - Message: "", - Subject: templates.DefaultMessageTitleEmbed, + Message: "", + Subject: templates.DefaultMessageTitleEmbed, + Template: templates.DefaultTemplate, }, }, { @@ -66,8 +68,9 @@ func TestNewConfig(t *testing.T) { Addresses: []string{ "test@grafana.com", }, - Message: "", - Subject: templates.DefaultMessageTitleEmbed, + Message: "", + Subject: templates.DefaultMessageTitleEmbed, + Template: templates.DefaultTemplate, }, }, { @@ -78,8 +81,9 @@ func TestNewConfig(t *testing.T) { Addresses: []string{ "test@grafana.com", }, - Message: "test-message", - Subject: "test-subject", + Message: "test-message", + Subject: "test-subject", + Template: templates.DefaultTemplate, }, }, } diff --git a/receivers/email/email.go b/receivers/email/email.go index 7117706b..8b3aae09 100644 --- a/receivers/email/email.go +++ b/receivers/email/email.go @@ -91,7 +91,7 @@ func (en *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, e EmbeddedFiles: embeddedFiles, To: en.settings.Addresses, SingleEmail: en.settings.SingleEmail, - Template: "ng_alert_notification", + Template: en.settings.Template, } if tmplErr != nil { diff --git a/receivers/email/email_test.go b/receivers/email/email_test.go index 2a242178..12c0e4c2 100644 --- a/receivers/email/email_test.go +++ b/receivers/email/email_test.go @@ -29,8 +29,9 @@ func TestNotify(t *testing.T) { "someops@example.com", "somedev@example.com", }, - Message: "{{ template \"default.title\" . }}", - Subject: templates.DefaultMessageTitleEmbed, + Message: "{{ template \"default.title\" . }}", + Subject: templates.DefaultMessageTitleEmbed, + Template: templates.DefaultTemplate, } emailSender := receivers.MockNotificationService() diff --git a/templates/default_template.go b/templates/default_template.go index 601a11d8..dc873a1d 100644 --- a/templates/default_template.go +++ b/templates/default_template.go @@ -9,6 +9,7 @@ import ( const ( DefaultMessageTitleEmbed = `{{ template "default.title" . }}` DefaultMessageEmbed = `{{ template "default.message" . }}` + DefaultTemplate = `ng_alert_notification` ) var DefaultTemplateString = `