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: change the behavior of destroy warning #32

Merged
merged 1 commit into from
Jan 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions COMPARED_WITH_TFNOTIFY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ tfcmt isn't compatible with tfnotify.
* configuration file name is changed
* command usage is changed
* don't remove duplicated comments
* change the behavior of deletion warning

### don't support platforms which we don't use

Expand Down Expand Up @@ -197,6 +198,35 @@ notifier:
token: $GITHUB_TOKEN
```

### change the behavior of deletion warning

tfnotify posts a deletion warning comment as the other comment.
tfcmt posts only one comment whose template is `when_destroy.template`.

```yaml
when_destroy:
label: "destroy"
label_color: "d93f0b" # red
template: |
{{ .Title }}

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

This plan contains **resource deletion**. Please check the plan result very carefully!

{{ .Message }}
{{if .Result}}
<pre><code>{{ .Result }}
</pre></code>
{{end}}
<details><summary>Details (Click me)</summary>

<pre><code>{{ .Body }}
</pre></code></details>
```

And the default title of destroy warning is changed to `## :warning: Resource Deletion will happen :warning:`.

## Others

* refactoring
Expand Down
28 changes: 1 addition & 27 deletions notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
_, isPlan := parser.(*terraform.PlanParser)
if isPlan {
if result.HasDestroy && cfg.WarnDestroy {
// Notify destroy warning as a new comment before normal plan result
if err := g.notifyDestoryWarning(ctx, body, result); err != nil {
return result.ExitCode, err
}
template = g.client.Config.DestroyWarningTemplate
}
if cfg.PR.IsNumber() && cfg.ResultLabels.HasAnyLabelDefined() {
var (
Expand Down Expand Up @@ -133,29 +130,6 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
})
}

func (g *NotifyService) notifyDestoryWarning(ctx context.Context, body string, result terraform.ParseResult) error {
cfg := g.client.Config
destroyWarningTemplate := g.client.Config.DestroyWarningTemplate
destroyWarningTemplate.SetValue(terraform.CommonTemplate{
Title: cfg.PR.DestroyWarningTitle,
Message: cfg.PR.DestroyWarningMessage,
Result: result.Result,
Body: body,
Link: cfg.CI,
UseRawOutput: cfg.UseRawOutput,
Vars: cfg.Vars,
})
body, err := destroyWarningTemplate.Execute()
if err != nil {
return err
}

return g.client.Comment.Post(ctx, body, PostOptions{
Number: cfg.PR.Number,
Revision: cfg.PR.Revision,
})
}

func (g *NotifyService) removeResultLabels(ctx context.Context, label string) (string, error) {
cfg := g.client.Config
labels, _, err := g.client.API.IssuesListLabels(ctx, cfg.PR.Number, nil)
Expand Down
2 changes: 1 addition & 1 deletion terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// DefaultPlanTitle is a default title for terraform plan
DefaultPlanTitle = "## Plan result"
// DefaultDestroyWarningTitle is a default title of destroy warning
DefaultDestroyWarningTitle = "## WARNING: Resource Deletion will happen"
DefaultDestroyWarningTitle = "## :warning: Resource Deletion will happen :warning:"
// DefaultApplyTitle is a default title for terraform apply
DefaultApplyTitle = "## Apply result"

Expand Down
2 changes: 1 addition & 1 deletion terraform/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestDestroyWarningTemplateExecute(t *testing.T) {
template: DefaultDestroyWarningTemplate,
value: CommonTemplate{},
resp: `
## WARNING: Resource Deletion will happen
## :warning: Resource Deletion will happen :warning:

This plan contains resource delete operation. Please check the plan result very carefully!

Expand Down