diff --git a/gitlab/resource_gitlab_project.go b/gitlab/resource_gitlab_project.go index e7418864b..104875de5 100644 --- a/gitlab/resource_gitlab_project.go +++ b/gitlab/resource_gitlab_project.go @@ -39,6 +39,19 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{ "default_branch": { Type: schema.TypeString, Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + // If the old default branch is empty, it means that the project does not + // have a default branch. This can only happen if the project does not have + // branches, i.e. it is an empty project. In that case it is useless to + // try setting a specific default branch (because no branch exists). + // This code will defer the setting of a default branch to a time when the + // project is no longer empty. + if old == "" { + return true + } + + return old == new + }, }, "issues_enabled": { Type: schema.TypeBool, diff --git a/gitlab/resource_gitlab_project_test.go b/gitlab/resource_gitlab_project_test.go index 9f7bfd30f..9c080857d 100644 --- a/gitlab/resource_gitlab_project_test.go +++ b/gitlab/resource_gitlab_project_test.go @@ -12,7 +12,7 @@ import ( ) func TestAccGitlabProject_basic(t *testing.T) { - var received, defaults gitlab.Project + var received, defaults, defaultsMasterBranch gitlab.Project rInt := acctest.RandInt() defaults = gitlab.Project{ @@ -35,6 +35,9 @@ func TestAccGitlabProject_basic(t *testing.T) { Archived: false, // needless, but let's make this explicit } + defaultsMasterBranch = defaults + defaultsMasterBranch.DefaultBranch = "master" + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -150,6 +153,16 @@ func TestAccGitlabProject_basic(t *testing.T) { testAccCheckAggregateGitlabProject(&defaults, &received), ), }, + // Step6 Update the project creating the default branch + { + // Get the ID from the project data at the previous step + SkipFunc: testAccGitlabProjectConfigDefaultBranchSkipFunc(&received, "master"), + Config: testAccGitlabProjectConfigDefaultBranch(rInt, "master"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGitlabProjectExists("gitlab_project.foo", &received), + testAccCheckAggregateGitlabProject(&defaultsMasterBranch, &received), + ), + }, }, }) } @@ -367,13 +380,21 @@ resource "gitlab_project" "foo" { `, rInt, rInt, rInt) } -func testAccGitlabProjectConfig(rInt int) string { +func testAccGitlabProjectConfigDefaultBranch(rInt int, defaultBranch string) string { + defaultBranchStatement := "" + + if len(defaultBranch) > 0 { + defaultBranchStatement = fmt.Sprintf("default_branch = \"%s\"", defaultBranch) + } + return fmt.Sprintf(` resource "gitlab_project" "foo" { name = "foo-%d" path = "foo.%d" description = "Terraform acceptance tests" + %s + tags = [ "tag1", ] @@ -385,7 +406,29 @@ resource "gitlab_project" "foo" { only_allow_merge_if_pipeline_succeeds = true only_allow_merge_if_all_discussions_are_resolved = true } - `, rInt, rInt) + `, rInt, rInt, defaultBranchStatement) +} + +func testAccGitlabProjectConfigDefaultBranchSkipFunc(project *gitlab.Project, defaultBranch string) func() (bool, error) { + return func() (bool, error) { + conn := testAccProvider.Meta().(*gitlab.Client) + + commitMessage := "Initial Commit" + + options := &gitlab.CreateCommitOptions{ + Branch: &defaultBranch, + CommitMessage: &commitMessage, + Actions: []*gitlab.CommitAction{}, + } + + _, _, err := conn.Commits.CreateCommit(project.ID, options) + + return false, err + } +} + +func testAccGitlabProjectConfig(rInt int) string { + return testAccGitlabProjectConfigDefaultBranch(rInt, "") } func testAccGitlabProjectUpdateConfig(rInt int) string {