feat(notification): email subject customization

This commit is contained in:
Nicolas Carlier 2021-05-19 18:44:30 +00:00
parent 25fe46c731
commit ed67fc72f6
3 changed files with 32 additions and 8 deletions

View File

@ -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

View File

@ -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)

View File

@ -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
}