Skip to content

Commit

Permalink
feat: Add Summaries to Plan And Apply PR Comments (#3889)
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Guardian authored Jan 2, 2024
1 parent 3180267 commit 310fc8a
Show file tree
Hide file tree
Showing 31 changed files with 294 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
* `atlantis plan -d dir2`

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 1 with changes, 1 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ workspace = "default"
### 2. dir: `dir2` workspace: `default`
**Apply Failed**: All policies must pass for project before running apply.

---
---
### Apply Summary

2 projects, 1 successful, 1 failed, 0 errored
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ null_resource.staging[0]: Creation complete after *s [id=*******************]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
```

---
---
### Apply Summary

2 projects, 2 successful, 0 failed, 0 errored
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
* `atlantis plan -d infrastructure/production`

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ postapply

</details>

---
---
### Apply Summary

2 projects, 2 successful, 0 failed, 0 errored
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ workspace = "new_workspace"

</details>

---
---
### Apply Summary

2 projects, 2 successful, 0 failed, 0 errored
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Plan: 1 to add, 0 to change, 0 to destroy.
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ and found no differences, so no changes are needed.
* `atlantis plan -d dir2`

---
### Plan Summary

2 projects, 0 with changes, 2 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Changes to Outputs:
Plan: 1 to add, 0 to change, 0 to destroy.

---
### Plan Summary

2 projects, 2 with changes, 0 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this pull request, comment:
* `atlantis apply`
* :put_litter_in_its_place: To **delete** all plans and locks for the PR, comment:
Expand Down
10 changes: 5 additions & 5 deletions server/events/apply_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestApplyCommandRunner_ExecutionOrder(t *testing.T) {
},
ExpComment: "Ran Apply for 2 projects:\n\n" +
"1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n\n### 1. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### " +
"2. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---",
"2. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---\n### Apply Summary\n\n2 projects, 1 successful, 0 failed, 1 errored",
},
{
Description: "When first apply fails, the second not will run",
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestApplyCommandRunner_ExecutionOrder(t *testing.T) {
},
ExpComment: "Ran Apply for 2 projects:\n\n" +
"1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n\n### 1. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### " +
"2. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---",
"2. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---\n### Apply Summary\n\n2 projects, 1 successful, 0 failed, 1 errored",
},
{
Description: "When one out of two fails, the following two will not run",
Expand Down Expand Up @@ -398,7 +398,7 @@ func TestApplyCommandRunner_ExecutionOrder(t *testing.T) {
"1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n\n### 1. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### " +
"2. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### " +
"3. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---\n### " +
"4. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---",
"4. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### Apply Summary\n\n4 projects, 3 successful, 0 failed, 1 errored",
},
{
Description: "Don't block when parallel is not set",
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestApplyCommandRunner_ExecutionOrder(t *testing.T) {
},
ExpComment: "Ran Apply for 2 projects:\n\n" +
"1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n\n### 1. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---\n### " +
"2. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---",
"2. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### Apply Summary\n\n2 projects, 1 successful, 0 failed, 1 errored",
},
{
Description: "Don't block when abortOnExcecutionOrderFail is not set",
Expand Down Expand Up @@ -460,7 +460,7 @@ func TestApplyCommandRunner_ExecutionOrder(t *testing.T) {
},
ExpComment: "Ran Apply for 2 projects:\n\n" +
"1. dir: `` workspace: ``\n1. dir: `` workspace: ``\n\n### 1. dir: `` workspace: ``\n**Apply Error**\n```\nshabang\n```\n\n---\n### " +
"2. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---",
"2. dir: `` workspace: ``\n```diff\nGreat success!\n```\n\n---\n### Apply Summary\n\n2 projects, 1 successful, 0 failed, 1 errored",
},
}

Expand Down
43 changes: 42 additions & 1 deletion server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ type resultData struct {
commonData
}

type planResultData struct {
Results []projectResultTmplData
commonData
NumPlansWithChanges int
NumPlansWithNoChanges int
NumPlanFailures int
}

type applyResultData struct {
Results []projectResultTmplData
commonData
NumApplySuccesses int
NumApplyFailures int
NumApplyErrors int
}

type planSuccessData struct {
models.PlanSuccess
PlanSummary string
Expand Down Expand Up @@ -187,6 +203,11 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
numPolicyCheckSuccesses := 0
numPolicyApprovalSuccesses := 0
numVersionSuccesses := 0
numPlansWithChanges := 0
numPlansWithNoChanges := 0
numApplySuccesses := 0
numApplyFailures := 0
numApplyErrors := 0

templates := m.markdownTemplates

Expand All @@ -213,6 +234,11 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
resultData.Rendered = m.renderTemplateTrimSpace(templates.Lookup("planSuccessUnwrapped"), data)
}
resultData.NoChanges = result.PlanSuccess.NoChanges()
if result.PlanSuccess.NoChanges() {
numPlansWithNoChanges++
} else {
numPlansWithChanges++
}
numPlanSuccesses++
} else if result.PolicyCheckResults != nil && common.Command == policyCheckCommandTitle {
policyCheckResults := policyCheckResultsData{
Expand Down Expand Up @@ -255,6 +281,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
} else {
resultData.Rendered = m.renderTemplateTrimSpace(templates.Lookup("applyUnwrappedSuccess"), struct{ Output string }{output})
}
numApplySuccesses++
} else if result.VersionSuccess != "" {
output := strings.TrimSpace(result.VersionSuccess)
if m.shouldUseWrappedTmpl(vcsHost, output) {
Expand Down Expand Up @@ -289,8 +316,14 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
tmpl = templates.Lookup("wrappedErr")
}
resultData.Rendered = m.renderTemplateTrimSpace(tmpl, errData{result.Error.Error(), resultData.Rendered, common})
if common.Command == applyCommandTitle {
numApplyErrors++
}
} else if result.Failure != "" {
resultData.Rendered = m.renderTemplateTrimSpace(templates.Lookup("failure"), failureData{result.Failure, resultData.Rendered, common})
if common.Command == applyCommandTitle {
numApplyFailures++
}
}
resultsTmplData = append(resultsTmplData, resultData)
}
Expand Down Expand Up @@ -324,7 +357,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
tmpl = templates.Lookup("multiProjectPlan")
case common.Command == policyCheckCommandTitle:
if numPolicyCheckSuccesses == len(results) {
tmpl = templates.Lookup("multiProjectPlan")
tmpl = templates.Lookup("multiProjectPolicy")
} else {
tmpl = templates.Lookup("multiProjectPolicyUnsuccessful")
}
Expand All @@ -350,6 +383,14 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult,
default:
return fmt.Sprintf("no template matched–this is a bug: command=%s", common.Command)
}

switch {
case common.Command == planCommandTitle:
numPlanFailures := len(results) - numPlanSuccesses
return m.renderTemplateTrimSpace(tmpl, planResultData{resultsTmplData, common, numPlansWithChanges, numPlansWithNoChanges, numPlanFailures})
case common.Command == applyCommandTitle:
return m.renderTemplateTrimSpace(tmpl, applyResultData{resultsTmplData, common, numApplySuccesses, numApplyFailures, numApplyErrors})
}
return m.renderTemplateTrimSpace(tmpl, resultData{resultsTmplData, common})
}

Expand Down
Loading

0 comments on commit 310fc8a

Please sign in to comment.