From c1d2dd78231eabad0887e184da5a6e8b9e6624a4 Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Tue, 15 Oct 2024 16:31:16 +1100 Subject: [PATCH 1/4] Fixed renaming if the branch names are identical --- github/resource_github_branch_default.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/github/resource_github_branch_default.go b/github/resource_github_branch_default.go index 27781d275d..9d4617e437 100644 --- a/github/resource_github_branch_default.go +++ b/github/resource_github_branch_default.go @@ -147,6 +147,12 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) if err != nil { return err } + + // We don't want to rename branch if its already the default branch + if defaultBranch == *repository.DefaultBranch { + return nil + } + if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil { return err } From 3c3e6682b814944365499cc0e3f47bdee81bc6ff Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Tue, 15 Oct 2024 16:41:27 +1100 Subject: [PATCH 2/4] Fixed logic --- github/resource_github_branch_default.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/github/resource_github_branch_default.go b/github/resource_github_branch_default.go index 9d4617e437..661ae49f42 100644 --- a/github/resource_github_branch_default.go +++ b/github/resource_github_branch_default.go @@ -148,13 +148,10 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) return err } - // We don't want to rename branch if its already the default branch - if defaultBranch == *repository.DefaultBranch { - return nil - } - - 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{ From cdab9067c39269e6ab9ba340422247b6630e70dd Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Tue, 15 Oct 2024 16:57:01 +1100 Subject: [PATCH 3/4] Added check to Create too --- github/resource_github_branch_default.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/github/resource_github_branch_default.go b/github/resource_github_branch_default.go index 661ae49f42..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{ From 90f3c3ef12115646cb2e530007089f66b345088c Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Tue, 3 Dec 2024 19:15:01 +1100 Subject: [PATCH 4/4] Added Integration test to prove functionality Co-authored-by: JoshWilsonWTG <143045916+JoshWilsonWTG@users.noreply.github.com> --- github/resource_github_branch_default_test.go | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) 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) + }) + + }) }