Skip to content

Commit 0f74e38

Browse files
authored
Fix update default branch (#719)
* 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").
1 parent 7e852db commit 0f74e38

File tree

2 files changed

+87
-22
lines changed

2 files changed

+87
-22
lines changed

github/resource_github_repository.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,12 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
467467
repoReq.Visibility = nil
468468
}
469469

470-
// Can only set `default_branch` on an already created repository with the target branches ref already in-place
471-
if v, ok := d.GetOk("default_branch"); ok {
472-
branch := v.(string)
473-
// If branch is "main", and the repository hasn't been initialized yet, setting this value will fail
474-
if branch != "main" {
475-
repoReq.DefaultBranch = &branch
476-
}
470+
// The documentation for `default_branch` states: "This can only be set
471+
// after a repository has already been created". However, for backwards
472+
// compatibility we need to allow terraform configurations that set
473+
// `default_branch` to "main" when a repository is created.
474+
if d.HasChange("default_branch") && !d.IsNewResource() {
475+
repoReq.DefaultBranch = github.String(d.Get("default_branch").(string))
477476
}
478477

479478
repoName := d.Id()

github/resource_github_repository_test.go

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ func TestAccGithubRepositories(t *testing.T) {
236236
resource "github_repository" "test" {
237237
name = "tf-acc-test-branch-%[1]s"
238238
description = "Terraform acceptance tests %[1]s"
239-
default_branch = "main"
239+
default_branch = "main"
240+
auto_init = true
241+
}
242+
243+
resource "github_branch" "default" {
244+
repository = github_repository.test.name
245+
branch = "default"
240246
}
241247
`, randomID)
242248

@@ -247,14 +253,12 @@ func TestAccGithubRepositories(t *testing.T) {
247253
"main",
248254
),
249255
),
250-
// FIXME: Deferred until https://github.com/integrations/terraform-provider-github/issues/513
251-
// > Cannot update default branch for an empty repository. Please init the repository and push first
252-
// "after": resource.ComposeTestCheckFunc(
253-
// resource.TestCheckResourceAttr(
254-
// "github_repository.test", "default_branch",
255-
// "default",
256-
// ),
257-
// ),
256+
"after": resource.ComposeTestCheckFunc(
257+
resource.TestCheckResourceAttr(
258+
"github_repository.test", "default_branch",
259+
"default",
260+
),
261+
),
258262
}
259263

260264
testCase := func(t *testing.T, mode string) {
@@ -266,12 +270,74 @@ func TestAccGithubRepositories(t *testing.T) {
266270
Config: config,
267271
Check: checks["before"],
268272
},
269-
// {
270-
// Config: strings.Replace(config,
271-
// `default_branch = "main"`,
272-
// `default_branch = "default"`, 1),
273-
// Check: checks["after"],
274-
// },
273+
// Test changing default_branch
274+
{
275+
Config: strings.Replace(config,
276+
`default_branch = "main"`,
277+
`default_branch = "default"`, 1),
278+
Check: checks["after"],
279+
},
280+
// Test changing default_branch back to main again
281+
{
282+
Config: config,
283+
Check: checks["before"],
284+
},
285+
},
286+
})
287+
}
288+
289+
t.Run("with an anonymous account", func(t *testing.T) {
290+
t.Skip("anonymous account not supported for this operation")
291+
})
292+
293+
t.Run("with an individual account", func(t *testing.T) {
294+
testCase(t, individual)
295+
})
296+
297+
t.Run("with an organization account", func(t *testing.T) {
298+
testCase(t, organization)
299+
})
300+
301+
})
302+
303+
t.Run("allows setting default_branch on an empty repository", func(t *testing.T) {
304+
305+
// Although default_branch is deprecated, for backwards compatibility
306+
// we allow setting it to "main".
307+
308+
config := fmt.Sprintf(`
309+
resource "github_repository" "test" {
310+
name = "tf-acc-test-empty-%[1]s"
311+
description = "Terraform acceptance tests %[1]s"
312+
default_branch = "main"
313+
}
314+
`, randomID)
315+
316+
check := resource.ComposeTestCheckFunc(
317+
resource.TestCheckResourceAttr(
318+
"github_repository.test", "default_branch",
319+
"main",
320+
),
321+
)
322+
323+
testCase := func(t *testing.T, mode string) {
324+
resource.Test(t, resource.TestCase{
325+
PreCheck: func() { skipUnlessMode(t, mode) },
326+
Providers: testAccProviders,
327+
Steps: []resource.TestStep{
328+
// Test creation with default_branch set
329+
{
330+
Config: config,
331+
Check: check,
332+
},
333+
// Test that changing another property does not try to set
334+
// default_branch (which would crash).
335+
{
336+
Config: strings.Replace(config,
337+
`acceptance tests`,
338+
`acceptance test`, 1),
339+
Check: check,
340+
},
275341
},
276342
})
277343
}

0 commit comments

Comments
 (0)