Skip to content

Commit

Permalink
feat: add the variable "target" to default template and default label…
Browse files Browse the repository at this point in the history
… name

BREAKING CHANGE: the variable "target" is added to default template and default label name
  • Loading branch information
suzuki-shunsuke committed Jan 19, 2021
1 parent 77a665c commit d36943a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions COMPARED_WITH_TFNOTIFY.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ This feature is useful especially for Monorepo.
tfcmt supports to pass variables to template by `-var <name>:<value>` options.
We can access the variable in the template by `{{.Vars.<variable name>}}`.

The variable `target` has a special meaning.
This variable is used at the default template and default label name.
This is useful for Monorepo. By setting `target`, we can distinguish the comment and label of each service.
When this variable isn't set, this is just ignored.

## Feature: Add template variables

* Stdout: standard output of terraform command
Expand Down
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (t *tfcmt) renderGitHubLabels() (github.ResultLabels, error) {
PlanErrorLabelColor: t.config.Terraform.Plan.WhenPlanError.Color,
}

target, ok := t.config.Vars["target"]
if !ok {
target = ""
}

if labels.AddOrUpdateLabelColor == "" {
labels.AddOrUpdateLabelColor = "1d76db" // blue
}
Expand All @@ -81,7 +86,11 @@ func (t *tfcmt) renderGitHubLabels() (github.ResultLabels, error) {
}

if t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label == "" {
labels.AddOrUpdateLabel = "add-or-update"
if target == "" {
labels.AddOrUpdateLabel = "add-or-update"
} else {
labels.AddOrUpdateLabel = target + "/add-or-update"
}
} else {
addOrUpdateLabel, err := t.renderTemplate(t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label)
if err != nil {
Expand All @@ -91,7 +100,11 @@ func (t *tfcmt) renderGitHubLabels() (github.ResultLabels, error) {
}

if t.config.Terraform.Plan.WhenDestroy.Label == "" {
labels.DestroyLabel = "destroy"
if target == "" {
labels.DestroyLabel = "destroy"
} else {
labels.DestroyLabel = target + "/destroy"
}
} else {
destroyLabel, err := t.renderTemplate(t.config.Terraform.Plan.WhenDestroy.Label)
if err != nil {
Expand All @@ -101,7 +114,11 @@ func (t *tfcmt) renderGitHubLabels() (github.ResultLabels, error) {
}

if t.config.Terraform.Plan.WhenNoChanges.Label == "" {
labels.NoChangesLabel = "no-changes"
if target == "" {
labels.NoChangesLabel = "no-changes"
} else {
labels.NoChangesLabel = "/no-changes"
}
} else {
nochangesLabel, err := t.renderTemplate(t.config.Terraform.Plan.WhenNoChanges.Label)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
const (
// DefaultPlanTemplate is a default template for terraform plan
DefaultPlanTemplate = `
## Plan Result
## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}
[CI link]({{ .Link }})
Expand Down Expand Up @@ -40,7 +40,7 @@ const (

// DefaultApplyTemplate is a default template for terraform apply
DefaultApplyTemplate = `
## Apply Result
## Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}}
[CI link]({{ .Link }})
Expand All @@ -60,7 +60,7 @@ const (

// DefaultDestroyWarningTemplate is a default template for terraform plan
DefaultDestroyWarningTemplate = `
## :warning: Plan Result: Resource Deletion will happen :warning:
## :warning: Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}: Resource Deletion will happen :warning:
[CI link]({{ .Link }})
Expand Down Expand Up @@ -93,7 +93,7 @@ This plan contains resource delete operation. Please check the plan result very
`

DefaultPlanParseErrorTemplate = `
## Plan Result
## Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}
[CI link]({{ .Link }})
Expand All @@ -105,7 +105,7 @@ It failed to parse the result.
`

DefaultApplyParseErrorTemplate = `
## Apply Result
## Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}}
[CI link]({{ .Link }})
Expand Down

0 comments on commit d36943a

Please sign in to comment.