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

Do not enforce the default_branch in an empty repository #158

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions gitlab/resource_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 46 additions & 3 deletions gitlab/resource_gitlab_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand Down Expand Up @@ -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),
),
},
},
})
}
Expand Down Expand Up @@ -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",
]
Expand All @@ -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 {
Expand Down