Skip to content

Commit

Permalink
Merge pull request #337 from djerfy/feat/email-subject-prefix
Browse files Browse the repository at this point in the history
feat: add titlePrefix option (email reports)
  • Loading branch information
fjogeleit committed Aug 31, 2023
2 parents 8fa2237 + dcde9e3 commit 0659c27
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions charts/policy-reporter/config-email-reports.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
emailReports:
clusterName: {{ .Values.emailReports.clusterName }}
titlePrefix: {{ .Values.emailReports.titlePrefix }}
{{- with .Values.emailReports.smtp }}
smtp:
{{- toYaml . | nindent 4 }}
Expand Down
1 change: 1 addition & 0 deletions charts/policy-reporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ policyPriorities: {}

emailReports:
clusterName: "" # (optional) - displayed in the email report if configured
titlePrefix: "Report" # title prefix in the email subject
smtp:
secret: "" # (optional) secret name to provide the complete or partial SMTP configuration
host: ""
Expand Down
1 change: 1 addition & 0 deletions manifest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ To configure your SMTP server and receiver emails use the following configuratio
```yaml
emailReports:
clusterName: '' # optional clustername shown in the Report
titlePrefix: 'Report' # title prefix in the email subject
smtp:
host: ''
port: 465
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type EmailReports struct {
Summary EmailReport `mapstructure:"summary"`
Violations EmailReport `mapstructure:"violations"`
ClusterName string `mapstructure:"clusterName"`
TitlePrefix string `mapstructure:"titlePrefix"`
}

// API configuration
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func (r *Resolver) SummaryReporter() *summary.Reporter {
return summary.NewReporter(
r.config.EmailReports.Templates.Dir,
r.config.EmailReports.ClusterName,
r.config.EmailReports.TitlePrefix,
)
}

Expand All @@ -372,6 +373,7 @@ func (r *Resolver) ViolationsReporter() *violations.Reporter {
return violations.NewReporter(
r.config.EmailReports.Templates.Dir,
r.config.EmailReports.ClusterName,
r.config.EmailReports.TitlePrefix,
)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/email/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Report struct {
Message string
Format string
ClusterName string
TitlePrefix string
}

type Reporter interface {
Expand Down
10 changes: 6 additions & 4 deletions pkg/email/summary/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Reporter struct {
templateDir string
clusterName string
titlePrefix string
}

func (o *Reporter) Report(sources []Source, format string) (email.Report, error) {
Expand All @@ -24,19 +25,20 @@ func (o *Reporter) Report(sources []Source, format string) (email.Report, error)
err = templ.Execute(b, struct {
Sources []Source
ClusterName string
}{Sources: sources, ClusterName: o.clusterName})
TitlePrefix string
}{Sources: sources, ClusterName: o.clusterName, TitlePrefix: o.titlePrefix})
if err != nil {
return email.Report{}, err
}

return email.Report{
ClusterName: o.clusterName,
Title: "Summary Report from " + time.Now().Format("2006-01-02"),
Title: o.titlePrefix + " (summary) on " + o.clusterName + " from " + time.Now().Format("2006-01-02"),
Message: b.String(),
Format: format,
}, nil
}

func NewReporter(templateDir, clusterName string) *Reporter {
return &Reporter{templateDir, clusterName}
func NewReporter(templateDir, clusterName string, titlePrefix string) *Reporter {
return &Reporter{templateDir, clusterName, titlePrefix}
}
5 changes: 4 additions & 1 deletion pkg/email/summary/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_CreateReport(t *testing.T) {

fmt.Println(path)

reporter := summary.NewReporter("../../../templates", "Cluster")
reporter := summary.NewReporter("../../../templates", "Cluster", "Report")
report, err := reporter.Report(data, "html")
if err != nil {
t.Fatalf("unexpected error: %s", err)
Expand All @@ -50,6 +50,9 @@ func Test_CreateReport(t *testing.T) {
if report.ClusterName != "Cluster" {
t.Fatal("expected clustername to be set")
}
if report.TitlePrefix != "Report" {
t.Fatal("expected titleprefix to be set")
}
if report.Format != "html" {
t.Fatal("expected format to be set")
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/email/violations/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Reporter struct {
templateDir string
clusterName string
titlePrefix string
}

func (o *Reporter) Report(sources []Source, format string) (email.Report, error) {
Expand All @@ -36,19 +37,20 @@ func (o *Reporter) Report(sources []Source, format string) (email.Report, error)
Sources []Source
Status []string
ClusterName string
}{Sources: sources, Status: []string{"warn", "fail", "error"}, ClusterName: o.clusterName})
TitlePrefix string
}{Sources: sources, Status: []string{"warn", "fail", "error"}, ClusterName: o.clusterName, TitlePrefix: o.titlePrefix})
if err != nil {
return email.Report{}, err
}

return email.Report{
ClusterName: o.clusterName,
Title: "Summary Report from " + time.Now().Format("2006-01-02"),
Title: o.titlePrefix + " (violations) on " + o.clusterName + " from " + time.Now().Format("2006-01-02"),
Message: b.String(),
Format: format,
}, nil
}

func NewReporter(templateDir string, clusterName string) *Reporter {
return &Reporter{templateDir, clusterName}
func NewReporter(templateDir string, clusterName string, titlePrefix string) *Reporter {
return &Reporter{templateDir, clusterName, titlePrefix}
}
5 changes: 4 additions & 1 deletion pkg/email/violations/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_CreateReport(t *testing.T) {

fmt.Println(path)

reporter := violations.NewReporter("../../../templates", "Cluster")
reporter := violations.NewReporter("../../../templates", "Cluster", "Report")
report, err := reporter.Report(data, "html")
if err != nil {
t.Fatalf("unexpected error: %s", err)
Expand All @@ -50,6 +50,9 @@ func Test_CreateReport(t *testing.T) {
if report.ClusterName != "Cluster" {
t.Fatal("expected clustername to be set")
}
if report.TitlePrefix != "Report" {
t.Fatal("expected titleprefix to be")
}
if report.Format != "html" {
t.Fatal("expected format to be set")
}
Expand Down

0 comments on commit 0659c27

Please sign in to comment.