Skip to content

Commit

Permalink
be explicit with cases, update readme and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
drlau committed May 28, 2020
1 parent 0a6026d commit a49b211
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 129 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ terraform:
# ...
```

You can also let tfnotify add a label to PRs whose `terraform plan` output result in no change to the current infrastructure. Currently, this feature is for Github labels only.
You can also let tfnotify add a label to PRs depending on the `terraform plan` output result. Currently, this feature is for Github labels only.

```yaml
---
Expand All @@ -174,8 +174,14 @@ terraform:
<pre><code>{{ .Body }}
</pre></code></details>
when_add_or_update:
label: "add-or-update"
when_destroy:
label: "destroy"
when_no_changes:
label: "no-changes"
when_plan_error:
label: "error"
# ...
```

Expand Down
22 changes: 11 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ type Fmt struct {

// Plan is a terraform plan config
type Plan struct {
Template string `yaml:"template"`
WhenChanges WhenChanges `yaml:"when_changes,omitempty"`
WhenDestroy WhenDestroy `yaml:"when_destroy,omitempty"`
WhenError WhenError `yaml:"when_error,omitempty"`
WhenNoChanges WhenNoChanges `yaml:"when_no_changes,omitempty"`
Template string `yaml:"template"`
WhenAddOrUpdate WhenAddOrUpdate `yaml:"when_add_or_update,omitempty"`
WhenDestroy WhenDestroy `yaml:"when_destroy,omitempty"`
WhenNoChanges WhenNoChanges `yaml:"when_no_changes,omitempty"`
WhenPlanError WhenPlanError `yaml:"when_plan_error,omitempty"`
}

// WhenChanges is a configuration to notify the plan result contains new or updated in place resources
type WhenChanges struct {
// WhenAddOrUpdate is a configuration to notify the plan result contains new or updated in place resources
type WhenAddOrUpdate struct {
Label string `yaml:"label,omitempty"`
}

Expand All @@ -99,13 +99,13 @@ type WhenDestroy struct {
Template string `yaml:"template,omitempty"`
}

// WhenError is a configuration to notify the plan result contains new or updated in place resources
type WhenError struct {
// WhenNoChange is a configuration to add a label when the plan result contains no change
type WhenNoChanges struct {
Label string `yaml:"label,omitempty"`
}

// WhenNoChange is a configuration to add a label when the plan result contains no change
type WhenNoChanges struct {
// WhenPlanError is a configuration to notify the plan result returns an error
type WhenPlanError struct {
Label string `yaml:"label,omitempty"`
}

Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ func TestLoadFile(t *testing.T) {
},
Plan: Plan{
Template: "{{ .Title }}\n{{ .Message }}\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .Body }}\n</pre></code></details>\n",
WhenChanges: WhenChanges{
Label: "changes",
WhenAddOrUpdate: WhenAddOrUpdate{
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",
},
WhenError: WhenError{
WhenPlanError: WhenPlanError{
Label: "error",
},
WhenNoChanges: WhenNoChanges{
Expand Down
8 changes: 4 additions & 4 deletions example-with-destroy-and-result-labels.tfnotify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ terraform:
<pre><code>{{ .Body }}
</pre></code></details>
when_changes:
label: "changes"
when_add_or_update:
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_error:
label: "error"
when_no_changes:
label: "no-changes"
when_plan_error:
label: "error"
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ func (t *tfnotify) Run() error {
DestroyWarningTemplate: t.destroyWarningTemplate,
WarnDestroy: t.warnDestroy,
ResultLabels: github.ResultLabels{
ChangesLabel: t.config.Terraform.Plan.WhenChanges.Label,
DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label,
ErrorLabel: t.config.Terraform.Plan.WhenError.Label,
NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label,
AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdate.Label,
DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label,
NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label,
PlanErrorLabel: t.config.Terraform.Plan.WhenPlanError.Label,
},
})
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ func (pr *PullRequest) IsNumber() bool {

// ResultLabels represents the labels to add to the PR depending on the plan result
type ResultLabels struct {
ChangesLabel string
DestroyLabel string
ErrorLabel string
NoChangesLabel string
AddOrUpdateLabel string
DestroyLabel string
NoChangesLabel string
PlanErrorLabel string
}

// HasAnyLabelDefined returns true if any of the internal labels are set
func (r *ResultLabels) HasAnyLabelDefined() bool {
return r.ChangesLabel != "" || r.DestroyLabel != "" || r.ErrorLabel != "" || r.NoChangesLabel != ""
return r.AddOrUpdateLabel != "" || r.DestroyLabel != "" || r.NoChangesLabel != "" || r.PlanErrorLabel != ""
}

// IsResultLabel returns true if a label matches any of the internal labels
func (r *ResultLabels) IsResultLabel(label string) bool {
switch label {
case "":
return false
case r.ChangesLabel, r.DestroyLabel, r.ErrorLabel, r.NoChangesLabel:
case r.AddOrUpdateLabel, r.DestroyLabel, r.NoChangesLabel, r.PlanErrorLabel:
return true
default:
return false
Expand Down
58 changes: 29 additions & 29 deletions notifier/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,28 @@ func TestHasAnyLabelDefined(t *testing.T) {
}{
{
rl: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "no-changes",
PlanErrorLabel: "error",
},
want: true,
},
{
rl: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "",
PlanErrorLabel: "error",
},
want: true,
},
{
rl: ResultLabels{
ChangesLabel: "",
DestroyLabel: "",
ErrorLabel: "",
NoChangesLabel: "",
AddOrUpdateLabel: "",
DestroyLabel: "",
NoChangesLabel: "",
PlanErrorLabel: "",
},
want: false,
},
Expand All @@ -228,40 +228,40 @@ func TestIsResultLabels(t *testing.T) {
}{
{
rl: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "no-changes",
PlanErrorLabel: "error",
},
label: "changes",
label: "add-or-update",
want: true,
},
{
rl: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "no-changes",
PlanErrorLabel: "error",
},
label: "my-label",
want: false,
},
{
rl: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "no-changes",
PlanErrorLabel: "error",
},
label: "",
want: false,
},
{
rl: ResultLabels{
ChangesLabel: "",
DestroyLabel: "",
ErrorLabel: "",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "",
DestroyLabel: "",
NoChangesLabel: "no-changes",
PlanErrorLabel: "",
},
label: "",
want: false,
Expand Down
8 changes: 4 additions & 4 deletions notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (g *NotifyService) Notify(body string) (exit int, err error) {
}
var labelToAdd string

if result.HasChanges {
labelToAdd = cfg.ResultLabels.ChangesLabel
if result.HasAddOrUpdate {
labelToAdd = cfg.ResultLabels.AddOrUpdateLabel
} else if result.HasDestroy {
labelToAdd = cfg.ResultLabels.DestroyLabel
} else if result.HasPlanError {
labelToAdd = cfg.ResultLabels.ErrorLabel
} else if result.HasNoChanges {
labelToAdd = cfg.ResultLabels.NoChangesLabel
} else if result.HasPlanError {
labelToAdd = cfg.ResultLabels.PlanErrorLabel
}

if labelToAdd != "" {
Expand Down
8 changes: 4 additions & 4 deletions notifier/github/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func TestNotifyNotify(t *testing.T) {
Parser: terraform.NewPlanParser(),
Template: terraform.NewPlanTemplate(terraform.DefaultPlanTemplate),
ResultLabels: ResultLabels{
ChangesLabel: "changes",
DestroyLabel: "destroy",
ErrorLabel: "error",
NoChangesLabel: "no-changes",
AddOrUpdateLabel: "add-or-update",
DestroyLabel: "destroy",
NoChangesLabel: "no-changes",
PlanErrorLabel: "error",
},
},
body: "No changes. Infrastructure is up-to-date.",
Expand Down
30 changes: 15 additions & 15 deletions terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type Parser interface {

// ParseResult represents the result of parsed terraform execution
type ParseResult struct {
Result string
HasChanges bool
HasDestroy bool
HasPlanError bool
HasNoChanges bool
ExitCode int
Error error
Result string
HasAddOrUpdate bool
HasDestroy bool
HasNoChanges bool
HasPlanError bool
ExitCode int
Error error
}

// DefaultParser is a parser for terraform commands
Expand Down Expand Up @@ -130,16 +130,16 @@ func (p *PlanParser) Parse(body string) ParseResult {

hasDestroy := p.HasDestroy.MatchString(line)
hasNoChanges := p.HasNoChanges.MatchString(line)
hasChanges := !hasNoChanges && !hasDestroy && !hasPlanError
HasAddOrUpdate := !hasNoChanges && !hasDestroy && !hasPlanError

return ParseResult{
Result: result,
HasChanges: hasChanges,
HasDestroy: hasDestroy,
HasPlanError: hasPlanError,
HasNoChanges: hasNoChanges,
ExitCode: exitCode,
Error: nil,
Result: result,
HasAddOrUpdate: HasAddOrUpdate,
HasDestroy: hasDestroy,
HasNoChanges: hasNoChanges,
HasPlanError: hasPlanError,
ExitCode: exitCode,
Error: nil,
}
}

Expand Down
Loading

0 comments on commit a49b211

Please sign in to comment.