Skip to content

Commit

Permalink
Merge pull request #111 from suzuki-shunsuke/feat/unify-template
Browse files Browse the repository at this point in the history
feat: add HasDestry variable and remove the template for destroy
  • Loading branch information
suzuki-shunsuke committed Jul 17, 2021
2 parents 3305a3e + fe2d1af commit b7dca74
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 253 deletions.
16 changes: 2 additions & 14 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Placeholder | Usage
`{{ .Stderr }}` | The standard error output of terraform command
`{{ .CombinedOutput }}` | The output of terraform command
`{{ .ExitCode }}` | The exit code of terraform command
`{{ .HasDestroy }}` | Whether there are destroyed resources
`{{ .ErrorMessages }}` | a list of error messages which occur in tfcmt
`{{ .CreatedResources }}` | a list of created resource paths. This variable can be used at only plan
`{{ .UpdatedResources }}` | a list of updated resource paths. This variable can be used at only plan
Expand Down Expand Up @@ -90,6 +91,7 @@ terraform:
{{if .Link}}[CI link]({{.Link}}){{end}}
{{if .HasDestroy}}{{template "deletion_warning" .}}{{end}}
{{template "result" .}}
{{template "updated_resources" .}}
<details><summary>Details (Click me)</summary>
Expand All @@ -106,18 +108,6 @@ terraform:
when_destroy:
label: "{{if .Vars.target}}{{.Vars.target}}/{{end}}destroy"
label_color: d93f0b # red
template: |
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "deletion_warning" .}}
{{template "result" .}}
{{template "updated_resources" .}}
<details><summary>Details (Click me)</summary>
{{wrapCode .CombinedOutput}}
</details>
when_no_changes:
label: "{{if .Vars.target}}{{.Vars.target}}/{{end}}no-changes"
label_color: 0e8a16 # green
Expand Down Expand Up @@ -156,8 +146,6 @@ terraform:
</details>
```

If the plan contains resource deletion, the template of `when_destroy` is used.

If you don't want to update labels, please set `terraform.plan.disable_label: true`.

```yaml
Expand Down
10 changes: 6 additions & 4 deletions example-with-destroy-and-result-labels.tfcmt.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
terraform:
plan:
template: |
{{if .HasDestroy}}
## :warning: WARNING: Resource Deletion will happen :warning:
This plan contains **resource deletion**. Please check the plan result very carefully!
{{else}}
## Plan Result
{{if .Result}}
<pre><code>{{ .Result }}
Expand All @@ -10,14 +15,11 @@ terraform:
<pre><code>{{ .CombinedOutput }}
</pre></code></details>
{{end}}
when_add_or_update_only:
label: "add-or-update"
when_destroy:
label: "destroy"
template: |
## :warning: WARNING: Resource Deletion will happen :warning:
This plan contains **resource deletion**. Please check the plan result very carefully!
when_no_changes:
label: "no-changes"
when_plan_error:
Expand Down
9 changes: 4 additions & 5 deletions pkg/cli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ func cmdPlan(ctx *cli.Context) error {
}

t := &controller.Controller{
Config: cfg,
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(cfg.Terraform.Plan.Template),
DestroyWarningTemplate: terraform.NewDestroyWarningTemplate(cfg.Terraform.Plan.WhenDestroy.Template),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(cfg.Terraform.Plan.WhenParseError.Template),
Config: cfg,
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(cfg.Terraform.Plan.Template),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(cfg.Terraform.Plan.WhenParseError.Template),
}
args := ctx.Args()

Expand Down
5 changes: 2 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ type WhenAddOrUpdateOnly struct {

// WhenDestroy is a configuration to notify the plan result contains destroy operation
type WhenDestroy struct {
Label string
Template string
Color string `yaml:"label_color"`
Label string
Color string `yaml:"label_color"`
}

// WhenNoChanges is a configuration to add a label when the plan result contains no change
Expand Down
26 changes: 20 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package config
import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/suzuki-shunsuke/tfcmt/pkg/domain"
)

Expand Down Expand Up @@ -53,13 +53,27 @@ func TestLoadFile(t *testing.T) {
cfg: Config{
Terraform: Terraform{
Plan: Plan{
Template: "## Plan Result\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .CombinedOutput }}\n</pre></code></details>\n",
Template: `{{if .HasDestroy}}
## :warning: WARNING: Resource Deletion will happen :warning:
This plan contains **resource deletion**. Please check the plan result very carefully!
{{else}}
## Plan Result
{{if .Result}}
<pre><code>{{ .Result }}
</pre></code>
{{end}}
<details><summary>Details (Click me)</summary>
<pre><code>{{ .CombinedOutput }}
</pre></code></details>
{{end}}
`,
WhenAddOrUpdateOnly: WhenAddOrUpdateOnly{
Label: "add-or-update",
},
WhenDestroy: WhenDestroy{
Label: "destroy",
Template: "## :warning: WARNING: Resource Deletion will happen :warning:\n\nThis plan contains **resource deletion**. Please check the plan result very carefully!\n",
Label: "destroy",
},
WhenPlanError: WhenPlanError{
Label: "error",
Expand Down Expand Up @@ -102,8 +116,8 @@ func TestLoadFile(t *testing.T) {
if err := cfg.LoadFile(testCase.file); err == nil {
if !testCase.ok {
t.Error("got no error but want error")
} else if !reflect.DeepEqual(cfg, testCase.cfg) {
t.Errorf("got %#v but want: %#v", cfg, testCase.cfg)
} else if diff := cmp.Diff(cfg, testCase.cfg); diff != "" {
t.Errorf(diff)
}
} else {
if testCase.ok {
Expand Down
26 changes: 12 additions & 14 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
)

type Controller struct {
Config config.Config
Parser terraform.Parser
Template *terraform.Template
DestroyWarningTemplate *terraform.Template
ParseErrorTemplate *terraform.Template
Config config.Config
Parser terraform.Parser
Template *terraform.Template
ParseErrorTemplate *terraform.Template
}

type Command struct {
Expand Down Expand Up @@ -179,15 +178,14 @@ func (ctrl *Controller) getNotifier(ctx context.Context) (notifier.Notifier, err
Revision: ctrl.Config.CI.SHA,
Number: ctrl.Config.CI.PRNumber,
},
CI: ctrl.Config.CI.Link,
Parser: ctrl.Parser,
UseRawOutput: ctrl.Config.Terraform.UseRawOutput,
Template: ctrl.Template,
DestroyWarningTemplate: ctrl.DestroyWarningTemplate,
ParseErrorTemplate: ctrl.ParseErrorTemplate,
ResultLabels: labels,
Vars: ctrl.Config.Vars,
Templates: ctrl.Config.Templates,
CI: ctrl.Config.CI.Link,
Parser: ctrl.Parser,
UseRawOutput: ctrl.Config.Terraform.UseRawOutput,
Template: ctrl.Template,
ParseErrorTemplate: ctrl.ParseErrorTemplate,
ResultLabels: labels,
Vars: ctrl.Config.Vars,
Templates: ctrl.Config.Templates,
})
if err != nil {
return nil, err
Expand Down
7 changes: 2 additions & 5 deletions pkg/notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ type Config struct {
CI string
Parser terraform.Parser
// Template is used for all Terraform command output
Template *terraform.Template
// DestroyWarningTemplate is used only for additional warning
// the plan result contains destroy operation
DestroyWarningTemplate *terraform.Template
ParseErrorTemplate *terraform.Template
Template *terraform.Template
ParseErrorTemplate *terraform.Template
// ResultLabels is a set of labels to apply depending on the plan result
ResultLabels ResultLabels
Vars map[string]string
Expand Down
4 changes: 1 addition & 3 deletions pkg/notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i

_, isPlan := parser.(*terraform.PlanParser)
if isPlan {
if result.HasDestroy {
template = g.client.Config.DestroyWarningTemplate
}
if cfg.PR.IsNumber() && cfg.ResultLabels.HasAnyLabelDefined() {
errMsgs = append(errMsgs, g.updateLabels(ctx, result)...)
}
Expand All @@ -50,6 +47,7 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
ChangedResult: result.ChangedResult,
ChangeOutsideTerraform: result.OutsideTerraform,
Warning: result.Warning,
HasDestroy: result.HasDestroy,
Link: cfg.CI,
UseRawOutput: cfg.UseRawOutput,
Vars: cfg.Vars,
Expand Down
14 changes: 6 additions & 8 deletions pkg/notifier/github/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ func TestNotifyNotify(t *testing.T) {
Revision: "",
Number: 1,
},
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
DestroyWarningTemplate: terraform.NewDestroyWarningTemplate(terraform.DefaultDestroyWarningTemplate),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
},
paramExec: notifier.ParamExec{
Stdout: "Plan: 1 to add, 1 to destroy",
Expand Down Expand Up @@ -191,10 +190,9 @@ func TestNotifyNotify(t *testing.T) {
Revision: "",
Number: 1,
},
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
DestroyWarningTemplate: terraform.NewDestroyWarningTemplate(terraform.DefaultDestroyWarningTemplate),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
ParseErrorTemplate: terraform.NewPlanParseErrorTemplate(terraform.DefaultPlanTemplate),
},
paramExec: notifier.ParamExec{
Stdout: "Plan: 1 to add, 1 to destroy",
Expand Down
28 changes: 3 additions & 25 deletions pkg/terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
{{if .Link}}[CI link]({{.Link}}){{end}}
{{if .HasDestroy}}{{template "deletion_warning" .}}{{end}}
{{template "result" .}}
{{template "updated_resources" .}}
<details><summary>Details (Click me)</summary>
Expand Down Expand Up @@ -44,21 +45,6 @@ const (
* {{. -}}
{{- end}}{{end}}`

// DefaultDestroyWarningTemplate is a default template for terraform plan
DefaultDestroyWarningTemplate = `
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "deletion_warning" .}}
{{template "result" .}}
{{template "updated_resources" .}}
<details><summary>Details (Click me)</summary>
{{wrapCode .CombinedOutput}}
</details>
`

// DefaultPlanParseErrorTemplate is a default template for terraform plan parse error
DefaultPlanParseErrorTemplate = `
{{template "plan_title" .}}
Expand Down Expand Up @@ -94,6 +80,7 @@ type CommonTemplate struct {
Warning string
Link string
UseRawOutput bool
HasDestroy bool
Vars map[string]string
Templates map[string]string
Stdout string
Expand Down Expand Up @@ -123,16 +110,6 @@ func NewPlanTemplate(template string) *Template {
}
}

// NewDestroyWarningTemplate is DestroyWarningTemplate initializer
func NewDestroyWarningTemplate(template string) *Template {
if template == "" {
template = DefaultDestroyWarningTemplate
}
return &Template{
Template: template,
}
}

// NewApplyTemplate is ApplyTemplate initializer
func NewApplyTemplate(template string) *Template {
if template == "" {
Expand Down Expand Up @@ -220,6 +197,7 @@ func (t *Template) Execute() (string, error) {
"UpdatedResources": t.UpdatedResources,
"DeletedResources": t.DeletedResources,
"ReplacedResources": t.ReplacedResources,
"HasDestroy": t.HasDestroy,
}

templates := map[string]string{
Expand Down

0 comments on commit b7dca74

Please sign in to comment.