From 2bcb4370f1b14f9ef993c14569e33600ea3fba0d Mon Sep 17 00:00:00 2001 From: michaelact <86778470+michaelact@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:03:34 +0700 Subject: [PATCH 1/7] feat(smtp): subject templating instead of hard-coded --- smtp/main.go | 32 +++++++++++++++++++++++++------- smtp/main_test.go | 44 ++++++++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/smtp/main.go b/smtp/main.go index dd49c87f..510bd419 100644 --- a/smtp/main.go +++ b/smtp/main.go @@ -18,7 +18,8 @@ import ( "bytes" "context" "fmt" - "html/template" + htmlTemplate "html/template" + textTemplate "text/template" "mime/quotedprintable" "net/smtp" "strings" @@ -41,15 +42,16 @@ func main() { type smtpNotifier struct { filter notifiers.EventFilter - tmpl *template.Template + htmlTmpl *htmlTemplate.Template + textTmpl *textTemplate.Template mcfg mailConfig br notifiers.BindingResolver tmplView *notifiers.TemplateView } type mailConfig struct { - server, port, sender, from, password string - recipients []string + server, port, sender, from, password, subject string + recipients []string } func (s *smtpNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, cfgTemplate string, sg notifiers.SecretGetter, br notifiers.BindingResolver) error { @@ -58,11 +60,19 @@ func (s *smtpNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, cfgTemp return fmt.Errorf("failed to create CELPredicate: %w", err) } s.filter = prd - tmpl, err := template.New("email_template").Parse(cfgTemplate) + htmlTmpl, err := htmlTemplate.New("email_template").Parse(cfgTemplate) if err != nil { return fmt.Errorf("failed to parse HTML email template: %w", err) } - s.tmpl = tmpl + s.htmlTmpl = htmlTmpl + + if _, subjectFound := cfg.Spec.Notification.Delivery["Subject"]; subjectFound { + textTmpl, err := textTemplate.New("subject_template").Parse(cfg.Spec.Notification.Delivery["Subject"].(string)) + if err != nil { + return fmt.Errorf("failed to parse TEXT subject template: %w", err) + } + s.textTmpl = textTmpl + } mcfg, err := getMailConfig(ctx, sg, cfg.Spec) if err != nil { @@ -175,11 +185,19 @@ func (s *smtpNotifier) buildEmail() (string, error) { build.LogUrl = logURL body := new(bytes.Buffer) - if err := s.tmpl.Execute(body, s.tmplView); err != nil { + if err := s.htmlTmpl.Execute(body, s.tmplView); err != nil { return "", err } subject := fmt.Sprintf("Cloud Build [%s]: %s", build.ProjectId, build.Id) + if s.textTmpl != nil { + subjectTmpl := new(bytes.Buffer) + if err := s.textTmpl.Execute(subjectTmpl, s.tmplView); err != nil { + return "", err + } + + subject = subjectTmpl.String() + } header := make(map[string]string) if s.mcfg.from != s.mcfg.sender { diff --git a/smtp/main_test.go b/smtp/main_test.go index 229349b2..a58a9e97 100644 --- a/smtp/main_test.go +++ b/smtp/main_test.go @@ -47,16 +47,16 @@ const htmlBody = `
Status | -{{.Params.buildStatus}} | +Status | +{{.Params.buildStatus}} |
Log URL | -Click Here | +Log URL | +Click Here |