Skip to content

Commit

Permalink
feat: change plan status without ctx.CommandResult
Browse files Browse the repository at this point in the history
  • Loading branch information
krrrr38 committed Jan 14, 2023
1 parent e1103b6 commit df936b4
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 143 deletions.
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
2 changes: 0 additions & 2 deletions server/events/command/project_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const (
// be executed for a project.
type ProjectContext struct {
CommandName Name
// CommandResult represent the Terraform outputs after the command execution.
CommandResult ProjectResult
// ApplyCmd is the command that users should run to apply this plan. If
// this is an apply then this will be empty.
ApplyCmd string
Expand Down
2 changes: 1 addition & 1 deletion server/events/command_runner_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (m *MockCSU) UpdateCombined(repo models.Repo, pull models.PullRequest, stat
return nil
}

func (m *MockCSU) UpdateProject(ctx command.ProjectContext, cmdName command.Name, status models.CommitStatus, url string) error {
func (m *MockCSU) UpdateProject(ctx command.ProjectContext, cmdName command.Name, status models.CommitStatus, url string, result *command.ProjectResult) error {
return nil
}

Expand Down
Loading

0 comments on commit df936b4

Please sign in to comment.