Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable autoplan label #3649

Merged
merged 22 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
DisableApplyAllFlag = "disable-apply-all"
DisableApplyFlag = "disable-apply"
DisableAutoplanFlag = "disable-autoplan"
DisableAutoplanLabelFlag = "disable-autoplan-label"
DisableMarkdownFoldingFlag = "disable-markdown-folding"
DisableRepoLockingFlag = "disable-repo-locking"
DiscardApprovalOnPlanFlag = "discard-approval-on-plan"
Expand Down Expand Up @@ -254,6 +255,10 @@ var stringFlags = map[string]stringFlag{
description: "Path to directory to store Atlantis data.",
defaultValue: DefaultDataDir,
},
DisableAutoplanLabelFlag: {
description: "Pull request label to disable atlantis auto planning feature only if present.",
defaultValue: "",
},
EmojiReaction: {
description: "Emoji Reaction to use to react to comments",
defaultValue: DefaultEmojiReaction,
Expand Down
1 change: 1 addition & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var testFlags = map[string]interface{}{
VCSStatusName: "my-status",
WriteGitCredsFlag: true,
DisableAutoplanFlag: true,
DisableAutoplanLabelFlag: "no-auto-plan",
EnablePolicyChecksFlag: false,
EnableRegExpCmdFlag: false,
EnableDiffMarkdownFormat: false,
Expand Down
10 changes: 10 additions & 0 deletions runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ and set `--autoplan-modules` to `false`.
```
Disable atlantis auto planning.

### `--disable-autoplan-label`
```bash
atlantis server --disable-autoplan-label="no-autoplan"
# or
ATLANTIS_DISABLE_AUTOPLAN_LABEL="no-autoplan"
```
Disable atlantis auto planning only on pull requests with the specified label.

If `disable-autoplan` property is `true`, this flag has no effect.

### `--disable-markdown-folding`
```bash
atlantis server --disable-markdown-folding
Expand Down
10 changes: 10 additions & 0 deletions server/events/command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package events

import (
"fmt"
"github.com/runatlantis/atlantis/server/utils"
"strconv"

"github.com/google/go-github/v53/github"
Expand Down Expand Up @@ -97,6 +98,7 @@ type DefaultCommandRunner struct {
AzureDevopsPullGetter AzureDevopsPullGetter
GitlabMergeRequestGetter GitlabMergeRequestGetter
DisableAutoplan bool
DisableAutoplanLabel string
EventParser EventParsing
Logger logging.SimpleLogging
GlobalCfg valid.GlobalCfg
Expand Down Expand Up @@ -162,6 +164,14 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
if c.DisableAutoplan {
return
}
if len(c.DisableAutoplanLabel) > 0 {
labels, err := c.VCSClient.GetPullLabels(baseRepo, pull)
if err != nil {
ctx.Log.Err("Unable to get pull labels. Proceeding with %s command.", err, command.Plan)
} else if utils.SlicesContains(labels, c.DisableAutoplanLabel) {
return
}
}

cmd := &CommentCommand{
Name: command.Autoplan,
Expand Down
56 changes: 53 additions & 3 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,11 @@ func TestRunCommentCommand_DisableApplyAllDisabled(t *testing.T) {
vcsClient.VerifyWasCalledOnce().CreateComment(testdata.GithubRepo, modelPull.Num, "**Error:** Running `atlantis apply` without flags is disabled. You must specify which project to apply via the `-d <dir>`, `-w <workspace>` or `-p <project name>` flags.", "apply")
}

func TestRunCommentCommand_DisableDisableAutoplan(t *testing.T) {
t.Log("if \"DisableAutoplan is true\" are disabled and we are silencing return and do not comment with error")
func TestRunCommentCommand_DisableAutoplan(t *testing.T) {
t.Log("if \"DisableAutoplan\" is true, auto plans are disabled and we are silencing return and do not comment with error")
setup(t)
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, BaseBranch: "main"}

ch.DisableAutoplan = true
defer func() { ch.DisableAutoplan = false }()

Expand All @@ -511,8 +513,56 @@ func TestRunCommentCommand_DisableDisableAutoplan(t *testing.T) {
},
}, nil)

ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, testdata.Pull, testdata.User)
ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, modelPull, testdata.User)
projectCommandBuilder.VerifyWasCalled(Never()).BuildAutoplanCommands(Any[*command.Context]())
}

func TestRunCommentCommand_DisableAutoplanLabel(t *testing.T) {
t.Log("if \"DisableAutoplanLabel\" is present and pull request has that label, auto plans are disabled and we are silencing return and do not comment with error")
vcsClient := setup(t)
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, BaseBranch: "main"}

ch.DisableAutoplanLabel = "disable-auto-plan"
defer func() { ch.DisableAutoplanLabel = "" }()

When(projectCommandBuilder.BuildAutoplanCommands(Any[*command.Context]())).
ThenReturn([]command.ProjectContext{
{
CommandName: command.Plan,
},
{
CommandName: command.Plan,
},
}, nil)
When(ch.VCSClient.GetPullLabels(testdata.GithubRepo, modelPull)).ThenReturn([]string{"disable-auto-plan", "need-help"}, nil)

ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, modelPull, testdata.User)
projectCommandBuilder.VerifyWasCalled(Never()).BuildAutoplanCommands(Any[*command.Context]())
vcsClient.VerifyWasCalledOnce().GetPullLabels(testdata.GithubRepo, modelPull)
}

func TestRunCommentCommand_DisableAutoplanLabel_PullNotLabeled(t *testing.T) {
t.Log("if \"DisableAutoplanLabel\" is present but pull request doesn't have that label, auto plans run")
vcsClient := setup(t)
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, BaseBranch: "main"}

ch.DisableAutoplanLabel = "disable-auto-plan"
defer func() { ch.DisableAutoplanLabel = "" }()

When(projectCommandBuilder.BuildAutoplanCommands(Any[*command.Context]())).
ThenReturn([]command.ProjectContext{
{
CommandName: command.Plan,
},
{
CommandName: command.Plan,
},
}, nil)
When(ch.VCSClient.GetPullLabels(testdata.GithubRepo, modelPull)).ThenReturn([]string{}, nil)

ch.RunAutoplanCommand(testdata.GithubRepo, testdata.GithubRepo, modelPull, testdata.User)
projectCommandBuilder.VerifyWasCalled(Once()).BuildAutoplanCommands(Any[*command.Context]())
vcsClient.VerifyWasCalledOnce().GetPullLabels(testdata.GithubRepo, modelPull)
}

func TestRunCommentCommand_ClosedPull(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions server/events/vcs/azuredevops_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,7 @@ func GitStatusContextFromSrc(src string) *azuredevops.GitStatusContext {
func (g *AzureDevopsClient) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return "", fmt.Errorf("not yet implemented")
}

func (g *AzureDevopsClient) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
return nil, fmt.Errorf("not yet implemented")
}
4 changes: 4 additions & 0 deletions server/events/vcs/bitbucketcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,7 @@ func (b *Client) GetFileContent(pull models.PullRequest, fileName string) (bool,
func (b *Client) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return "", fmt.Errorf("not yet implemented")
}

func (b *Client) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
return nil, fmt.Errorf("not yet implemented")
}
4 changes: 4 additions & 0 deletions server/events/vcs/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,7 @@ func (b *Client) GetFileContent(pull models.PullRequest, fileName string) (bool,
func (b *Client) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return "", fmt.Errorf("not yet implemented")
}

func (b *Client) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
return nil, fmt.Errorf("not yet implemented")
}
3 changes: 3 additions & 0 deletions server/events/vcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ type Client interface {
GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error)
SupportsSingleFileDownload(repo models.Repo) bool
GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error)

// GetPullLabels returns the labels of a pull request
GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error)
}
15 changes: 15 additions & 0 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,18 @@ func (g *GithubClient) GetCloneURL(VCSHostType models.VCSHostType, repo string)
}
return repository.GetCloneURL(), nil
}

func (g *GithubClient) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
ghaiszaher marked this conversation as resolved.
Show resolved Hide resolved
pullDetails, _, err := g.client.PullRequests.Get(g.ctx, repo.Owner, repo.Name, pull.Num)
if err != nil {
return nil, err
}

var labels []string

for _, label := range pullDetails.Labels {
labels = append(labels, *label.Name)
}

return labels, nil
}
10 changes: 10 additions & 0 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,13 @@ func (g *GitlabClient) GetCloneURL(VCSHostType models.VCSHostType, repo string)
}
return project.HTTPURLToRepo, nil
}

func (g *GitlabClient) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
ghaiszaher marked this conversation as resolved.
Show resolved Hide resolved
mr, _, err := g.Client.MergeRequests.GetMergeRequest(repo.FullName, pull.Num, nil)

if err != nil {
return nil, err
}

return mr.Labels, nil
}
50 changes: 50 additions & 0 deletions server/events/vcs/mocks/mock_client.go

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

4 changes: 4 additions & 0 deletions server/events/vcs/not_configured_vcs_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ func (a *NotConfiguredVCSClient) GetFileContent(pull models.PullRequest, fileNam
func (a *NotConfiguredVCSClient) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return "", a.err()
}

func (a *NotConfiguredVCSClient) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
return nil, a.err()
}
4 changes: 4 additions & 0 deletions server/events/vcs/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ func (d *ClientProxy) SupportsSingleFileDownload(repo models.Repo) bool {
func (d *ClientProxy) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
return d.clients[VCSHostType].GetCloneURL(VCSHostType, repo)
}

func (d *ClientProxy) GetPullLabels(repo models.Repo, pull models.PullRequest) ([]string, error) {
return d.clients[repo.VCSHost.Type].GetPullLabels(repo, pull)
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
SilenceForkPRErrors: userConfig.SilenceForkPRErrors,
SilenceForkPRErrorsFlag: config.SilenceForkPRErrorsFlag,
DisableAutoplan: userConfig.DisableAutoplan,
DisableAutoplanLabel: userConfig.DisableAutoplanLabel,
Drainer: drainer,
PreWorkflowHooksCommandRunner: preWorkflowHooksCommandRunner,
PostWorkflowHooksCommandRunner: postWorkflowHooksCommandRunner,
Expand Down
1 change: 1 addition & 0 deletions server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type UserConfig struct {
DisableApplyAll bool `mapstructure:"disable-apply-all"`
DisableApply bool `mapstructure:"disable-apply"`
DisableAutoplan bool `mapstructure:"disable-autoplan"`
DisableAutoplanLabel string `mapstructure:"disable-autoplan-label"`
DisableMarkdownFolding bool `mapstructure:"disable-markdown-folding"`
DisableRepoLocking bool `mapstructure:"disable-repo-locking"`
DiscardApprovalOnPlanFlag bool `mapstructure:"discard-approval-on-plan"`
Expand Down