Skip to content

Commit

Permalink
Add Template field in email receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
diorcety authored and Yann Diorcet committed Mar 15, 2024
1 parent bc62236 commit 2706d62
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
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
4 changes: 4 additions & 0 deletions receivers/email/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestNewConfig(t *testing.T) {
},
Message: "",
Subject: templates.DefaultMessageTitleEmbed,
Template: templates.DefaultTemplate,
},
},
{
Expand All @@ -56,6 +57,7 @@ func TestNewConfig(t *testing.T) {
},
Message: "",
Subject: templates.DefaultMessageTitleEmbed,
Template: templates.DefaultTemplate,
},
},
{
Expand All @@ -68,6 +70,7 @@ func TestNewConfig(t *testing.T) {
},
Message: "",
Subject: templates.DefaultMessageTitleEmbed,
Template: templates.DefaultTemplate,
},
},
{
Expand All @@ -80,6 +83,7 @@ func TestNewConfig(t *testing.T) {
},
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
1 change: 1 addition & 0 deletions receivers/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestNotify(t *testing.T) {
},
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

0 comments on commit 2706d62

Please sign in to comment.