Skip to content
Draft
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 runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ atlantis server --hide-unchanged-plan-comments
ATLANTIS_HIDE_UNCHANGED_PLAN_COMMENTS=true
```

Remove no-changes plan comments from the pull request.
Remove no-changes plan comments and cancellation plan comments from the pull request.

This is useful when you have many projects and want to keep the pull request clean from useless comments.

Expand Down
27 changes: 21 additions & 6 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package events
import (
"bytes"
"embed"
"errors"
"fmt"
"strings"
"text/template"
Expand Down Expand Up @@ -124,12 +125,13 @@ type policyCheckResultsData struct {
}

type projectResultTmplData struct {
Workspace string
RepoRelDir string
ProjectName string
Rendered string
NoChanges bool
IsSuccessful bool
Workspace string
RepoRelDir string
ProjectName string
Rendered string
NoChanges bool
SuppressComment bool
IsSuccessful bool
}

// Initialize templates
Expand Down Expand Up @@ -226,6 +228,11 @@ func (m *MarkdownRenderer) renderProjectResults(ctx *command.Context, results []
ProjectName: result.ProjectName,
IsSuccessful: result.IsSuccessful(),
}
if common.Command == planCommandTitle && common.HideUnchangedPlanComments && isSuppressedPlanError(result.Error) {
resultData.SuppressComment = true
resultsTmplData = append(resultsTmplData, resultData)
continue
}
if result.PlanSuccess != nil {
result.PlanSuccess.TerraformOutput = strings.TrimSpace(result.PlanSuccess.TerraformOutput)
data := planSuccessData{
Expand Down Expand Up @@ -431,3 +438,11 @@ func (m *MarkdownRenderer) renderTemplateTrimSpace(tmpl *template.Template, data
}
return strings.TrimSpace(buf.String())
}

func isSuppressedPlanError(err error) bool {
if err == nil {
return false
}

return errors.Is(err, errOperationCancelled) || err.Error() == operationCancelledErrMsg
}
114 changes: 114 additions & 0 deletions server/events/markdown_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4377,6 +4377,77 @@ Ran Plan for 3 projects:

3 projects, 0 with changes, 3 with no changes, 0 failed

* :fast_forward: To **apply** all unapplied plans from this Pull Request, comment:
$$$shell
atlantis apply
$$$
* :put_litter_in_its_place: To **delete** all plans and locks from this Pull Request, comment:
$$$shell
atlantis unlock
$$$
`,
},
{
"hide cancelled plan errors when hide unchanged plans is enabled",
command.Plan,
"",
[]command.ProjectResult{
{
Workspace: "workspace",
RepoRelDir: "path",
ProjectCommandOutput: command.ProjectCommandOutput{
PlanSuccess: &models.PlanSuccess{
TerraformOutput: "terraform-output",
LockURL: "lock-url",
ApplyCmd: "atlantis apply -d path -w workspace",
RePlanCmd: "atlantis plan -d path -w workspace",
},
},
},
{
Workspace: "workspace",
RepoRelDir: "path2",
ProjectName: "projectname",
ProjectCommandOutput: command.ProjectCommandOutput{
Error: errors.New("operation cancelled via `atlantis cancel` command"),
},
},
{
Workspace: "workspace",
RepoRelDir: "path3",
ProjectName: "projectname2",
ProjectCommandOutput: command.ProjectCommandOutput{
Error: errors.New("operation cancelled via `atlantis cancel` command"),
},
},
},
models.Github,
`
Ran Plan for 3 projects:

1. dir: $path$ workspace: $workspace$
---

### 1. dir: $path$ workspace: $workspace$
$$$diff
terraform-output
$$$

* :arrow_forward: To **apply** this plan, comment:
$$$shell
atlantis apply -d path -w workspace
$$$
* :put_litter_in_its_place: To **delete** this plan and lock, click [here](lock-url)
* :repeat: To **plan** this project again, comment:
$$$shell
atlantis plan -d path -w workspace
$$$

---
### Plan Summary

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

* :fast_forward: To **apply** all unapplied plans from this Pull Request, comment:
$$$shell
atlantis apply
Expand Down Expand Up @@ -4440,3 +4511,46 @@ Ran Plan for 3 projects:
})
}
}

func TestRenderProjectResultsHideUnchangedPlansDoesNotHideNonCancellationErrors(t *testing.T) {
r := events.NewMarkdownRenderer(
false, // gitlabSupportsCommonMark
false, // disableApplyAll
false, // disableApply
false, // disableMarkdownFolding
false, // disableRepoLocking
false, // enableDiffMarkdownFormat
"", // markdownTemplateOverridesDir
"atlantis", // executableName
true, // hideUnchangedPlanComments
false, // quietPolicyChecks
)

logger := logging.NewNoopLogger(t).WithHistory()
ctx := &command.Context{
Log: logger,
Pull: models.PullRequest{
BaseRepo: models.Repo{
VCSHost: models.VCSHost{Type: models.Github},
},
},
}

res := command.Result{
ProjectResults: []command.ProjectResult{
{
Workspace: "workspace",
RepoRelDir: "path",
ProjectCommandOutput: command.ProjectCommandOutput{
Error: errors.New("some plan error"),
},
},
},
}

cmd := &events.CommentCommand{Name: command.Plan}
rendered := r.Render(ctx, res, cmd)

Assert(t, strings.Contains(rendered, "some plan error"), "expected non-cancellation plan errors to be rendered")
}

10 changes: 7 additions & 3 deletions server/events/project_command_pool_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package events

import (
"fmt"
"errors"
"sort"
"sync"

Expand All @@ -15,6 +15,10 @@ import (

type prjCmdRunnerFunc func(ctx command.ProjectContext) command.ProjectCommandOutput

const operationCancelledErrMsg = "operation cancelled via `atlantis cancel` command"

var errOperationCancelled = errors.New(operationCancelledErrMsg)

func RunOneProjectCmd(
runnerFunc prjCmdRunnerFunc,
cmd command.ProjectContext,
Expand Down Expand Up @@ -81,7 +85,7 @@ func runProjectCmdsParallel(
results = append(results, command.ProjectResult{
Command: pCmd.CommandName,
ProjectCommandOutput: command.ProjectCommandOutput{
Error: fmt.Errorf("operation cancelled via `atlantis cancel` command"),
Error: errOperationCancelled,
},
RepoRelDir: pCmd.RepoRelDir,
Workspace: pCmd.Workspace,
Expand Down Expand Up @@ -227,7 +231,7 @@ func createCancelledResults(remainingGroups [][]command.ProjectContext) []comman
cancelledResults = append(cancelledResults, command.ProjectResult{
Command: cmd.CommandName,
ProjectCommandOutput: command.ProjectCommandOutput{
Error: fmt.Errorf("operation cancelled via `atlantis cancel` command"),
Error: errOperationCancelled,
},
RepoRelDir: cmd.RepoRelDir,
Workspace: cmd.Workspace,
Expand Down
1 change: 1 addition & 0 deletions server/events/templates/multi_project_header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Ran {{.Command}} for {{ len .Results }} projects:

{{ range $result := .Results -}}
{{ if $result.SuppressComment }}{{continue}}{{end -}}
1. {{ if $result.ProjectName }}project: `{{ $result.ProjectName }}` {{ end }}dir: `{{ $result.RepoRelDir }}` workspace: `{{ $result.Workspace }}`
{{ end -}}
{{ if (gt (len .Results) 0) -}}
Expand Down
1 change: 1 addition & 0 deletions server/events/templates/multi_project_plan.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{ $disableApplyAll := .DisableApplyAll -}}
{{ $hideUnchangedPlans := .HideUnchangedPlanComments -}}
{{ range $i, $result := .Results -}}
{{ if $result.SuppressComment }}{{continue}}{{end -}}
{{ if (and $hideUnchangedPlans $result.NoChanges) }}{{continue}}{{end -}}
### {{ add $i 1 }}. {{ if $result.ProjectName }}project: `{{ $result.ProjectName }}` {{ end }}dir: `{{ $result.RepoRelDir }}` workspace: `{{ $result.Workspace }}`
{{ $result.Rendered }}
Expand Down
Loading