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(tf,plan): extract action part from terraform plan output. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Placeholder | Usage
---|---
`{{ .Title }}` | Like `## Plan result`
`{{ .Message }}` | A string that can be set from CLI with `--message` option
`{{ .Action }}` | Using in terraform plan, and matched leading message by parsing like `Terraform will perform the following actions:`
`{{ .Result }}` | Matched result by parsing like `Plan: 1 to add` or `No changes`
`{{ .Body }}` | The entire of Terraform execution result
`{{ .Link }}` | The link of the build page on CI
Expand Down
20 changes: 16 additions & 4 deletions terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Parser interface {
type ParseResult struct {
Result string
HasDestroy bool
Action string
ExitCode int
Error error
}
Expand All @@ -32,6 +33,7 @@ type FmtParser struct {
// PlanParser is a parser for terraform plan
type PlanParser struct {
Pass *regexp.Regexp
Action *regexp.Regexp
Fail *regexp.Regexp
HasDestroy *regexp.Regexp
}
Expand All @@ -57,8 +59,9 @@ func NewFmtParser() *FmtParser {
// NewPlanParser is PlanParser initialized with its Regexp
func NewPlanParser() *PlanParser {
return &PlanParser{
Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`),
Action: regexp.MustCompile(`(?m)^(An execution plan has been generated)`),
Fail: regexp.MustCompile(`(?m)^(Error: )`),
// "0 to destroy" should be treated as "no destroy"
HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`),
}
Expand Down Expand Up @@ -107,16 +110,24 @@ func (p *PlanParser) Parse(body string) ParseResult {
}
}
lines := strings.Split(body, "\n")
var i int
var result, line string
var i, actionStartIdx int
var result, action, line string
for i, line = range lines {
if p.Action.MatchString(line) {
// action starts with the line: An execution plan...
actionStartIdx = i
}
if p.Pass.MatchString(line) || p.Fail.MatchString(line) {
break
}
}
switch {
case p.Pass.MatchString(line):
result = lines[i]
if actionStartIdx != 0 {
// action ends with the line above summary
action = strings.Join(trimLastNewline(lines[actionStartIdx:i]), "\n")
}
case p.Fail.MatchString(line):
result = strings.Join(trimLastNewline(lines[i:]), "\n")
}
Expand All @@ -126,6 +137,7 @@ func (p *PlanParser) Parse(body string) ParseResult {
return ParseResult{
Result: result,
HasDestroy: hasDestroy,
Action: action,
ExitCode: exitCode,
Error: nil,
}
Expand Down
34 changes: 28 additions & 6 deletions terraform/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ versions.tf
--- old/versions.tf
+++ new/versions.tf
@@ -1,4 +1,4 @@

terraform {
- required_version = ">= 0.12"
+ required_version = ">= 0.12"
Expand Down Expand Up @@ -309,8 +309,22 @@ func TestPlanParserParse(t *testing.T) {
result: ParseResult{
Result: "Plan: 1 to add, 0 to change, 0 to destroy.",
HasDestroy: false,
ExitCode: 0,
Error: nil,
Action: `An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ google_compute_global_address.my_another_project
id: <computed>
address: <computed>
ip_version: "IPV4"
name: "my-another-project"
project: "my-project"
self_link: <computed>
`,
ExitCode: 0,
Error: nil,
},
},
{
Expand Down Expand Up @@ -353,15 +367,23 @@ func TestPlanParserParse(t *testing.T) {
result: ParseResult{
Result: "Plan: 0 to add, 0 to change, 1 to destroy.",
HasDestroy: true,
ExitCode: 0,
Error: nil,
Action: `An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

- google_project_iam_member.team_platform[2]
`,
ExitCode: 0,
Error: nil,
},
},
}
for _, testCase := range testCases {
result := NewPlanParser().Parse(testCase.body)
if !reflect.DeepEqual(result, testCase.result) {
t.Errorf("got %v but want %v", result, testCase.result)
t.Errorf("<%s>: got %v but want %v", testCase.name, result, testCase.result)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions terraform/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Template interface {
type CommonTemplate struct {
Title string
Message string
Action string
Result string
Body string
Link string
Expand Down Expand Up @@ -261,6 +262,7 @@ func (t *PlanTemplate) Execute() (string, error) {
"Title": t.Title,
"Message": t.Message,
"Result": t.Result,
"Action": t.Action,
"Body": t.Body,
"Link": t.Link,
}
Expand All @@ -279,6 +281,7 @@ func (t *DestroyWarningTemplate) Execute() (string, error) {
"Title": t.Title,
"Message": t.Message,
"Result": t.Result,
"Action": t.Action,
"Body": t.Body,
"Link": t.Link,
}
Expand Down
11 changes: 11 additions & 0 deletions terraform/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,17 @@ message
},
resp: `a-b-c-d`,
},
{
template: `{{ .Title }}-{{ .Message }}-{{ .Action }}-{{ .Result }}-{{ .Body }}`,
value: CommonTemplate{
Title: "a",
Message: "b",
Action: "action",
Result: "c",
Body: "d",
},
resp: `a-b-action-c-d`,
},
}
for _, testCase := range testCases {
template := NewPlanTemplate(testCase.template)
Expand Down