Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add templates config #51

Merged
merged 4 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions COMPARED_WITH_TFNOTIFY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -318,6 +319,29 @@ 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 and builtin templates

[#50](https:/suzuki-shunsuke/tfcmt/issues/50) [#51](https:/suzuki-shunsuke/tfcmt/pull/51)

ex.

```yaml
templates:
title: "## Plan Result ({{.Vars.target}})"
terraform:
plan:
template: |
{{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:/suzuki-shunsuke/tfnotify/pull/32)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
94 changes: 48 additions & 46 deletions terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
<pre><code>{{ .Result }}
</code></pre>
{{end}}
{{if .CreatedResources}}
* Create
{{- range .CreatedResources}}
* {{.}}
{{- end}}{{end}}{{if .UpdatedResources}}
* Update
{{- range .UpdatedResources}}
* {{.}}
{{- end}}{{end}}
{{template "result" .}}
{{template "updated_resources" .}}
<details><summary>Details (Click me)</summary>
{{wrapCode .Body}}
</details>
Expand All @@ -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}}
<pre><code>{{ .Result }}
</code></pre>
{{end}}
{{template "result" .}}

<details><summary>Details (Click me)</summary>
{{wrapCode .Body}}
Expand All @@ -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}}
<pre><code>{{ .Result }}
</code></pre>
{{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><summary>Details (Click me)</summary>
{{wrapCode .Body}}
</details>
`

DefaultPlanParseErrorTemplate = `
## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}
{{template "plan_title" .}}

[CI link]({{ .Link }})

Expand Down Expand Up @@ -126,6 +91,7 @@ type CommonTemplate struct {
Link string
UseRawOutput bool
Vars map[string]string
Templates map[string]string
Stdout string
Stderr string
CombinedOutput string
Expand Down Expand Up @@ -250,7 +216,36 @@ func (t *Template) Execute() (string, error) {
"ReplacedResources": t.ReplacedResources,
}

resp, err := generateOutput("default", t.Template, 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}}<pre><code>{{ .Result }}</code></pre>{{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
}
Expand All @@ -262,3 +257,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
}
25 changes: 4 additions & 21 deletions terraform/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func TestPlanTemplateExecute(t *testing.T) {

[CI link]()


<pre><code>result
</code></pre>

<pre><code>result</code></pre>

<details><summary>Details (Click me)</summary>

Expand Down Expand Up @@ -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!


Expand All @@ -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!


<pre><code>This is a &#34;result&#34;.
</code></pre>
<pre><code>This is a &#34;result&#34;.</code></pre>


<details><summary>Details (Click me)</summary>
Expand All @@ -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!


<pre><code>This is a "result".
</code></pre>
<pre><code>This is a "result".</code></pre>


<details><summary>Details (Click me)</summary>
Expand All @@ -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!


Expand All @@ -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!


Expand Down Expand Up @@ -394,10 +380,7 @@ func TestApplyTemplateExecute(t *testing.T) {

[CI link]()


<pre><code>result
</code></pre>

<pre><code>result</code></pre>

<details><summary>Details (Click me)</summary>

Expand Down