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

Add plan summary to unfolded part of comment #1518

Merged
merged 4 commits into from
Apr 22, 2021
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
6 changes: 4 additions & 2 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type resultData struct {

type planSuccessData struct {
models.PlanSuccess
PlanSummary string
PlanWasDeleted bool
DisableApply bool
DisableRepoLocking bool
Expand Down Expand Up @@ -147,7 +148,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []models.ProjectResult,
})
} else if result.PlanSuccess != nil {
if m.shouldUseWrappedTmpl(vcsHost, result.PlanSuccess.TerraformOutput) {
resultData.Rendered = m.renderTemplate(planSuccessWrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
resultData.Rendered = m.renderTemplate(planSuccessWrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanSummary: result.PlanSuccess.Summary(), PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
} else {
resultData.Rendered = m.renderTemplate(planSuccessUnwrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
}
Expand Down Expand Up @@ -280,7 +281,8 @@ var planSuccessWrappedTmpl = template.Must(template.New("").Parse(
"{{.TerraformOutput}}\n" +
"```\n\n" +
planNextSteps + "\n" +
"</details>" +
"</details>" + "\n" +
"{{.PlanSummary}}" +
"{{ if .HasDiverged }}\n\n:warning: The branch we're merging into is ahead, it is recommended to pull new commits first.{{end}}"))

var policyCheckSuccessUnwrappedTmpl = template.Must(template.New("").Parse(
Expand Down
9 changes: 6 additions & 3 deletions server/events/markdown_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ func TestRenderProjectResults_WrapSingleProject(t *testing.T) {
},
{
VCSHost: models.Github,
Output: strings.Repeat("line\n", 13),
Output: strings.Repeat("line\n", 13) + fmt.Sprintf("No changes. Infrastructure is up-to-date."),
ShouldWrap: true,
},
{
Expand All @@ -1234,7 +1234,7 @@ func TestRenderProjectResults_WrapSingleProject(t *testing.T) {
{
VCSHost: models.Gitlab,
GitlabCommonMarkSupport: true,
Output: strings.Repeat("line\n", 13),
Output: strings.Repeat("line\n", 13) + fmt.Sprintf("No changes. Infrastructure is up-to-date."),
ShouldWrap: true,
},
{
Expand Down Expand Up @@ -1309,6 +1309,7 @@ $$$
* :repeat: To **plan** this project again, comment:
* $replancmd$
</details>
No changes. Infrastructure is up-to-date.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down Expand Up @@ -1414,7 +1415,7 @@ $$$

func TestRenderProjectResults_MultiProjectPlanWrapped(t *testing.T) {
mr := events.MarkdownRenderer{}
tfOut := strings.Repeat("line\n", 13)
tfOut := strings.Repeat("line\n", 13) + fmt.Sprintf("Plan: 1 to add, 0 to change, 0 to destroy.")
rendered := mr.Render(events.CommandResult{
ProjectResults: []models.ProjectResult{
{
Expand Down Expand Up @@ -1457,6 +1458,7 @@ $$$
* :repeat: To **plan** this project again, comment:
* $staging-replan-cmd$
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: $.$ workspace: $production$
Expand All @@ -1472,6 +1474,7 @@ $$$
* :repeat: To **plan** this project again, comment:
* $production-replan-cmd$
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
11 changes: 11 additions & 0 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/url"
paths "path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -491,6 +492,16 @@ type PlanSuccess struct {
HasDiverged bool
}

// Summary extracts one line summary of plan changes from TerraformOutput.
func (p *PlanSuccess) Summary() string {
r := regexp.MustCompile(`Plan: \d+ to add, \d+ to change, \d+ to destroy.`)
if match := r.FindString(p.TerraformOutput); match != "" {
return match
}
r = regexp.MustCompile(`No changes. Infrastructure is up-to-date.`)
return r.FindString(p.TerraformOutput)
}

// PolicyCheckSuccess is the result of a successful policy check run.
type PolicyCheckSuccess struct {
// PolicyCheckOutput is the output from policy check binary(conftest|opa)
Expand Down
52 changes: 52 additions & 0 deletions server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,58 @@ func TestProjectResult_PlanStatus(t *testing.T) {
}
}

func TestPlanSuccess_Summary(t *testing.T) {
cases := []struct {
p models.ProjectResult
expResult string
}{
{
p: models.ProjectResult{
PlanSuccess: &models.PlanSuccess{
TerraformOutput: `
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:

- null_resource.hi[1]


Plan: 0 to add, 0 to change, 1 to destroy.`,
},
},
expResult: "Plan: 0 to add, 0 to change, 1 to destroy.",
},
{
p: models.ProjectResult{
PlanSuccess: &models.PlanSuccess{
TerraformOutput: `
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

No changes. Infrastructure is up-to-date.`,
},
},
expResult: "No changes. Infrastructure is up-to-date.",
},
{
p: models.ProjectResult{
PlanSuccess: &models.PlanSuccess{
TerraformOutput: `No match, expect empty`,
},
},
expResult: "",
},
}

for _, c := range cases {
t.Run(c.expResult, func(t *testing.T) {
Equals(t, c.expResult, c.p.PlanSuccess.Summary())
})
}
}

func TestPullStatus_StatusCount(t *testing.T) {
ps := models.PullStatus{
Projects: []models.ProjectStatus{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Plan: 1 to add, 0 to change, 0 to destroy.
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d dir1`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: `dir2` workspace: `default`
Expand Down Expand Up @@ -57,6 +58,7 @@ Plan: 1 to add, 0 to change, 0 to destroy.
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d dir2`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: `production` workspace: `default`
Expand Down Expand Up @@ -63,6 +64,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d production`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d production`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d dir1`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: `dir2` workspace: `default`
Expand Down Expand Up @@ -63,6 +64,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d dir2`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ postplan custom
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: `.` workspace: `staging`
Expand Down Expand Up @@ -69,6 +70,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -w staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ postplan
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. dir: `.` workspace: `staging`
Expand Down Expand Up @@ -69,6 +70,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -w staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -w new_workspace -- -var var=new_workspace`
</details>
Plan: 3 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d . -- -var var=overridden`
</details>
Plan: 3 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d . -- -var var=default_workspace`
</details>
Plan: 3 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -d .`
</details>
Plan: 3 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -p default`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -p staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ workspace=default
* :repeat: To **plan** this project again, comment:
* `atlantis plan -p default`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
### 2. project: `staging` dir: `.` workspace: `default`
Expand Down Expand Up @@ -67,6 +68,7 @@ Changes to Outputs:
* :repeat: To **plan** this project again, comment:
* `atlantis plan -p staging`
</details>
Plan: 1 to add, 0 to change, 0 to destroy.

---
* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
Expand Down