From 5993d837322a7de0cc6e87a497cebeea0fff256d Mon Sep 17 00:00:00 2001 From: Julian Tibble Date: Thu, 4 Mar 2021 13:51:10 +0000 Subject: [PATCH 1/2] Test changing default_branch from/to main --- github/resource_github_repository_test.go | 96 +++++++++++++++++++---- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 0deac806a6..26348f2495 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -236,7 +236,13 @@ func TestAccGithubRepositories(t *testing.T) { resource "github_repository" "test" { name = "tf-acc-test-branch-%[1]s" description = "Terraform acceptance tests %[1]s" - default_branch = "main" + default_branch = "main" + auto_init = true + } + + resource "github_branch" "default" { + repository = github_repository.test.name + branch = "default" } `, randomID) @@ -247,14 +253,12 @@ func TestAccGithubRepositories(t *testing.T) { "main", ), ), - // FIXME: Deferred until https://github.com/integrations/terraform-provider-github/issues/513 - // > Cannot update default branch for an empty repository. Please init the repository and push first - // "after": resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr( - // "github_repository.test", "default_branch", - // "default", - // ), - // ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "default_branch", + "default", + ), + ), } testCase := func(t *testing.T, mode string) { @@ -266,12 +270,74 @@ func TestAccGithubRepositories(t *testing.T) { Config: config, Check: checks["before"], }, - // { - // Config: strings.Replace(config, - // `default_branch = "main"`, - // `default_branch = "default"`, 1), - // Check: checks["after"], - // }, + // Test changing default_branch + { + Config: strings.Replace(config, + `default_branch = "main"`, + `default_branch = "default"`, 1), + Check: checks["after"], + }, + // Test changing default_branch back to main again + { + Config: config, + Check: checks["before"], + }, + }, + }) + } + + 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("allows setting default_branch on an empty repository", func(t *testing.T) { + + // Although default_branch is deprecated, for backwards compatibility + // we allow setting it to "main". + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-empty-%[1]s" + description = "Terraform acceptance tests %[1]s" + default_branch = "main" + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "default_branch", + "main", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + // Test creation with default_branch set + { + Config: config, + Check: check, + }, + // Test that changing another property does not try to set + // default_branch (which would crash). + { + Config: strings.Replace(config, + `acceptance tests`, + `acceptance test`, 1), + Check: check, + }, }, }) } From cfb41618b27d01311d47e992557569e7fdb70cb1 Mon Sep 17 00:00:00 2001 From: Julian Tibble Date: Thu, 4 Mar 2021 22:33:47 +0000 Subject: [PATCH 2/2] Fix updating default_branch on github_repository Special-casing "main" in the previous version of this code meant that it was impossible to change the default branch to "main" from something else (e.g. "master"). --- github/resource_github_repository.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 42d851943d..682220d6f1 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -467,13 +467,12 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er repoReq.Visibility = nil } - // Can only set `default_branch` on an already created repository with the target branches ref already in-place - if v, ok := d.GetOk("default_branch"); ok { - branch := v.(string) - // If branch is "main", and the repository hasn't been initialized yet, setting this value will fail - if branch != "main" { - repoReq.DefaultBranch = &branch - } + // The documentation for `default_branch` states: "This can only be set + // after a repository has already been created". However, for backwards + // compatibility we need to allow terraform configurations that set + // `default_branch` to "main" when a repository is created. + if d.HasChange("default_branch") && !d.IsNewResource() { + repoReq.DefaultBranch = github.String(d.Get("default_branch").(string)) } repoName := d.Id()