Skip to content

Commit

Permalink
Fix update default branch (integrations#719)
Browse files Browse the repository at this point in the history
* Test changing default_branch from/to main

* 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").
  • Loading branch information
tibbes authored Mar 16, 2021
1 parent 94738b8 commit 1df259c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
13 changes: 6 additions & 7 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
96 changes: 81 additions & 15 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand All @@ -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,
},
},
})
}
Expand Down

0 comments on commit 1df259c

Please sign in to comment.