Skip to content

Commit 5a80c27

Browse files
X-GuardianPetr Bubenik
authored andcommitted
fix: Pre Workflow Hook VCS Combined Status Check Set to Pending Twice (runatlantis#5242)
Signed-off-by: X-Guardian <[email protected]> Signed-off-by: Petr Bubenik <[email protected]>
1 parent dc660c0 commit 5a80c27

8 files changed

+47
-44
lines changed

server/controllers/api_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type APIController struct {
3333
RepoAllowlistChecker *events.RepoAllowlistChecker
3434
Scope tally.Scope
3535
VCSClient vcs.Client
36+
CommitStatusUpdater events.CommitStatusUpdater
3637
}
3738

3839
type APIRequest struct {
@@ -150,6 +151,11 @@ func (a *APIController) apiPlan(request *APIRequest, ctx *command.Context) (*com
150151
return nil, err
151152
}
152153

154+
// Update the combined plan commit status to pending
155+
if err := a.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Plan); err != nil {
156+
ctx.Log.Warn("unable to update plan commit status: %s", err)
157+
}
158+
153159
var projectResults []command.ProjectResult
154160
for i, cmd := range cmds {
155161
err = a.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, cc[i])
@@ -173,6 +179,11 @@ func (a *APIController) apiApply(request *APIRequest, ctx *command.Context) (*co
173179
return nil, err
174180
}
175181

182+
// Update the combined apply commit status to pending
183+
if err := a.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Apply); err != nil {
184+
ctx.Log.Warn("unable to update apply commit status: %s", err)
185+
}
186+
176187
var projectResults []command.ProjectResult
177188
for i, cmd := range cmds {
178189
err = a.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, cc[i])

server/controllers/api_controller_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func setup(t *testing.T) (controllers.APIController, *MockProjectCommandBuilder,
9494

9595
When(postWorkflowHooksCommandRunner.RunPostHooks(Any[*command.Context](), Any[*events.CommentCommand]())).ThenReturn(nil)
9696

97+
commitStatusUpdater := NewMockCommitStatusUpdater()
98+
99+
When(commitStatusUpdater.UpdateCombined(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](), Any[models.CommitStatus](), Any[command.Name]())).ThenReturn(nil)
100+
97101
ac := controllers.APIController{
98102
APISecret: []byte(atlantisToken),
99103
Locker: locker,
@@ -107,6 +111,7 @@ func setup(t *testing.T) (controllers.APIController, *MockProjectCommandBuilder,
107111
PostWorkflowHooksCommandRunner: postWorkflowHooksCommandRunner,
108112
VCSClient: vcsClient,
109113
RepoAllowlistChecker: repoAllowlistChecker,
114+
CommitStatusUpdater: commitStatusUpdater,
110115
}
111116
return ac, projectCommandBuilder, projectCommandRunner
112117
}

server/controllers/events/events_controller_e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
16471647
PostWorkflowHooksCommandRunner: postWorkflowHooksCommandRunner,
16481648
PullStatusFetcher: backend,
16491649
DisableAutoplan: opt.disableAutoplan,
1650+
CommitStatusUpdater: commitStatusUpdater,
16501651
}
16511652

16521653
repoAllowlistChecker, err := events.NewRepoAllowlistChecker("*")

server/events/apply_command_runner.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) {
9494
return
9595
}
9696

97-
if err = a.commitStatusUpdater.UpdateCombined(ctx.Log, baseRepo, pull, models.PendingCommitStatus, cmd.CommandName()); err != nil {
98-
ctx.Log.Warn("unable to update commit status: %s", err)
99-
}
100-
10197
// Get the mergeable status before we set any build statuses of our own.
10298
// We do this here because when we set a "Pending" status, if users have
10399
// required the Atlantis status checks to pass, then we've now changed

server/events/command_runner.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
202202
cmd := &CommentCommand{
203203
Name: command.Autoplan,
204204
}
205+
206+
// Update the combined plan commit status to pending
207+
if err := c.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Plan); err != nil {
208+
ctx.Log.Warn("unable to update plan commit status: %s", err)
209+
}
210+
205211
err = c.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, cmd)
206212

207213
if err != nil {
@@ -354,6 +360,18 @@ func (c *DefaultCommandRunner) RunCommentCommand(baseRepo models.Repo, maybeHead
354360
return
355361
}
356362

363+
// Update the combined plan or apply commit status to pending
364+
switch cmd.Name {
365+
case command.Plan:
366+
if err := c.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Plan); err != nil {
367+
ctx.Log.Warn("unable to update plan commit status: %s", err)
368+
}
369+
case command.Apply:
370+
if err := c.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Apply); err != nil {
371+
ctx.Log.Warn("unable to update apply commit status: %s", err)
372+
}
373+
}
374+
357375
err = c.PreWorkflowHooksCommandRunner.RunPreHooks(ctx, cmd)
358376

359377
if err != nil {

server/events/command_runner_test.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock
253253
PreWorkflowHooksCommandRunner: preWorkflowHooksCommandRunner,
254254
PostWorkflowHooksCommandRunner: postWorkflowHooksCommandRunner,
255255
PullStatusFetcher: testConfig.backend,
256+
CommitStatusUpdater: commitUpdater,
256257
}
257258

258259
return vcsClient
@@ -440,15 +441,8 @@ func TestRunCommentCommandApply_NoProjects_SilenceEnabled(t *testing.T) {
440441
ch.RunCommentCommand(testdata.GithubRepo, nil, nil, testdata.User, testdata.Pull.Num, &events.CommentCommand{Name: command.Apply})
441442
vcsClient.VerifyWasCalled(Never()).CreateComment(
442443
Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string]())
443-
commitUpdater.VerifyWasCalledOnce().UpdateCombinedCount(
444-
Any[logging.SimpleLogging](),
445-
Any[models.Repo](),
446-
Any[models.PullRequest](),
447-
Eq[models.CommitStatus](models.SuccessCommitStatus),
448-
Eq[command.Name](command.Apply),
449-
Eq(0),
450-
Eq(0),
451-
)
444+
commitUpdater.VerifyWasCalledOnce().UpdateCombined(
445+
Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](), Eq(models.PendingCommitStatus), Eq(command.Apply))
452446
}
453447

454448
func TestRunCommentCommandApprovePolicy_NoProjects_SilenceEnabled(t *testing.T) {
@@ -463,15 +457,6 @@ func TestRunCommentCommandApprovePolicy_NoProjects_SilenceEnabled(t *testing.T)
463457
ch.RunCommentCommand(testdata.GithubRepo, nil, nil, testdata.User, testdata.Pull.Num, &events.CommentCommand{Name: command.ApprovePolicies})
464458
vcsClient.VerifyWasCalled(Never()).CreateComment(
465459
Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string]())
466-
commitUpdater.VerifyWasCalledOnce().UpdateCombinedCount(
467-
Any[logging.SimpleLogging](),
468-
Any[models.Repo](),
469-
Any[models.PullRequest](),
470-
Eq[models.CommitStatus](models.SuccessCommitStatus),
471-
Eq[command.Name](command.PolicyCheck),
472-
Eq(0),
473-
Eq(0),
474-
)
475460
}
476461

477462
func TestRunCommentCommandUnlock_NoProjects_SilenceEnabled(t *testing.T) {
@@ -485,6 +470,8 @@ func TestRunCommentCommandUnlock_NoProjects_SilenceEnabled(t *testing.T) {
485470

486471
ch.RunCommentCommand(testdata.GithubRepo, nil, nil, testdata.User, testdata.Pull.Num, &events.CommentCommand{Name: command.Unlock})
487472
vcsClient.VerifyWasCalled(Never()).CreateComment(Any[logging.SimpleLogging](), Any[models.Repo](), Any[int](), Any[string](), Any[string]())
473+
commitUpdater.VerifyWasCalled(Never()).UpdateCombined(
474+
Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](), Eq(models.PendingCommitStatus), Any[command.Name]())
488475
}
489476

490477
func TestRunCommentCommandImport_NoProjects_SilenceEnabled(t *testing.T) {
@@ -535,7 +522,7 @@ func TestRunCommentCommand_DisableAutoplan(t *testing.T) {
535522
CommandName: command.Plan,
536523
},
537524
}, nil)
538-
525+
When(commitUpdater.UpdateCombinedCount(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](), Any[models.CommitStatus](), Any[command.Name](), Any[int](), Any[int]())).ThenReturn(nil)
539526
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, modelPull, testdata.User)
540527
projectCommandBuilder.VerifyWasCalled(Never()).BuildAutoplanCommands(Any[*command.Context]())
541528
}
@@ -831,6 +818,10 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Fal
831818
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, testdata.Pull, testdata.User)
832819
pendingPlanFinder.VerifyWasCalledOnce().DeletePlans(tmp)
833820
lockingLocker.VerifyWasCalledOnce().UnlockByPull(testdata.Pull.BaseRepo.FullName, testdata.Pull.Num)
821+
commitUpdater.VerifyWasCalledOnce().UpdateCombined(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](),
822+
Eq(models.PendingCommitStatus), Eq(command.Plan))
823+
commitUpdater.VerifyWasCalled(Never()).UpdateCombined(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](),
824+
Eq(models.FailedCommitStatus), Any[command.Name]())
834825
}
835826

836827
func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_True(t *testing.T) {
@@ -853,6 +844,8 @@ func TestRunAutoplanCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_Tru
853844
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, testdata.Pull, testdata.User)
854845
pendingPlanFinder.VerifyWasCalled(Never()).DeletePlans(Any[string]())
855846
lockingLocker.VerifyWasCalled(Never()).UnlockByPull(Any[string](), Any[int]())
847+
commitUpdater.VerifyWasCalledOnce().UpdateCombined(Any[logging.SimpleLogging](), Any[models.Repo](), Any[models.PullRequest](),
848+
Eq(models.PendingCommitStatus), Eq(command.Plan))
856849
}
857850

858851
func TestRunCommentCommand_FailedPreWorkflowHook_FailOnPreWorkflowHookError_False(t *testing.T) {

server/events/plan_command_runner.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ func (p *PlanCommandRunner) runAutoplan(ctx *command.Context) {
114114
return
115115
}
116116

117-
// At this point we are sure Atlantis has work to do, so set commit status to pending
118-
if err := p.commitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Plan); err != nil {
119-
ctx.Log.Warn("unable to update plan commit status: %s", err)
120-
}
121-
122117
// discard previous plans that might not be relevant anymore
123118
ctx.Log.Debug("deleting previous plans and locks")
124119
p.deletePlans(ctx)
@@ -188,10 +183,6 @@ func (p *PlanCommandRunner) run(ctx *command.Context, cmd *CommentCommand) {
188183
}
189184
}
190185

191-
if err = p.commitStatusUpdater.UpdateCombined(ctx.Log, baseRepo, pull, models.PendingCommitStatus, command.Plan); err != nil {
192-
ctx.Log.Warn("unable to update commit status: %s", err)
193-
}
194-
195186
projectCmds, err := p.prjCmdBuilder.BuildPlanCommands(ctx, cmd)
196187
if err != nil {
197188
if statusErr := p.commitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.FailedCommitStatus, command.Plan); statusErr != nil {

server/events/pre_workflow_hooks_command_runner.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks(ctx *command.Context,
6969
escapedArgs = escapeArgs(cmd.Flags)
7070
}
7171

72-
// Update the plan or apply commit status to pending whilst the pre workflow hook is running
73-
switch cmd.Name {
74-
case command.Plan:
75-
if err := w.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Plan); err != nil {
76-
ctx.Log.Warn("unable to update plan commit status: %s", err)
77-
}
78-
case command.Apply:
79-
if err := w.CommitStatusUpdater.UpdateCombined(ctx.Log, ctx.Pull.BaseRepo, ctx.Pull, models.PendingCommitStatus, command.Apply); err != nil {
80-
ctx.Log.Warn("unable to update apply commit status: %s", err)
81-
}
82-
}
83-
8472
err = w.runHooks(
8573
models.WorkflowHookCommandContext{
8674
BaseRepo: ctx.Pull.BaseRepo,

0 commit comments

Comments
 (0)