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

fix: PR status summary should remove Note: Objects have changed outside of Terraform #3010

Merged
merged 2 commits into from
Jan 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/events/commit_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (d *DefaultCommitStatusUpdater) UpdateProject(ctx command.ProjectContext, c
descripWords = genProjectStatusDescription(cmdName.String(), "failed.")
case models.SuccessCommitStatus:
if result != nil && result.PlanSuccess != nil {
descripWords = result.PlanSuccess.Summary()
descripWords = result.PlanSuccess.DiffSummary()
} else {
descripWords = genProjectStatusDescription(cmdName.String(), "succeeded.")
}
Expand Down
2 changes: 1 addition & 1 deletion server/events/commit_status_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestDefaultCommitStatusUpdater_UpdateProject(t *testing.T) {
cmd: command.Plan,
result: &command.ProjectResult{
PlanSuccess: &models.PlanSuccess{
TerraformOutput: "aaa\nPlan: 1 to add, 2 to change, 3 to destroy.\nbbb",
TerraformOutput: "aaa\nNote: Objects have changed outside of Terraform\nbbb\nPlan: 1 to add, 2 to change, 3 to destroy.\nbbb",
},
},
expDescrip: "Plan: 1 to add, 2 to change, 3 to destroy.",
Expand Down
11 changes: 8 additions & 3 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,21 @@ var (
reNoChanges = regexp.MustCompile(`No changes. (Infrastructure is up-to-date|Your infrastructure matches the configuration).`)
)

// Summary extracts one line summary of plan changes from TerraformOutput.
// Summary extracts summaries of plan changes from TerraformOutput.
func (p *PlanSuccess) Summary() string {
note := ""
if match := reChangesOutside.FindString(p.TerraformOutput); match != "" {
note = "\n**" + match + "**\n"
}
return note + p.DiffSummary()
}

// DiffSummary extracts one line summary of plan changes from TerraformOutput.
func (p *PlanSuccess) DiffSummary() string {
if match := rePlanChanges.FindString(p.TerraformOutput); match != "" {
return note + match
return match
}
return note + reNoChanges.FindString(p.TerraformOutput)
return reNoChanges.FindString(p.TerraformOutput)
}

// Diff Markdown regexes
Expand Down
64 changes: 64 additions & 0 deletions server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,70 @@ func TestAzureDevopsSplitRepoFullName(t *testing.T) {
}
}

func TestPlanSuccess_Summary(t *testing.T) {
cases := []struct {
input string
exp string
}{
{
"Note: Objects have changed outside of Terraform\ndummy\nPlan: 0 to add, 1 to change, 2 to destroy.",
"\n**Note: Objects have changed outside of Terraform**\nPlan: 0 to add, 1 to change, 2 to destroy.",
},
{
"dummy\nPlan: 100 to add, 111 to change, 222 to destroy.",
"Plan: 100 to add, 111 to change, 222 to destroy.",
},
{
"Note: Objects have changed outside of Terraform\ndummy\nNo changes. Infrastructure is up-to-date.",
"\n**Note: Objects have changed outside of Terraform**\nNo changes. Infrastructure is up-to-date.",
},
{
"dummy\nNo changes. Your infrastructure matches the configuration.",
"No changes. Your infrastructure matches the configuration.",
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("summary %d", i), func(t *testing.T) {
pcs := models.PlanSuccess{
TerraformOutput: c.input,
}
Equals(t, c.exp, pcs.Summary())
})
}
}

func TestPlanSuccess_DiffSummary(t *testing.T) {
cases := []struct {
input string
exp string
}{
{
"Note: Objects have changed outside of Terraform\ndummy\nPlan: 0 to add, 1 to change, 2 to destroy.",
"Plan: 0 to add, 1 to change, 2 to destroy.",
},
{
"dummy\nPlan: 100 to add, 111 to change, 222 to destroy.",
"Plan: 100 to add, 111 to change, 222 to destroy.",
},
{
"Note: Objects have changed outside of Terraform\ndummy\nNo changes. Infrastructure is up-to-date.",
"No changes. Infrastructure is up-to-date.",
},
{
"dummy\nNo changes. Your infrastructure matches the configuration.",
"No changes. Your infrastructure matches the configuration.",
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("summary %d", i), func(t *testing.T) {
pcs := models.PlanSuccess{
TerraformOutput: c.input,
}
Equals(t, c.exp, pcs.DiffSummary())
})
}
}

func TestPolicyCheckSuccess_Summary(t *testing.T) {
cases := []string{
"20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
Expand Down