Skip to content

Commit ea35511

Browse files
cketlkysow
authored andcommitted
Add test for plan-draft behavior
1 parent c300dee commit ea35511

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

cmd/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const (
6464
GitlabWebhookSecretFlag = "gitlab-webhook-secret" // nolint: gosec
6565
HidePrevPlanComments = "hide-prev-plan-comments"
6666
LogLevelFlag = "log-level"
67-
PlanDrafts = "plan-drafts"
67+
EnableDraftPRs = "enable-draft-prs"
6868
PortFlag = "port"
6969
RepoConfigFlag = "repo-config"
7070
RepoConfigJSONFlag = "repo-config-json"
@@ -259,7 +259,7 @@ var boolFlags = map[string]boolFlag{
259259
"VCS support is limited to: GitHub.",
260260
defaultValue: false,
261261
},
262-
PlanDrafts: {
262+
EnableDraftPRs: {
263263
description: "Enable autoplan for Github Draft Pull Requests",
264264
defaultValue: false,
265265
},

cmd/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var testFlags = map[string]interface{}{
7575
GitlabUserFlag: "gitlab-user",
7676
GitlabWebhookSecretFlag: "gitlab-secret",
7777
LogLevelFlag: "debug",
78-
PlanDrafts: true,
78+
EnableDraftPRs: true,
7979
PortFlag: 8181,
8080
RepoWhitelistFlag: "github.com/runatlantis/atlantis",
8181
RequireApprovalFlag: true,

server/events/event_parser.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ type EventParser struct {
266266
GithubToken string
267267
GitlabUser string
268268
GitlabToken string
269+
EnableDraftPRs bool
269270
BitbucketUser string
270271
BitbucketToken string
271272
BitbucketServerURL string
@@ -418,11 +419,11 @@ func (e *EventParser) ParseGithubPullEvent(pullEvent *github.PullRequestEvent) (
418419
}
419420

420421
if pullEvent.GetPullRequest().GetDraft() {
421-
// if the PR is in draft state we do not initiate actions proactively however,
422-
// we must still clean up locks in the event of a user initiated plan
422+
// Even if the PR is in draft state users can manually run plan or may
423+
// be using the -allow-draft-prs flag. If so then we need to ensure locks are cleaned up.
423424
if pullEvent.GetAction() == "closed" {
424425
pullEventType = models.ClosedPullEvent
425-
} else {
426+
} else if !e.EnableDraftPRs {
426427
pullEventType = models.OtherPullEvent
427428
}
428429
} else {

server/events/event_parser_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var parser = events.EventParser{
3636
GithubToken: "github-token",
3737
GitlabUser: "gitlab-user",
3838
GitlabToken: "gitlab-token",
39+
EnableDraftPRs: false,
3940
BitbucketUser: "bitbucket-user",
4041
BitbucketToken: "bitbucket-token",
4142
BitbucketServerURL: "http://mycorp.com:7490",
@@ -160,6 +161,21 @@ func TestParseGithubPullEvent(t *testing.T) {
160161
Equals(t, models.User{Username: "user"}, actUser)
161162
}
162163

164+
func TestParseGithubPullEventFromDraft(t *testing.T) {
165+
// verify that draft PRs are treated as 'other' events by default
166+
testEvent := deepcopy.Copy(PullEvent).(github.PullRequestEvent)
167+
draftPR := true
168+
testEvent.PullRequest.Draft = &draftPR
169+
_, evType, _, _, _, err := parser.ParseGithubPullEvent(&testEvent)
170+
Ok(t, err)
171+
Equals(t, models.OtherPullEvent, evType)
172+
// verify that drafts are planned if requested
173+
parser.EnableDraftPRs = true
174+
_, evType, _, _, _, err = parser.ParseGithubPullEvent(&testEvent)
175+
Ok(t, err)
176+
Equals(t, models.OpenedPullEvent, evType)
177+
}
178+
163179
func TestParseGithubPullEvent_EventType(t *testing.T) {
164180
cases := []struct {
165181
action string

server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
290290
GithubToken: userConfig.GithubToken,
291291
GitlabUser: userConfig.GitlabUser,
292292
GitlabToken: userConfig.GitlabToken,
293-
PlanDrafts: userConfig.PlanDrafts,
293+
EnableDraftPRs: userConfig.PlanDrafts,
294294
BitbucketUser: userConfig.BitbucketUser,
295295
BitbucketToken: userConfig.BitbucketToken,
296296
BitbucketServerURL: userConfig.BitbucketBaseURL,

server/user_config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type UserConfig struct {
3434
GitlabWebhookSecret string `mapstructure:"gitlab-webhook-secret"`
3535
HidePrevPlanComments bool `mapstructure:"hide-prev-plan-comments"`
3636
LogLevel string `mapstructure:"log-level"`
37-
PlanDrafts bool `mapstructure:"plan-drafts"`
37+
PlanDrafts bool `mapstructure:"enable-draft-prs"`
3838
Port int `mapstructure:"port"`
3939
RepoConfig string `mapstructure:"repo-config"`
4040
RepoConfigJSON string `mapstructure:"repo-config-json"`

0 commit comments

Comments
 (0)