Skip to content

Commit 48b342f

Browse files
feat: allow to unlock closed PRs (#2916)
1 parent 7746655 commit 48b342f

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

server/events/command_runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
156156
PullStatus: status,
157157
Trigger: command.AutoTrigger,
158158
}
159-
if !c.validateCtxAndComment(ctx) {
159+
if !c.validateCtxAndComment(ctx, command.Autoplan) {
160160
return
161161
}
162162
if c.DisableAutoplan {
@@ -281,7 +281,7 @@ func (c *DefaultCommandRunner) RunCommentCommand(baseRepo models.Repo, maybeHead
281281
Trigger: command.CommentTrigger,
282282
}
283283

284-
if !c.validateCtxAndComment(ctx) {
284+
if !c.validateCtxAndComment(ctx, cmd.Name) {
285285
return
286286
}
287287

@@ -391,7 +391,7 @@ func (c *DefaultCommandRunner) ensureValidRepoMetadata(
391391
return
392392
}
393393

394-
func (c *DefaultCommandRunner) validateCtxAndComment(ctx *command.Context) bool {
394+
func (c *DefaultCommandRunner) validateCtxAndComment(ctx *command.Context, commandName command.Name) bool {
395395
if !c.AllowForkPRs && ctx.HeadRepo.Owner != ctx.Pull.BaseRepo.Owner {
396396
if c.SilenceForkPRErrors {
397397
return false
@@ -403,7 +403,7 @@ func (c *DefaultCommandRunner) validateCtxAndComment(ctx *command.Context) bool
403403
return false
404404
}
405405

406-
if ctx.Pull.State != models.OpenPullState {
406+
if ctx.Pull.State != models.OpenPullState && commandName != command.Unlock {
407407
ctx.Log.Info("command was run on closed pull request")
408408
if err := c.VCSClient.CreateComment(ctx.Pull.BaseRepo, ctx.Pull.Num, "Atlantis commands can't be run on closed pull requests", ""); err != nil {
409409
ctx.Log.Err("unable to comment: %s", err)

server/events/command_runner_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func TestRunCommentCommand_ForkPRDisabled(t *testing.T) {
338338
headRepo.Owner = "forkrepo"
339339
When(eventParsing.ParseGithubPull(&pull)).ThenReturn(modelPull, modelPull.BaseRepo, headRepo, nil)
340340

341-
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, fixtures.Pull.Num, nil)
341+
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: command.Plan})
342342
commentMessage := fmt.Sprintf("Atlantis commands can't be run on fork pull requests. To enable, set --%s or, to disable this message, set --%s", ch.AllowForkPRsFlag, ch.SilenceForkPRErrorsFlag)
343343
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, commentMessage, "")
344344
}
@@ -357,7 +357,7 @@ func TestRunCommentCommand_ForkPRDisabled_SilenceEnabled(t *testing.T) {
357357
headRepo.Owner = "forkrepo"
358358
When(eventParsing.ParseGithubPull(&pull)).ThenReturn(modelPull, modelPull.BaseRepo, headRepo, nil)
359359

360-
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, fixtures.Pull.Num, nil)
360+
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: command.Plan})
361361
vcsClient.VerifyWasCalled(Never()).CreateComment(matchers.AnyModelsRepo(), AnyInt(), AnyString(), AnyString())
362362
}
363363

@@ -484,7 +484,7 @@ func TestRunCommentCommand_ClosedPull(t *testing.T) {
484484
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil)
485485
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil)
486486

487-
ch.RunCommentCommand(fixtures.GithubRepo, &fixtures.GithubRepo, nil, fixtures.User, fixtures.Pull.Num, nil)
487+
ch.RunCommentCommand(fixtures.GithubRepo, &fixtures.GithubRepo, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: command.Plan})
488488
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, "Atlantis commands can't be run on closed pull requests", "")
489489
}
490490

@@ -523,21 +523,40 @@ func TestRunCommentCommand_UnmatchedBranch(t *testing.T) {
523523
}
524524

525525
func TestRunUnlockCommand_VCSComment(t *testing.T) {
526-
t.Log("if unlock PR command is run, atlantis should" +
527-
" invoke the delete command and comment on PR accordingly")
526+
testCases := []struct {
527+
name string
528+
prState *string
529+
}{
530+
{
531+
name: "PR open",
532+
prState: github.String("open"),
533+
},
528534

529-
vcsClient := setup(t)
530-
pull := &github.PullRequest{
531-
State: github.String("open"),
535+
{
536+
name: "PR closed",
537+
prState: github.String("closed"),
538+
},
532539
}
533-
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num}
534-
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil)
535-
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil)
536540

537-
ch.RunCommentCommand(fixtures.GithubRepo, &fixtures.GithubRepo, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: command.Unlock})
541+
for _, tc := range testCases {
542+
t.Run(tc.name, func(t *testing.T) {
543+
t.Logf("if an unlock command is run on a pull request in state %s, atlantis should"+
544+
" invoke the delete command and comment on PR accordingly", *tc.prState)
538545

539-
deleteLockCommand.VerifyWasCalledOnce().DeleteLocksByPull(fixtures.GithubRepo.FullName, fixtures.Pull.Num)
540-
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, fixtures.Pull.Num, "All Atlantis locks for this PR have been unlocked and plans discarded", "unlock")
546+
vcsClient := setup(t)
547+
pull := &github.PullRequest{
548+
State: tc.prState,
549+
}
550+
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num}
551+
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil)
552+
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil)
553+
554+
ch.RunCommentCommand(fixtures.GithubRepo, &fixtures.GithubRepo, nil, fixtures.User, fixtures.Pull.Num, &events.CommentCommand{Name: command.Unlock})
555+
556+
deleteLockCommand.VerifyWasCalledOnce().DeleteLocksByPull(fixtures.GithubRepo.FullName, fixtures.Pull.Num)
557+
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, fixtures.Pull.Num, "All Atlantis locks for this PR have been unlocked and plans discarded", "unlock")
558+
})
559+
}
541560
}
542561

543562
func TestRunUnlockCommandFail_VCSComment(t *testing.T) {

0 commit comments

Comments
 (0)