Skip to content

Commit

Permalink
feat: PR check status to show summary e.g. Plan: 1 to add, 0 to chang…
Browse files Browse the repository at this point in the history
…e, 1 to destroy (#2983)

* feat: Update plan GitHub Status summary

* feat: revert result changes for Plan and Apply functions

* docs: Add CommandResult description

* feat: change plan status without ctx.CommandResult

* ensure DefaultCommitStatusUpdater implements runtime.statusUpdater

Co-authored-by: Alberto Rojas <alberto.rojas@n26.com>
  • Loading branch information
krrrr38 and albertorm95 authored Jan 14, 2023
1 parent 33bc28f commit ee7a5ca
Show file tree
Hide file tree
Showing 22 changed files with 399 additions and 149 deletions.
3 changes: 2 additions & 1 deletion server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
parallelPoolSize := 1
silenceNoProjects := false

statusUpdater := runtimemocks.NewMockStatusUpdater()
commitStatusUpdater := mocks.NewMockCommitStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()

Expand Down Expand Up @@ -1143,7 +1144,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
PlanStepRunner: runtime.NewPlanStepRunner(
terraformClient,
defaultTFVersion,
commitStatusUpdater,
statusUpdater,
asyncTfExec,
),
ShowStepRunner: showStepRunner,
Expand Down
2 changes: 1 addition & 1 deletion server/core/runtime/apply_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (a *ApplyStepRunner) runRemoteApply(

// updateStatusF will update the commit status and log any error.
updateStatusF := func(status models.CommitStatus, url string) {
if err := a.CommitStatusUpdater.UpdateProject(ctx, command.Apply, status, url); err != nil {
if err := a.CommitStatusUpdater.UpdateProject(ctx, command.Apply, status, url, nil); err != nil {
ctx.Log.Err("unable to update status: %s", err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions server/core/runtime/apply_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
. "github.com/petergtz/pegomock"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/runtime"
runtimemocks "github.com/runatlantis/atlantis/server/core/runtime/mocks"
runtimemodels "github.com/runatlantis/atlantis/server/core/runtime/models"
"github.com/runatlantis/atlantis/server/core/terraform/mocks"
matchers2 "github.com/runatlantis/atlantis/server/core/terraform/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/command"
mocks2 "github.com/runatlantis/atlantis/server/events/mocks"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
Expand Down Expand Up @@ -241,7 +241,7 @@ Plan: 0 to add, 0 to change, 1 to destroy.`
RegisterMockTestingT(t)
tfOut := fmt.Sprintf(preConfirmOutFmt, planFileContents) + postConfirmOut
tfExec := &remoteApplyMock{LinesToSend: tfOut, DoneCh: make(chan bool)}
updater := mocks2.NewMockCommitStatusUpdater()
updater := runtimemocks.NewMockStatusUpdater()
o := runtime.ApplyStepRunner{
AsyncTFExec: tfExec,
CommitStatusUpdater: updater,
Expand Down Expand Up @@ -273,8 +273,8 @@ Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

// Check that the status was updated with the run url.
runURL := "https://app.terraform.io/app/lkysow-enterprises/atlantis-tfe-test-dir2/runs/run-PiDsRYKGcerTttV2"
updater.VerifyWasCalledOnce().UpdateProject(ctx, command.Apply, models.PendingCommitStatus, runURL)
updater.VerifyWasCalledOnce().UpdateProject(ctx, command.Apply, models.SuccessCommitStatus, runURL)
updater.VerifyWasCalledOnce().UpdateProject(ctx, command.Apply, models.PendingCommitStatus, runURL, nil)
updater.VerifyWasCalledOnce().UpdateProject(ctx, command.Apply, models.SuccessCommitStatus, runURL, nil)
}

// Test that if the plan is different, we error out.
Expand Down Expand Up @@ -304,7 +304,7 @@ Plan: 0 to add, 0 to change, 1 to destroy.`
}
o := runtime.ApplyStepRunner{
AsyncTFExec: tfExec,
CommitStatusUpdater: mocks2.NewMockCommitStatusUpdater(),
CommitStatusUpdater: runtimemocks.NewMockStatusUpdater(),
}
tfVersion, _ := version.NewVersion("0.11.0")

Expand Down
33 changes: 33 additions & 0 deletions server/core/runtime/mocks/matchers/command_name.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions server/core/runtime/mocks/matchers/models_commitstatus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions server/core/runtime/mocks/matchers/ptr_to_command_projectresult.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions server/core/runtime/mocks/mock_status_updater.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/core/runtime/plan_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (p *planStepRunner) runRemotePlan(

// updateStatusF will update the commit status and log any error.
updateStatusF := func(status models.CommitStatus, url string) {
if err := p.CommitStatusUpdater.UpdateProject(ctx, command.Plan, status, url); err != nil {
if err := p.CommitStatusUpdater.UpdateProject(ctx, command.Plan, status, url, nil); err != nil {
ctx.Log.Err("unable to update status: %s", err)
}
}
Expand Down
22 changes: 10 additions & 12 deletions server/core/runtime/plan_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
"testing"

"github.com/hashicorp/go-version"
runtimemocks "github.com/runatlantis/atlantis/server/core/runtime/mocks"
"github.com/runatlantis/atlantis/server/events/command"
eventsmocks "github.com/runatlantis/atlantis/server/events/mocks"

. "github.com/petergtz/pegomock"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/runtime"
runtimemocks "github.com/runatlantis/atlantis/server/core/runtime/mocks"
runtimemodels "github.com/runatlantis/atlantis/server/core/runtime/models"
"github.com/runatlantis/atlantis/server/core/terraform/mocks"
matchers2 "github.com/runatlantis/atlantis/server/core/terraform/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
Expand All @@ -29,7 +27,7 @@ func TestRun_AddsEnvVarFile(t *testing.T) {
// Test that if env/workspace.tfvars file exists we use -var-file option.
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()

// Create the env/workspace.tfvars file.
Expand Down Expand Up @@ -98,7 +96,7 @@ func TestRun_UsesDiffPathForProject(t *testing.T) {
// file.
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()
tfVersion, _ := version.NewVersion("0.10.0")
logger := logging.NewNoopLogger(t)
Expand Down Expand Up @@ -178,7 +176,7 @@ Terraform will perform the following actions:
`
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()
tfVersion, _ := version.NewVersion("0.10.0")
s := runtime.NewPlanStepRunner(terraform, tfVersion, commitStatusUpdater, asyncTfExec)
Expand Down Expand Up @@ -229,7 +227,7 @@ Terraform will perform the following actions:
func TestRun_OutputOnErr(t *testing.T) {
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()
tfVersion, _ := version.NewVersion("0.10.0")
s := runtime.NewPlanStepRunner(terraform, tfVersion, commitStatusUpdater, asyncTfExec)
Expand Down Expand Up @@ -294,7 +292,7 @@ func TestRun_NoOptionalVarsIn012(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
asyncTfExec := runtimemocks.NewMockAsyncTFExec()
When(terraform.RunCommandWithVersion(
matchers.AnyCommandProjectContext(),
Expand Down Expand Up @@ -392,7 +390,7 @@ locally at this time.
}
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
commitStatusUpdater := eventsmocks.NewMockCommitStatusUpdater()
commitStatusUpdater := runtimemocks.NewMockStatusUpdater()
tfVersion, _ := version.NewVersion(c.tfVersion)
asyncTf := &remotePlanMock{}
s := runtime.NewPlanStepRunner(terraform, tfVersion, commitStatusUpdater, asyncTf)
Expand Down Expand Up @@ -471,8 +469,8 @@ Plan: 0 to add, 0 to change, 1 to destroy.`), "expect plan success")

// Ensure that the status was updated with the runURL.
runURL := "https://app.terraform.io/app/lkysow-enterprises/atlantis-tfe-test/runs/run-is4oVvJfrkud1KvE"
commitStatusUpdater.VerifyWasCalledOnce().UpdateProject(ctx, command.Plan, models.PendingCommitStatus, runURL)
commitStatusUpdater.VerifyWasCalledOnce().UpdateProject(ctx, command.Plan, models.SuccessCommitStatus, runURL)
commitStatusUpdater.VerifyWasCalledOnce().UpdateProject(ctx, command.Plan, models.PendingCommitStatus, runURL, nil)
commitStatusUpdater.VerifyWasCalledOnce().UpdateProject(ctx, command.Plan, models.SuccessCommitStatus, runURL, nil)
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion server/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ type AsyncTFExec interface {

// StatusUpdater brings the interface from CommitStatusUpdater into this package
// without causing circular imports.
//
//go:generate pegomock generate -m --package mocks -o mocks/mock_status_updater.go StatusUpdater
type StatusUpdater interface {
UpdateProject(ctx command.ProjectContext, cmdName command.Name, status models.CommitStatus, url string) error
UpdateProject(ctx command.ProjectContext, cmdName command.Name, status models.CommitStatus, url string, result *command.ProjectResult) error
}

// Runner mirrors events.StepRunner as a way to bring it into this package
Expand Down
Loading

0 comments on commit ee7a5ca

Please sign in to comment.