Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Template field in email receiver #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions receivers/email/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Addresses []string
Message string
Subject string
Template string
}

func NewConfig(jsonData json.RawMessage) (Config, error) {
Expand All @@ -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
Expand All @@ -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
}

Expand Down
20 changes: 12 additions & 8 deletions receivers/email/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
{
Expand All @@ -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,
},
},
{
Expand All @@ -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,
},
},
{
Expand All @@ -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,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion receivers/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions receivers/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions templates/default_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const (
DefaultMessageTitleEmbed = `{{ template "default.title" . }}`
DefaultMessageEmbed = `{{ template "default.message" . }}`
DefaultTemplate = `ng_alert_notification`
)

var DefaultTemplateString = `
Expand Down