diff --git a/github/resource_github_branch_default.go b/github/resource_github_branch_default.go index 27781d275d..790aa23c8e 100644 --- a/github/resource_github_branch_default.go +++ b/github/resource_github_branch_default.go @@ -60,8 +60,11 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{}) if err != nil { return err } - if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil { - return err + + if defaultBranch != *repository.DefaultBranch { + if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil { + return err + } } } else { repository := &github.Repository{ @@ -147,8 +150,11 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) if err != nil { return err } - if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil { - return err + + if defaultBranch != *repository.DefaultBranch { + if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil { + return err + } } } else { repository := &github.Repository{ diff --git a/github/resource_github_branch_default_test.go b/github/resource_github_branch_default_test.go index 65c83e1bda..6477200075 100644 --- a/github/resource_github_branch_default_test.go +++ b/github/resource_github_branch_default_test.go @@ -168,4 +168,126 @@ func TestAccGithubBranchDefault(t *testing.T) { }) }) + + t.Run("replaces the default_branch of a repository without creating a branch resource prior to", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_branch_default" "test"{ + repository = github_repository.test.name + branch = "development" + rename = true + } + + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_branch_default.test", "branch", + "development", + ), + ) + + testCase := func(t *testing.T, mode string) { + + ts := githubApiMock([]*mockResponse{}) + defer ts.Close() + + d := resourceGithubBranchDefault().Data(nil) + meta := testAccProvider.Meta() + err := resourceGithubBranchDefaultUpdate(d, meta) + + if err != nil { + t.Logf("Unexpected error: %s", err) + t.Fail() + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) + + t.Run("uses the rename functionality of the default_branch where the default_branch is already set as expected", func(t *testing.T) { + + initial_config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_branch_default" "test"{ + repository = github_repository.test.name + branch = "main" + rename = false + } + + `, randomID) + + updated_config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_branch_default" "test"{ + repository = github_repository.test.name + branch = "main" + rename = true + } + + `, randomID) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: initial_config, + }, + { + Config: updated_config, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) }