Skip to content

Commit

Permalink
deleting unused object (#216)
Browse files Browse the repository at this point in the history
* deleting unused object

* fix tests
  • Loading branch information
msarvar authored Mar 22, 2022
1 parent ff9e62f commit 5fb8bdb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 96 deletions.
52 changes: 52 additions & 0 deletions server/events/project_command_output_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package events

import (
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
)

// ProjectOutputWrapper is a decorator that creates a new PR status check per project.
// The status contains a url that outputs current progress of the terraform plan/apply command.
type ProjectOutputWrapper struct {
ProjectCommandRunner
JobURLSetter JobURLSetter
JobCloser JobCloser
}

func (p *ProjectOutputWrapper) Plan(ctx command.ProjectContext) command.ProjectResult {
result := p.updateProjectPRStatus(command.Plan, ctx, p.ProjectCommandRunner.Plan)
p.JobCloser.CloseJob(ctx.JobID, ctx.BaseRepo)
return result
}

func (p *ProjectOutputWrapper) Apply(ctx command.ProjectContext) command.ProjectResult {
result := p.updateProjectPRStatus(command.Apply, ctx, p.ProjectCommandRunner.Apply)
p.JobCloser.CloseJob(ctx.JobID, ctx.BaseRepo)
return result
}

func (p *ProjectOutputWrapper) updateProjectPRStatus(commandName command.Name, ctx command.ProjectContext, execute func(ctx command.ProjectContext) command.ProjectResult) command.ProjectResult {
// Create a PR status to track project's plan status. The status will
// include a link to view the progress of atlantis plan command in real
// time
if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.PendingCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

// ensures we are differentiating between project level command and overall command
result := execute(ctx)

if result.Error != nil || result.Failure != "" {
if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.FailedCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

return result
}

if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.SuccessCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

return result
}
56 changes: 0 additions & 56 deletions server/events/project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/webhooks"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/lyft/feature"
)

const OperationComplete = true
Expand Down Expand Up @@ -107,61 +106,6 @@ type JobCloser interface {
CloseJob(jobID string, repo models.Repo)
}

// ProjectOutputWrapper is a decorator that creates a new PR status check per project.
// The status contains a url that outputs current progress of the terraform plan/apply command.
type ProjectOutputWrapper struct {
ProjectCommandRunner
JobURLSetter JobURLSetter
JobCloser JobCloser
}

func (p *ProjectOutputWrapper) Plan(ctx command.ProjectContext) command.ProjectResult {
result := p.updateProjectPRStatus(command.Plan, ctx, p.ProjectCommandRunner.Plan)
p.JobCloser.CloseJob(ctx.JobID, ctx.BaseRepo)
return result
}

func (p *ProjectOutputWrapper) Apply(ctx command.ProjectContext) command.ProjectResult {
result := p.updateProjectPRStatus(command.Apply, ctx, p.ProjectCommandRunner.Apply)
p.JobCloser.CloseJob(ctx.JobID, ctx.BaseRepo)
return result
}

func (p *ProjectOutputWrapper) updateProjectPRStatus(commandName command.Name, ctx command.ProjectContext, execute func(ctx command.ProjectContext) command.ProjectResult) command.ProjectResult {
// Create a PR status to track project's plan status. The status will
// include a link to view the progress of atlantis plan command in real
// time
if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.PendingCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

// ensures we are differentiating between project level command and overall command
result := execute(ctx)

if result.Error != nil || result.Failure != "" {
if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.FailedCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

return result
}

if err := p.JobURLSetter.SetJobURLWithStatus(ctx, commandName, models.SuccessCommitStatus); err != nil {
ctx.Log.Err("updating project PR status", err)
}

return result
}

type FeatureAwareProjectCommandRunner struct {
ProjectCommandRunner
FeatureAllocator feature.Allocator
}

func (f *FeatureAwareProjectCommandRunner) Apply(ctx command.ProjectContext) command.ProjectResult {
return f.ProjectCommandRunner.Apply(ctx)
}

func NewProjectCommandRunner(
stepsRunner runtime.StepsRunner,
locker ProjectLocker,
Expand Down
34 changes: 0 additions & 34 deletions server/events/project_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,40 +283,6 @@ func TestDefaultProjectCommandRunner_ForceOverridesApplyReqs(t *testing.T) {
Equals(t, "", res.Failure)
}

func TestFeatureAwareProjectCommandRunner_ForceOverrideWhenEnabled(t *testing.T) {
RegisterMockTestingT(t)
mockWorkingDir := mocks.NewMockWorkingDir()
mockSender := mocks.NewMockWebhooksSender()
runner := &events.DefaultProjectCommandRunner{
WorkingDir: mockWorkingDir,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
StepsRunner: smocks.NewMockStepsRunner(),
AggregateApplyRequirements: &events.AggregateApplyRequirements{
WorkingDir: mockWorkingDir,
},
Webhooks: mockSender,
}
featureAwareRunner := &events.FeatureAwareProjectCommandRunner{
ProjectCommandRunner: runner,
}
ctx := command.ProjectContext{
ApplyRequirements: []string{"approved"},
ForceApply: true,
PullReqStatus: models.PullReqStatus{
ApprovalStatus: models.ApprovalStatus{
IsApproved: false,
},
},
Log: logging.NewNoopLogger(t),
}
tmp, cleanup := TempDir(t)
defer cleanup()
When(mockWorkingDir.GetWorkingDir(ctx.BaseRepo, ctx.Pull, ctx.Workspace)).ThenReturn(tmp, nil)

res := featureAwareRunner.Apply(ctx)
Equals(t, "", res.Failure)
}

// Test that if mergeable is required and the PR isn't mergeable we give an error.
func TestDefaultProjectCommandRunner_ApplyNotMergeable(t *testing.T) {
RegisterMockTestingT(t)
Expand Down
7 changes: 1 addition & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,6 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
JobCloser: projectCmdOutputHandler,
}

featureAwareProjectCommandRunner := &events.FeatureAwareProjectCommandRunner{
FeatureAllocator: featureAllocator,
ProjectCommandRunner: projectOutputWrapper,
}

session, err := aws.NewSession()
if err != nil {
return nil, errors.Wrap(err, "initializing new aws session")
Expand All @@ -656,7 +651,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
}
auditProjectCmdRunner := &lyftDecorators.AuditProjectCommandWrapper{
SnsWriter: snsWriter,
ProjectCommandRunner: featureAwareProjectCommandRunner,
ProjectCommandRunner: projectOutputWrapper,
}

instrumentedProjectCmdRunner := &events.InstrumentedProjectCommandRunner{
Expand Down

0 comments on commit 5fb8bdb

Please sign in to comment.