Skip to content

Commit

Permalink
feat: add a template variable "ErrorMessages"
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 8, 2021
1 parent 6cf75e2 commit adbbdeb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions COMPARED_WITH_TFNOTIFY.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ We can access the variable in the template by `{{.Vars.<variable name>}}`.
* CombinedOutput: output of terraform command
* ExitCode: exit code of terraform command
* Vars: variables which are passed by `-var` option
* ErrorMessages: a list of error messages which occur in tfcmt

## Feature: Don't recreate labels

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Placeholder | Usage
`{{ .Stderr }}` | The standard error output of terraform command
`{{ .CombinedOutput }}` | The output of terraform command
`{{ .ExitCode }}` | The exit code of terraform command
`{{ .ErrorMessages }}` | a list of error messages which occur in tfcmt

On GitHub, tfcmt can also put a warning message if the plan result contains resource deletion (optional).

Expand Down
18 changes: 14 additions & 4 deletions notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
cfg := g.client.Config
parser := g.client.Config.Parser
template := g.client.Config.Template
var errMsgs []string

body := param.Stdout
result := parser.Parse(body)
Expand Down Expand Up @@ -61,22 +62,28 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i

currentLabelColor, err := g.removeResultLabels(ctx, labelToAdd)
if err != nil {
log.Printf("[ERROR][tfcmt] remove labels: %v", err)
msg := "remove labels: " + err.Error()
log.Printf("[ERROR][tfcmt] " + msg)
errMsgs = append(errMsgs, msg)
}

if labelToAdd != "" {
if currentLabelColor == "" {
labels, _, err := g.client.API.IssuesAddLabels(ctx, cfg.PR.Number, []string{labelToAdd})
if err != nil {
log.Printf("[ERROR][tfcmt] add a label %s: %v", labelToAdd, err)
msg := "add a label " + labelToAdd + ": " + err.Error()
log.Printf("[ERROR][tfcmt] " + msg)
errMsgs = append(errMsgs, msg)
}
if labelColor != "" {
// set the color of label
for _, label := range labels {
if labelToAdd == label.GetName() {
if label.GetColor() != labelColor {
if _, _, err := g.client.API.IssuesUpdateLabel(ctx, labelToAdd, labelColor); err != nil {
log.Printf("[ERROR][tfcmt] update a label color(name: %s, color: %s): %v", labelToAdd, labelColor, err)
msg := "update a label color (name: " + labelToAdd + ", color: " + labelColor + "): " + err.Error()
log.Printf("[ERROR][tfcmt] " + msg)
errMsgs = append(errMsgs, msg)
}
}
}
Expand All @@ -85,7 +92,9 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
} else if labelColor != "" && labelColor != currentLabelColor {
// set the color of label
if _, _, err := g.client.API.IssuesUpdateLabel(ctx, labelToAdd, labelColor); err != nil {
log.Printf("[ERROR][tfcmt] update a label color(name: %s, color: %s): %v", labelToAdd, labelColor, err)
msg := "update a label color (name: " + labelToAdd + ", color: " + labelColor + "): " + err.Error()
log.Printf("[ERROR][tfcmt] " + msg)
errMsgs = append(errMsgs, msg)
}
}
}
Expand All @@ -104,6 +113,7 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
Stderr: param.Stderr,
CombinedOutput: param.CombinedOutput,
ExitCode: param.ExitCode,
ErrorMessages: errMsgs,
})
body, err := template.Execute()
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const (
<pre><code>{{ .Body }}
</code></pre></details>
`
{{if .ErrorMessages}}
## :warning: Errors
{{range .ErrorMessages}}
* {{. -}}
{{- end}}{{end}}`

// DefaultPlanTemplate is a default template for terraform plan
DefaultPlanTemplate = defaultTemplate
Expand Down Expand Up @@ -76,6 +80,7 @@ type CommonTemplate struct {
Stderr string
CombinedOutput string
ExitCode int
ErrorMessages []string
}

// Template is a default template for terraform commands
Expand Down Expand Up @@ -176,6 +181,7 @@ func (t *Template) Execute() (string, error) {
"Stderr": t.Stderr,
"CombinedOutput": t.CombinedOutput,
"ExitCode": t.ExitCode,
"ErrorMessages": t.ErrorMessages,
}

resp, err := generateOutput("default", t.Template, data, t.UseRawOutput)
Expand Down

0 comments on commit adbbdeb

Please sign in to comment.