From ed67fc72f6a9512ca040edfce3f4119ac113eb5a Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Wed, 19 May 2021 18:44:30 +0000 Subject: [PATCH] feat(notification): email subject customization --- README.md | 3 +++ pkg/model/work_request.go | 14 ++++++++++++++ pkg/notification/smtp_notifier.go | 23 +++++++++++++++-------- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f746b19..4616140 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ $ go get -v github.com/ncarlier/webhookd ```bash $ sudo curl -s https://raw.githubusercontent.com/ncarlier/webhookd/master/install.sh | bash +or +$ curl -sf https://gobinaries.com/ncarlier/za | sh ``` **Or** use Docker: @@ -269,6 +271,7 @@ Options (using query parameters): - `password`: SMTP password (not set by default) - `conn`: SMTP connection type (`tls`, `tls-insecure` or by default: `plain`) - `from`: Sender email (by default: `noreply@nunux.org`) +- `subject`: Email subject (by default: `[whd-notification] {name}#{id} {status}`) ### Authentication diff --git a/pkg/model/work_request.go b/pkg/model/work_request.go index 440a0f8..a4c191b 100644 --- a/pkg/model/work_request.go +++ b/pkg/model/work_request.go @@ -93,6 +93,20 @@ func (wr *WorkRequest) IsTerminated() bool { return wr.Status == Success || wr.Status == Error } +// StatusLabel return work status as string +func (wr *WorkRequest) StatusLabel() string { + switch wr.Status { + case Error: + return "error" + case Success: + return "success" + case Running: + return "running" + default: + return "idle" + } +} + // GetLogContent returns work logs filtered with the prefix func (wr *WorkRequest) GetLogContent(prefixFilter string) string { file, err := os.Open(wr.LogFilename) diff --git a/pkg/notification/smtp_notifier.go b/pkg/notification/smtp_notifier.go index 1a4da5f..24a3a4a 100644 --- a/pkg/notification/smtp_notifier.go +++ b/pkg/notification/smtp_notifier.go @@ -6,6 +6,7 @@ import ( "net" "net/smtp" "net/url" + "strconv" "strings" "time" @@ -21,6 +22,7 @@ type SMTPNotifier struct { Conn string From string To string + Subject string PrefixFilter string } @@ -34,7 +36,8 @@ func newSMTPNotifier(uri *url.URL) *SMTPNotifier { Conn: getValueOrAlt(q, "conn", "plain"), From: getValueOrAlt(q, "from", "noreply@nunux.org"), To: uri.Opaque, - PrefixFilter: getValueOrAlt(q, "prefix", "notify:"), + Subject: getValueOrAlt(uri.Query(), "subject", "[whd-notification] {name}#{id} {status}"), + PrefixFilter: getValueOrAlt(uri.Query(), "prefix", "notify:"), } } @@ -44,13 +47,10 @@ func (n *SMTPNotifier) buildEmailPayload(work *model.WorkRequest) string { if strings.TrimSpace(body) == "" { return "" } - // Get email subject - var subject string - if work.Status == model.Success { - subject = fmt.Sprintf("Webhook %s#%d SUCCESS.", work.Name, work.ID) - } else { - subject = fmt.Sprintf("Webhook %s#%d FAILED.", work.Name, work.ID) - } + + // Build email subject + subject := buildSubject(n.Subject, work) + // Build email headers headers := make(map[string]string) headers["From"] = n.From @@ -132,3 +132,10 @@ func (n *SMTPNotifier) Notify(work *model.WorkRequest) error { // Send the QUIT command and close the connection. return client.Quit() } + +func buildSubject(template string, work *model.WorkRequest) string { + result := strings.ReplaceAll(template, "{name}", work.Name) + result = strings.ReplaceAll(result, "{id}", strconv.FormatUint(uint64(work.ID), 10)) + result = strings.ReplaceAll(result, "{status}", work.StatusLabel()) + return result +}