From f97e05e23e70956a1b71e53e97b79e3e238ceda6 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Tue, 19 Jan 2021 19:12:24 +0900 Subject: [PATCH 1/4] feat: add templates config --- config/config.go | 1 + main.go | 1 + notifier/github/client.go | 1 + notifier/github/notify.go | 1 + terraform/template.go | 10 +++++++++- 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 0d97d66a..d3cdf55d 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ type Config struct { Notifier Notifier `yaml:"notifier"` Terraform Terraform `yaml:"terraform"` Vars map[string]string `yaml:"-"` + Templates map[string]string path string } diff --git a/main.go b/main.go index 473e38a0..29efa150 100644 --- a/main.go +++ b/main.go @@ -162,6 +162,7 @@ func (t *tfcmt) getNotifier(ctx context.Context, ci CI) (notifier.Notifier, erro ParseErrorTemplate: t.parseErrorTemplate, ResultLabels: labels, Vars: t.config.Vars, + Templates: t.config.Templates, }) if err != nil { return nil, err diff --git a/notifier/github/client.go b/notifier/github/client.go index 16426550..acec3df7 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -52,6 +52,7 @@ type Config struct { // ResultLabels is a set of labels to apply depending on the plan result ResultLabels ResultLabels Vars map[string]string + Templates map[string]string } // PullRequest represents GitHub Pull Request metadata diff --git a/notifier/github/notify.go b/notifier/github/notify.go index c5e76f1e..a4cf0c0c 100644 --- a/notifier/github/notify.go +++ b/notifier/github/notify.go @@ -50,6 +50,7 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i Link: cfg.CI, UseRawOutput: cfg.UseRawOutput, Vars: cfg.Vars, + Templates: cfg.Templates, Stdout: param.Stdout, Stderr: param.Stderr, CombinedOutput: param.CombinedOutput, diff --git a/terraform/template.go b/terraform/template.go index ddcf32f5..721cf859 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -126,6 +126,7 @@ type CommonTemplate struct { Link string UseRawOutput bool Vars map[string]string + Templates map[string]string Stdout string Stderr string CombinedOutput string @@ -250,7 +251,7 @@ func (t *Template) Execute() (string, error) { "ReplacedResources": t.ReplacedResources, } - resp, err := generateOutput("default", t.Template, data, t.UseRawOutput) + resp, err := generateOutput("default", addTemplates(t.Template, t.Templates), data, t.UseRawOutput) if err != nil { return "", err } @@ -262,3 +263,10 @@ func (t *Template) Execute() (string, error) { func (t *Template) SetValue(ct CommonTemplate) { t.CommonTemplate = ct } + +func addTemplates(tpl string, templates map[string]string) string { + for k, v := range templates { + tpl += `{{define "` + k + `"}}` + v + "{{end}}" + } + return tpl +} From 65047ebbbce4fe2536ad918ea29495f4023bcbc5 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Tue, 19 Jan 2021 19:55:34 +0900 Subject: [PATCH 2/4] docs: update document --- COMPARED_WITH_TFNOTIFY.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/COMPARED_WITH_TFNOTIFY.md b/COMPARED_WITH_TFNOTIFY.md index 9081597d..2e6757b6 100644 --- a/COMPARED_WITH_TFNOTIFY.md +++ b/COMPARED_WITH_TFNOTIFY.md @@ -24,6 +24,7 @@ tfcmt isn't compatible with tfnotify. * Support template functions [sprig](http://masterminds.github.io/sprig/) * [Support to pass variables by -var option](#feature-support-to-pass-variables-by--var-option) * [Add template variables](#feature-add-template-variables) + * [Add templates configuration](#feature-add-templates-configuration) * [Don't recreate labels](#feature-dont-recreate-labels) * [--version option and `version` command](#feature---version-option-and-version-command) * Fixes @@ -318,6 +319,21 @@ When this variable isn't set, this is just ignored. * Vars: variables which are passed by `-var` option * ErrorMessages: a list of error messages which occur in tfcmt +## Feature: Add templates configuration + +[#50](https://github.com/suzuki-shunsuke/tfcmt/issues/50) [#51](https://github.com/suzuki-shunsuke/tfcmt/pull/51) + +ex. + +```yaml +templates: + title: "## Plan Result ({{.Vars.target}})" +terraform: + plan: + template: | + {{template "title" .}} +``` + ## Feature: Don't recreate labels [suzuki-shunsuke/tfnotify#32](https://github.com/suzuki-shunsuke/tfnotify/pull/32) From 6544da14fb8e27438381b3bc2542807cf99e1909 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Tue, 19 Jan 2021 20:19:35 +0900 Subject: [PATCH 3/4] feat: add builtin templates and update document --- COMPARED_WITH_TFNOTIFY.md | 10 ++++- terraform/template.go | 86 ++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/COMPARED_WITH_TFNOTIFY.md b/COMPARED_WITH_TFNOTIFY.md index 2e6757b6..aa087bde 100644 --- a/COMPARED_WITH_TFNOTIFY.md +++ b/COMPARED_WITH_TFNOTIFY.md @@ -319,7 +319,7 @@ When this variable isn't set, this is just ignored. * Vars: variables which are passed by `-var` option * ErrorMessages: a list of error messages which occur in tfcmt -## Feature: Add templates configuration +## Feature: Add templates configuration and builtin templates [#50](https://github.com/suzuki-shunsuke/tfcmt/issues/50) [#51](https://github.com/suzuki-shunsuke/tfcmt/pull/51) @@ -334,6 +334,14 @@ terraform: {{template "title" .}} ``` +The following builtin templates are defined. We can override them. + +* plan_title +* apply_title +* result +* updated_resources +* deletion_warning + ## Feature: Don't recreate labels [suzuki-shunsuke/tfnotify#32](https://github.com/suzuki-shunsuke/tfnotify/pull/32) diff --git a/terraform/template.go b/terraform/template.go index 721cf859..a9cb7410 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -12,23 +12,12 @@ import ( const ( // DefaultPlanTemplate is a default template for terraform plan DefaultPlanTemplate = ` -## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}} +{{template "plan_title" .}} [CI link]({{ .Link }}) -{{if .Result}} -
{{ .Result }}
-
-{{end}} -{{if .CreatedResources}} -* Create -{{- range .CreatedResources}} - * {{.}} -{{- end}}{{end}}{{if .UpdatedResources}} -* Update -{{- range .UpdatedResources}} - * {{.}} -{{- end}}{{end}} +{{template "result" .}} +{{template "updated_resources" .}}
Details (Click me) {{wrapCode .Body}}
@@ -40,14 +29,11 @@ const ( // DefaultApplyTemplate is a default template for terraform apply DefaultApplyTemplate = ` -## Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}} +{{template "apply_title" .}} [CI link]({{ .Link }}) -{{if .Result}} -
{{ .Result }}
-
-{{end}} +{{template "result" .}}
Details (Click me) {{wrapCode .Body}} @@ -60,42 +46,21 @@ const ( // DefaultDestroyWarningTemplate is a default template for terraform plan DefaultDestroyWarningTemplate = ` -## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}} +{{template "plan_title" .}} [CI link]({{ .Link }}) -### :warning: Resource Deletion will happen :warning: - -This plan contains resource delete operation. Please check the plan result very carefully! +{{template "deletion_warning" .}} +{{template "result" .}} -{{if .Result}} -
{{ .Result }}
-
-{{end}} -{{if .CreatedResources}} -* Create -{{- range .CreatedResources}} - * {{.}} -{{- end}}{{end}}{{if .UpdatedResources}} -* Update -{{- range .UpdatedResources}} - * {{.}} -{{- end}}{{end}}{{if .DeletedResources}} -* Delete -{{- range .DeletedResources}} - * {{.}} -{{- end}}{{end}}{{if .ReplacedResources}} -* Replace -{{- range .ReplacedResources}} - * {{.}} -{{- end}}{{end}} +{{template "updated_resources" .}}
Details (Click me) {{wrapCode .Body}}
` DefaultPlanParseErrorTemplate = ` -## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}} +{{template "plan_title" .}} [CI link]({{ .Link }}) @@ -251,7 +216,36 @@ func (t *Template) Execute() (string, error) { "ReplacedResources": t.ReplacedResources, } - resp, err := generateOutput("default", addTemplates(t.Template, t.Templates), data, t.UseRawOutput) + templates := map[string]string{ + "plan_title": "## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}", + "apply_title": "## Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}}", + "result": "{{if .Result}}
{{ .Result }}
{{end}}", + "updated_resources": `{{if .CreatedResources}} +* Create +{{- range .CreatedResources}} + * {{.}} +{{- end}}{{end}}{{if .UpdatedResources}} +* Update +{{- range .UpdatedResources}} + * {{.}} +{{- end}}{{end}}{{if .DeletedResources}} +* Delete +{{- range .DeletedResources}} + * {{.}} +{{- end}}{{end}}{{if .ReplacedResources}} +* Replace +{{- range .ReplacedResources}} + * {{.}} +{{- end}}{{end}}`, + "deletion_warning": `### :warning: Resource Deletion will happen :warning: +This plan contains resource delete operation. Please check the plan result very carefully!`, + } + + for k, v := range t.Templates { + templates[k] = v + } + + resp, err := generateOutput("default", addTemplates(t.Template, templates), data, t.UseRawOutput) if err != nil { return "", err } From 812448ed5cd4840d180113e1a41ac387688bcde3 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Tue, 19 Jan 2021 20:29:21 +0900 Subject: [PATCH 4/4] test: fix tests --- terraform/template_test.go | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/terraform/template_test.go b/terraform/template_test.go index 02b4fa80..25a8f3de 100644 --- a/terraform/template_test.go +++ b/terraform/template_test.go @@ -44,10 +44,7 @@ func TestPlanTemplateExecute(t *testing.T) { [CI link]() - -
result
-
- +
result
Details (Click me) @@ -199,7 +196,6 @@ func TestDestroyWarningTemplateExecute(t *testing.T) { [CI link]() ### :warning: Resource Deletion will happen :warning: - This plan contains resource delete operation. Please check the plan result very carefully! @@ -225,12 +221,8 @@ This plan contains resource delete operation. Please check the plan result very [CI link]() ### :warning: Resource Deletion will happen :warning: - This plan contains resource delete operation. Please check the plan result very carefully! - - -
This is a "result".
-
+
This is a "result".
Details (Click me) @@ -255,12 +247,8 @@ This plan contains resource delete operation. Please check the plan result very [CI link]() ### :warning: Resource Deletion will happen :warning: - This plan contains resource delete operation. Please check the plan result very carefully! - - -
This is a "result".
-
+
This is a "result".
Details (Click me) @@ -284,7 +272,6 @@ This plan contains resource delete operation. Please check the plan result very [CI link]() ### :warning: Resource Deletion will happen :warning: - This plan contains resource delete operation. Please check the plan result very carefully! @@ -310,7 +297,6 @@ This plan contains resource delete operation. Please check the plan result very [CI link]() ### :warning: Resource Deletion will happen :warning: - This plan contains resource delete operation. Please check the plan result very carefully! @@ -394,10 +380,7 @@ func TestApplyTemplateExecute(t *testing.T) { [CI link]() - -
result
-
- +
result
Details (Click me)