From c300dee745898ce31d54daee1fd4f104200461ab Mon Sep 17 00:00:00 2001 From: Christopher Ketchum Date: Fri, 24 Apr 2020 18:58:40 -0700 Subject: [PATCH 1/2] Add flag to toggle autoplanning on draft PRs --- cmd/server.go | 5 +++++ cmd/server_test.go | 1 + server/server.go | 1 + server/user_config.go | 1 + 4 files changed, 8 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index b0405768f3..2a7006b63a 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -64,6 +64,7 @@ const ( GitlabWebhookSecretFlag = "gitlab-webhook-secret" // nolint: gosec HidePrevPlanComments = "hide-prev-plan-comments" LogLevelFlag = "log-level" + PlanDrafts = "plan-drafts" PortFlag = "port" RepoConfigFlag = "repo-config" RepoConfigJSONFlag = "repo-config-json" @@ -258,6 +259,10 @@ var boolFlags = map[string]boolFlag{ "VCS support is limited to: GitHub.", defaultValue: false, }, + PlanDrafts: { + description: "Enable autoplan for Github Draft Pull Requests", + defaultValue: false, + }, RequireApprovalFlag: { description: "Require pull requests to be \"Approved\" before allowing the apply command to be run.", defaultValue: false, diff --git a/cmd/server_test.go b/cmd/server_test.go index 7a197b1e77..d9e9fc1d46 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -75,6 +75,7 @@ var testFlags = map[string]interface{}{ GitlabUserFlag: "gitlab-user", GitlabWebhookSecretFlag: "gitlab-secret", LogLevelFlag: "debug", + PlanDrafts: true, PortFlag: 8181, RepoWhitelistFlag: "github.com/runatlantis/atlantis", RequireApprovalFlag: true, diff --git a/server/server.go b/server/server.go index 4a36123e66..50a9a834e5 100644 --- a/server/server.go +++ b/server/server.go @@ -290,6 +290,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { GithubToken: userConfig.GithubToken, GitlabUser: userConfig.GitlabUser, GitlabToken: userConfig.GitlabToken, + PlanDrafts: userConfig.PlanDrafts, BitbucketUser: userConfig.BitbucketUser, BitbucketToken: userConfig.BitbucketToken, BitbucketServerURL: userConfig.BitbucketBaseURL, diff --git a/server/user_config.go b/server/user_config.go index 2524636707..958400e54b 100644 --- a/server/user_config.go +++ b/server/user_config.go @@ -34,6 +34,7 @@ type UserConfig struct { GitlabWebhookSecret string `mapstructure:"gitlab-webhook-secret"` HidePrevPlanComments bool `mapstructure:"hide-prev-plan-comments"` LogLevel string `mapstructure:"log-level"` + PlanDrafts bool `mapstructure:"plan-drafts"` Port int `mapstructure:"port"` RepoConfig string `mapstructure:"repo-config"` RepoConfigJSON string `mapstructure:"repo-config-json"` From d7ce63742ec3a81021363e84252714206c2a5d03 Mon Sep 17 00:00:00 2001 From: CJ Ketchum Date: Fri, 24 Apr 2020 19:24:56 -0700 Subject: [PATCH 2/2] Add test for plan-draft behavior --- cmd/server.go | 10 +++++----- cmd/server_test.go | 2 +- server/events/event_parser.go | 7 ++++--- server/events/event_parser_test.go | 17 +++++++++++++++++ server/server.go | 2 +- server/user_config.go | 2 +- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 2a7006b63a..9bfab8e2c8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -64,7 +64,7 @@ const ( GitlabWebhookSecretFlag = "gitlab-webhook-secret" // nolint: gosec HidePrevPlanComments = "hide-prev-plan-comments" LogLevelFlag = "log-level" - PlanDrafts = "plan-drafts" + AllowDraftPRs = "allow-draft-prs" PortFlag = "port" RepoConfigFlag = "repo-config" RepoConfigJSONFlag = "repo-config-json" @@ -254,15 +254,15 @@ var boolFlags = map[string]boolFlag{ description: "Disable \"atlantis apply\" command so a specific project/workspace/directory has to be specified for applies.", defaultValue: false, }, + AllowDraftPRs: { + description: "Enable autoplan for Github Draft Pull Requests", + defaultValue: false, + }, HidePrevPlanComments: { description: "Hide previous plan comments to reduce clutter in the PR. " + "VCS support is limited to: GitHub.", defaultValue: false, }, - PlanDrafts: { - description: "Enable autoplan for Github Draft Pull Requests", - defaultValue: false, - }, RequireApprovalFlag: { description: "Require pull requests to be \"Approved\" before allowing the apply command to be run.", defaultValue: false, diff --git a/cmd/server_test.go b/cmd/server_test.go index d9e9fc1d46..e6f3728976 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -75,7 +75,7 @@ var testFlags = map[string]interface{}{ GitlabUserFlag: "gitlab-user", GitlabWebhookSecretFlag: "gitlab-secret", LogLevelFlag: "debug", - PlanDrafts: true, + AllowDraftPRs: true, PortFlag: 8181, RepoWhitelistFlag: "github.com/runatlantis/atlantis", RequireApprovalFlag: true, diff --git a/server/events/event_parser.go b/server/events/event_parser.go index c61f228ebb..1eabf91341 100644 --- a/server/events/event_parser.go +++ b/server/events/event_parser.go @@ -266,6 +266,7 @@ type EventParser struct { GithubToken string GitlabUser string GitlabToken string + AllowDraftPRs bool BitbucketUser string BitbucketToken string BitbucketServerURL string @@ -418,11 +419,11 @@ func (e *EventParser) ParseGithubPullEvent(pullEvent *github.PullRequestEvent) ( } if pullEvent.GetPullRequest().GetDraft() { - // if the PR is in draft state we do not initiate actions proactively however, - // we must still clean up locks in the event of a user initiated plan + // Even if the PR is in draft state users can manually run plan or may + // be using the -allow-draft-prs flag. If so then we need to ensure locks are cleaned up. if pullEvent.GetAction() == "closed" { pullEventType = models.ClosedPullEvent - } else { + } else if !e.AllowDraftPRs { pullEventType = models.OtherPullEvent } } else { diff --git a/server/events/event_parser_test.go b/server/events/event_parser_test.go index 969e36269a..f982d90dbc 100644 --- a/server/events/event_parser_test.go +++ b/server/events/event_parser_test.go @@ -36,6 +36,7 @@ var parser = events.EventParser{ GithubToken: "github-token", GitlabUser: "gitlab-user", GitlabToken: "gitlab-token", + AllowDraftPRs: false, BitbucketUser: "bitbucket-user", BitbucketToken: "bitbucket-token", BitbucketServerURL: "http://mycorp.com:7490", @@ -160,6 +161,22 @@ func TestParseGithubPullEvent(t *testing.T) { Equals(t, models.User{Username: "user"}, actUser) } +func TestParseGithubPullEventFromDraft(t *testing.T) { + // verify that draft PRs are treated as 'other' events by default + testEvent := deepcopy.Copy(PullEvent).(github.PullRequestEvent) + draftPR := true + testEvent.PullRequest.Draft = &draftPR + _, evType, _, _, _, err := parser.ParseGithubPullEvent(&testEvent) + Ok(t, err) + Equals(t, models.OtherPullEvent, evType) + // verify that drafts are planned if requested + parser.AllowDraftPRs = true + defer func() { parser.AllowDraftPRs = false }() + _, evType, _, _, _, err = parser.ParseGithubPullEvent(&testEvent) + Ok(t, err) + Equals(t, models.OpenedPullEvent, evType) +} + func TestParseGithubPullEvent_EventType(t *testing.T) { cases := []struct { action string diff --git a/server/server.go b/server/server.go index 50a9a834e5..4c98a4500d 100644 --- a/server/server.go +++ b/server/server.go @@ -290,7 +290,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { GithubToken: userConfig.GithubToken, GitlabUser: userConfig.GitlabUser, GitlabToken: userConfig.GitlabToken, - PlanDrafts: userConfig.PlanDrafts, + AllowDraftPRs: userConfig.PlanDrafts, BitbucketUser: userConfig.BitbucketUser, BitbucketToken: userConfig.BitbucketToken, BitbucketServerURL: userConfig.BitbucketBaseURL, diff --git a/server/user_config.go b/server/user_config.go index 958400e54b..0355a45633 100644 --- a/server/user_config.go +++ b/server/user_config.go @@ -34,7 +34,7 @@ type UserConfig struct { GitlabWebhookSecret string `mapstructure:"gitlab-webhook-secret"` HidePrevPlanComments bool `mapstructure:"hide-prev-plan-comments"` LogLevel string `mapstructure:"log-level"` - PlanDrafts bool `mapstructure:"plan-drafts"` + PlanDrafts bool `mapstructure:"allow-draft-prs"` Port int `mapstructure:"port"` RepoConfig string `mapstructure:"repo-config"` RepoConfigJSON string `mapstructure:"repo-config-json"`