Skip to content

Commit

Permalink
Hide successful policy check results when --quiet-policy-checks is se…
Browse files Browse the repository at this point in the history
…t with multiple projects

Signed-off-by: Joe Cai <[email protected]>
  • Loading branch information
joec4i committed Dec 15, 2024
1 parent c46c0e0 commit b22a3aa
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 17 deletions.
25 changes: 22 additions & 3 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,25 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {
{"exp-output-merge.txt"},
},
},
{
Description: "1 failing policy and 1 passing policy with --quiet-policy-checks",
RepoDir: "policy-checks-multi-projects",
ModifiedFiles: []string{"dir1/main.tf,", "dir2/main.tf"},
PolicyCheck: true,
ExpAutoplan: true,
ExpPolicyChecks: true,
ExpQuietPolicyChecks: true,
ExpQuietPolicyCheckFailure: true,
Comments: []string{
"atlantis apply",
},
ExpReplies: [][]string{
{"exp-output-autoplan.txt"},
{"exp-output-auto-policy-check-quiet.txt"},
{"exp-output-apply.txt"},
{"exp-output-merge.txt"},
},
},
{
Description: "failing policy without policies passing using extra args",
RepoDir: "policy-checks-extra-args",
Expand Down Expand Up @@ -1182,7 +1201,7 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {
userConfig.EnablePolicyChecksFlag = c.PolicyCheck
userConfig.QuietPolicyChecks = c.ExpQuietPolicyChecks

ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t, c.RepoDir, setupOption{})
ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t, c.RepoDir, setupOption{userConfig: userConfig})

// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA := initializeRepo(t, c.RepoDir)
Expand Down Expand Up @@ -1273,13 +1292,13 @@ type setupOption struct {
allowCommands []command.Name
disableAutoplan bool
disablePreWorkflowHooks bool
userConfig server.UserConfig
}

func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers.VCSEventsController, *vcsmocks.MockClient, *mocks.MockGithubPullGetter, *events.FileWorkspace) {
allowForkPRs := false
discardApprovalOnPlan := true
dataDir, binDir, cacheDir := mkSubDirs(t)

// Mocks.
e2eVCSClient := vcsmocks.NewMockClient()
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient}
Expand Down Expand Up @@ -1487,7 +1506,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
pullUpdater := &events.PullUpdater{
HidePrevPlanComments: false,
VCSClient: e2eVCSClient,
MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis", false),
MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis", false, opt.userConfig.QuietPolicyChecks),
}

autoMerger := &events.AutoMerger{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Ran Policy Check for 2 projects:

1. dir: `dir1` workspace: `default`
1. dir: `dir2` workspace: `default`
---

### 2. dir: `dir2` workspace: `default`
**Policy Check Failed**: Some policy sets did not pass.
#### Policy Set: `test_policy`
```diff
FAIL - <redacted plan file> - main - WARNING: Forbidden Resource creation is prohibited.

1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions

```


#### Policy Approval Status:
```
policy set: test_policy: requires: 1 approval(s), have: 0.
```
* :heavy_check_mark: To **approve** this project, comment:
```shell
atlantis approve_policies -d dir2
```
* :put_litter_in_its_place: To **delete** this plan and lock, click [here](lock-url)
* :repeat: To re-run policies **plan** this project again by commenting:
```shell
atlantis plan -d dir2
```

---
* :heavy_check_mark: To **approve** all unapplied plans from this Pull Request, comment:
```shell
atlantis approve_policies
```
* :put_litter_in_its_place: To **delete** all plans and locks from this Pull Request, comment:
```shell
atlantis unlock
```
* :repeat: To re-run policies **plan** this project again by commenting:
```shell
atlantis plan
```
2 changes: 1 addition & 1 deletion server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock
pullUpdater = &events.PullUpdater{
HidePrevPlanComments: false,
VCSClient: vcsClient,
MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis", false),
MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis", false, false),
}

autoMerger = &events.AutoMerger{
Expand Down
23 changes: 15 additions & 8 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type MarkdownRenderer struct {
markdownTemplates *template.Template
executableName string
hideUnchangedPlanComments bool
quietPolicyChecks bool
}

// commonData is data that all responses have.
Expand All @@ -72,6 +73,7 @@ type commonData struct {
EnableDiffMarkdownFormat bool
ExecutableName string
HideUnchangedPlanComments bool
QuietPolicyChecks bool
VcsRequestType string
}

Expand Down Expand Up @@ -131,11 +133,12 @@ type policyCheckResultsData struct {
}

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

// Initialize templates
Expand All @@ -149,6 +152,7 @@ func NewMarkdownRenderer(
markdownTemplateOverridesDir string,
executableName string,
hideUnchangedPlanComments bool,
quietPolicyChecks bool,
) *MarkdownRenderer {
var templates *template.Template
templates, _ = template.New("").Funcs(sprig.TxtFuncMap()).ParseFS(templatesFS, "templates/*.tmpl")
Expand All @@ -166,6 +170,7 @@ func NewMarkdownRenderer(
markdownTemplates: templates,
executableName: executableName,
hideUnchangedPlanComments: hideUnchangedPlanComments,
quietPolicyChecks: quietPolicyChecks,
}
}

Expand All @@ -192,6 +197,7 @@ func (m *MarkdownRenderer) Render(ctx *command.Context, res command.Result, cmd
EnableDiffMarkdownFormat: m.enableDiffMarkdownFormat,
ExecutableName: m.executableName,
HideUnchangedPlanComments: m.hideUnchangedPlanComments,
QuietPolicyChecks: m.quietPolicyChecks,
VcsRequestType: vcsRequestType,
}

Expand Down Expand Up @@ -224,9 +230,10 @@ func (m *MarkdownRenderer) renderProjectResults(ctx *command.Context, results []

for _, result := range results {
resultData := projectResultTmplData{
Workspace: result.Workspace,
RepoRelDir: result.RepoRelDir,
ProjectName: result.ProjectName,
Workspace: result.Workspace,
RepoRelDir: result.RepoRelDir,
ProjectName: result.ProjectName,
IsSuccessful: result.IsSuccessful(),
}
if result.PlanSuccess != nil {
result.PlanSuccess.TerraformOutput = strings.TrimSpace(result.PlanSuccess.TerraformOutput)
Expand Down
Loading

0 comments on commit b22a3aa

Please sign in to comment.