Skip to content

Commit

Permalink
fix: PR status summary should remove Note: Objects have changed outsi…
Browse files Browse the repository at this point in the history
…de of Terraform (#3010)

* extract PlanSuccess.DiffSummary

* commit status updater use DiffSummary instead Summary to show only diff result
  • Loading branch information
krrrr38 authored Jan 19, 2023
1 parent ec54d83 commit 9e3d887
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
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

0 comments on commit 9e3d887

Please sign in to comment.