Skip to content

Commit

Permalink
feat: add templates
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jul 12, 2021
1 parent 0938b8e commit 9144e77
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 40 deletions.
30 changes: 16 additions & 14 deletions pkg/notifier/github/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ func (g *NotifyService) Notify(ctx context.Context, param notifier.ParamExec) (i
}

template.SetValue(terraform.CommonTemplate{
Result: result.Result,
Link: cfg.CI,
UseRawOutput: cfg.UseRawOutput,
Vars: cfg.Vars,
Templates: cfg.Templates,
Stdout: param.Stdout,
Stderr: param.Stderr,
CombinedOutput: param.CombinedOutput,
ExitCode: param.ExitCode,
ErrorMessages: errMsgs,
CreatedResources: result.CreatedResources,
UpdatedResources: result.UpdatedResources,
DeletedResources: result.DeletedResources,
ReplacedResources: result.ReplacedResources,
Result: result.Result,
ChangedResult: result.ChangeResult,
ChangeOutsideTerraform: result.OutsideTerraform,
Link: cfg.CI,
UseRawOutput: cfg.UseRawOutput,
Vars: cfg.Vars,
Templates: cfg.Templates,
Stdout: param.Stdout,
Stderr: param.Stderr,
CombinedOutput: param.CombinedOutput,
ExitCode: param.ExitCode,
ErrorMessages: errMsgs,
CreatedResources: result.CreatedResources,
UpdatedResources: result.UpdatedResources,
DeletedResources: result.DeletedResources,
ReplacedResources: result.ReplacedResources,
})
body, err := template.Execute()
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Parser interface {
// ParseResult represents the result of parsed terraform execution
type ParseResult struct {
Result string
OutsideTerraform string
ChangeResult string
HasAddOrUpdateOnly bool
HasDestroy bool
HasNoChanges bool
Expand Down Expand Up @@ -112,7 +114,27 @@ func (p *PlanParser) Parse(body string) ParseResult { //nolint:cyclop
firstMatchLineIndex := -1
var result, firstMatchLine string
var createdResources, updatedResources, deletedResources, replacedResources []string
startOutsideTerraform := -1
endOutsideTerraform := -1
startChangeOutput := -1
endChangeOutput := -1
warnings := []string{}
for i, line := range lines {
if line == "Note: Objects have changed outside of Terraform" { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L403
startOutsideTerraform = i + 1
}
if startOutsideTerraform != -1 && endOutsideTerraform == -1 && strings.HasPrefix(line, "Unless you have made equivalent changes to your configuration") { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L110
endOutsideTerraform = i + 1
}
if line == "Terraform will perform the following actions:" { // https://github.com/hashicorp/terraform/blob/332045a4e4b1d256c45f98aac74e31102ace7af7/internal/command/views/plan.go#L252
startChangeOutput = i + 1
}
if startChangeOutput != -1 && endChangeOutput == -1 && strings.HasPrefix(line, "Plan: ") { // https://github.com/hashicorp/terraform/blob/dfc12a6a9e1cff323829026d51873c1b80200757/internal/command/views/plan.go#L306
endChangeOutput = i + 1
}
if strings.HasPrefix(line, "Warning:") {
warnings = append(warnings, line)
}
if firstMatchLineIndex == -1 {
if p.Pass.MatchString(line) || p.Fail.MatchString(line) {
firstMatchLineIndex = i
Expand Down Expand Up @@ -142,8 +164,20 @@ func (p *PlanParser) Parse(body string) ParseResult { //nolint:cyclop
hasNoChanges := p.HasNoChanges.MatchString(firstMatchLine)
HasAddOrUpdateOnly := !hasNoChanges && !hasDestroy && !hasPlanError

outsideTerraform := ""
if startOutsideTerraform != -1 {
outsideTerraform = strings.Join(lines[startOutsideTerraform:endOutsideTerraform], "\n")
}

changeResult := ""
if startChangeOutput != -1 {
changeResult = strings.Join(lines[startChangeOutput:endChangeOutput], "\n")
}

return ParseResult{
Result: result,
ChangeResult: changeResult,
OutsideTerraform: outsideTerraform,
HasAddOrUpdateOnly: HasAddOrUpdateOnly,
HasDestroy: hasDestroy,
HasNoChanges: hasNoChanges,
Expand Down
56 changes: 30 additions & 26 deletions pkg/terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,22 @@ It failed to parse the result.

// CommonTemplate represents template entities
type CommonTemplate struct {
Result string
Link string
UseRawOutput bool
Vars map[string]string
Templates map[string]string
Stdout string
Stderr string
CombinedOutput string
ExitCode int
ErrorMessages []string
CreatedResources []string
UpdatedResources []string
DeletedResources []string
ReplacedResources []string
Result string
ChangedResult string
ChangeOutsideTerraform string
Link string
UseRawOutput bool
Vars map[string]string
Templates map[string]string
Stdout string
Stderr string
CombinedOutput string
ExitCode int
ErrorMessages []string
CreatedResources []string
UpdatedResources []string
DeletedResources []string
ReplacedResources []string
}

// Template is a default template for terraform commands
Expand Down Expand Up @@ -202,18 +204,20 @@ func generateOutput(kind, template string, data map[string]interface{}, useRawOu
// Execute binds the execution result of terraform command into template
func (t *Template) Execute() (string, error) {
data := map[string]interface{}{
"Result": t.Result,
"Link": t.Link,
"Vars": t.Vars,
"Stdout": t.Stdout,
"Stderr": t.Stderr,
"CombinedOutput": t.CombinedOutput,
"ExitCode": t.ExitCode,
"ErrorMessages": t.ErrorMessages,
"CreatedResources": t.CreatedResources,
"UpdatedResources": t.UpdatedResources,
"DeletedResources": t.DeletedResources,
"ReplacedResources": t.ReplacedResources,
"Result": t.Result,
"ChangedResult": t.ChangedResult,
"ChangeOutsideTerraform": t.ChangeOutsideTerraform,
"Link": t.Link,
"Vars": t.Vars,
"Stdout": t.Stdout,
"Stderr": t.Stderr,
"CombinedOutput": t.CombinedOutput,
"ExitCode": t.ExitCode,
"ErrorMessages": t.ErrorMessages,
"CreatedResources": t.CreatedResources,
"UpdatedResources": t.UpdatedResources,
"DeletedResources": t.DeletedResources,
"ReplacedResources": t.ReplacedResources,
}

templates := map[string]string{
Expand Down

0 comments on commit 9144e77

Please sign in to comment.