Skip to content

Commit

Permalink
Add test for plan-draft behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
cket authored and lkysow committed May 25, 2020
1 parent c300dee commit ea35511
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const (
GitlabWebhookSecretFlag = "gitlab-webhook-secret" // nolint: gosec
HidePrevPlanComments = "hide-prev-plan-comments"
LogLevelFlag = "log-level"
PlanDrafts = "plan-drafts"
EnableDraftPRs = "enable-draft-prs"
PortFlag = "port"
RepoConfigFlag = "repo-config"
RepoConfigJSONFlag = "repo-config-json"
Expand Down Expand Up @@ -259,7 +259,7 @@ var boolFlags = map[string]boolFlag{
"VCS support is limited to: GitHub.",
defaultValue: false,
},
PlanDrafts: {
EnableDraftPRs: {
description: "Enable autoplan for Github Draft Pull Requests",
defaultValue: false,
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var testFlags = map[string]interface{}{
GitlabUserFlag: "gitlab-user",
GitlabWebhookSecretFlag: "gitlab-secret",
LogLevelFlag: "debug",
PlanDrafts: true,
EnableDraftPRs: true,
PortFlag: 8181,
RepoWhitelistFlag: "github.com/runatlantis/atlantis",
RequireApprovalFlag: true,
Expand Down
7 changes: 4 additions & 3 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type EventParser struct {
GithubToken string
GitlabUser string
GitlabToken string
EnableDraftPRs bool
BitbucketUser string
BitbucketToken string
BitbucketServerURL string
Expand Down Expand Up @@ -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.EnableDraftPRs {
pullEventType = models.OtherPullEvent
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var parser = events.EventParser{
GithubToken: "github-token",
GitlabUser: "gitlab-user",
GitlabToken: "gitlab-token",
EnableDraftPRs: false,
BitbucketUser: "bitbucket-user",
BitbucketToken: "bitbucket-token",
BitbucketServerURL: "http://mycorp.com:7490",
Expand Down Expand Up @@ -160,6 +161,21 @@ 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.EnableDraftPRs = true
_, evType, _, _, _, err = parser.ParseGithubPullEvent(&testEvent)
Ok(t, err)
Equals(t, models.OpenedPullEvent, evType)
}

func TestParseGithubPullEvent_EventType(t *testing.T) {
cases := []struct {
action string
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
GithubToken: userConfig.GithubToken,
GitlabUser: userConfig.GitlabUser,
GitlabToken: userConfig.GitlabToken,
PlanDrafts: userConfig.PlanDrafts,
EnableDraftPRs: userConfig.PlanDrafts,
BitbucketUser: userConfig.BitbucketUser,
BitbucketToken: userConfig.BitbucketToken,
BitbucketServerURL: userConfig.BitbucketBaseURL,
Expand Down
2 changes: 1 addition & 1 deletion server/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"enable-draft-prs"`
Port int `mapstructure:"port"`
RepoConfig string `mapstructure:"repo-config"`
RepoConfigJSON string `mapstructure:"repo-config-json"`
Expand Down

0 comments on commit ea35511

Please sign in to comment.