Skip to content

Commit 9a39905

Browse files
rhughes1kfcampbell
andauthored
Adding new argument to the branch_default resource to allow renaming the default branch on the repository without the need of first calling the github_branch resource (#1523)
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
1 parent fdb2e0c commit 9a39905

File tree

3 files changed

+108
-12
lines changed

3 files changed

+108
-12
lines changed

github/resource_github_branch_default.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func resourceGithubBranchDefault() *schema.Resource {
2727
Required: true,
2828
ForceNew: true,
2929
},
30+
"rename": {
31+
Type: schema.TypeBool,
32+
Optional: true,
33+
Default: false,
34+
},
3035
},
3136
}
3237
}
@@ -37,15 +42,26 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{})
3742
owner := meta.(*Owner).name
3843
repoName := d.Get("repository").(string)
3944
defaultBranch := d.Get("branch").(string)
40-
41-
repository := &github.Repository{
42-
DefaultBranch: &defaultBranch,
43-
}
45+
rename := d.Get("rename").(bool)
4446

4547
ctx := context.Background()
4648

47-
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
48-
return err
49+
if rename {
50+
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
51+
if err != nil {
52+
return err
53+
}
54+
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
55+
return err
56+
}
57+
} else {
58+
repository := &github.Repository{
59+
DefaultBranch: &defaultBranch,
60+
}
61+
62+
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
63+
return err
64+
}
4965
}
5066

5167
d.SetId(repoName)
@@ -98,15 +114,26 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{})
98114
owner := meta.(*Owner).name
99115
repoName := d.Id()
100116
defaultBranch := d.Get("branch").(string)
101-
102-
repository := &github.Repository{
103-
DefaultBranch: &defaultBranch,
104-
}
117+
rename := d.Get("rename").(bool)
105118

106119
ctx := context.Background()
107120

108-
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
109-
return err
121+
if rename {
122+
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
123+
if err != nil {
124+
return err
125+
}
126+
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
127+
return err
128+
}
129+
} else {
130+
repository := &github.Repository{
131+
DefaultBranch: &defaultBranch,
132+
}
133+
134+
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
135+
return err
136+
}
110137
}
111138

112139
return resourceGithubBranchDefaultRead(d, meta)

github/resource_github_branch_default_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,54 @@ func TestAccGithubBranchDefault(t *testing.T) {
118118
})
119119

120120
})
121+
122+
t.Run("replaces the default_branch of a repository without creating a branch resource prior to", func(t *testing.T) {
123+
124+
config := fmt.Sprintf(`
125+
resource "github_repository" "test" {
126+
name = "tf-acc-test-%s"
127+
auto_init = true
128+
}
129+
130+
resource "github_branch_default" "test"{
131+
repository = github_repository.test.name
132+
branch = "development"
133+
rename = true
134+
}
135+
136+
`, randomID)
137+
138+
check := resource.ComposeTestCheckFunc(
139+
resource.TestCheckResourceAttr(
140+
"github_branch_default.test", "branch",
141+
"development",
142+
),
143+
)
144+
145+
testCase := func(t *testing.T, mode string) {
146+
resource.Test(t, resource.TestCase{
147+
PreCheck: func() { skipUnlessMode(t, mode) },
148+
Providers: testAccProviders,
149+
Steps: []resource.TestStep{
150+
{
151+
Config: config,
152+
Check: check,
153+
},
154+
},
155+
})
156+
}
157+
158+
t.Run("with an anonymous account", func(t *testing.T) {
159+
t.Skip("anonymous account not supported for this operation")
160+
})
161+
162+
t.Run("with an individual account", func(t *testing.T) {
163+
testCase(t, individual)
164+
})
165+
166+
t.Run("with an organization account", func(t *testing.T) {
167+
testCase(t, organization)
168+
})
169+
170+
})
121171
}

website/docs/r/branch_default.html.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Note that use of this resource is incompatible with the `default_branch` option
1515

1616
## Example Usage
1717

18+
Basic usage:
19+
1820
```hcl
1921
resource "github_repository" "example" {
2022
name = "example"
@@ -33,12 +35,29 @@ resource "github_branch_default" "default"{
3335
}
3436
```
3537

38+
Renaming to a branch that doesn't exist:
39+
40+
```hcl
41+
resource "github_repository" "example" {
42+
name = "example"
43+
description = "My awesome codebase"
44+
auto_init = true
45+
}
46+
47+
resource "github_branch_default" "default"{
48+
repository = github_repository.example.name
49+
branch = "development"
50+
rename = true
51+
}
52+
```
53+
3654
## Argument Reference
3755

3856
The following arguments are supported:
3957

4058
* `repository` - (Required) The GitHub repository
4159
* `branch` - (Required) The branch (e.g. `main`)
60+
* `rename` - (Optional) Indicate if it should rename the branch rather than use an existing branch. Defaults to `false`.
4261

4362
## Import
4463

0 commit comments

Comments
 (0)