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

Fix update default branch #719

Merged
merged 2 commits into from
Mar 16, 2021
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: 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