Skip to content

Commit b61a71f

Browse files
dee-kryvenkoJeremy Udit
and
Jeremy Udit
authored
github_branch_default: send only fields that changed. Fixes #625 #620. (#666)
* github_branch_default: send only fields that changed. Fixes #625 #620. * fix failing test and update docs Co-authored-by: Jeremy Udit <[email protected]>
1 parent 3a9dbb0 commit b61a71f

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

github/resource_github_branch_default.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66

7+
"github.com/google/go-github/v32/github"
78
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
89
)
910

@@ -38,14 +39,11 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{})
3839
repoName := d.Get("repository").(string)
3940
defaultBranch := d.Get("branch").(string)
4041

41-
ctx := context.Background()
42-
43-
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
44-
if err != nil {
45-
return err
42+
repository := &github.Repository{
43+
DefaultBranch: &defaultBranch,
4644
}
4745

48-
repository.DefaultBranch = &defaultBranch
46+
ctx := context.Background()
4947

5048
log.Printf("[DEBUG] Creating branch default: %s (%s/%s)", defaultBranch, owner, repoName)
5149
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
@@ -87,17 +85,14 @@ func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta interface{})
8785
repoName := d.Id()
8886
defaultBranch := d.Get("branch").(string)
8987

90-
ctx := context.Background()
91-
92-
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
93-
if err != nil {
94-
return err
88+
repository := &github.Repository{
89+
DefaultBranch: nil,
9590
}
9691

97-
repository.DefaultBranch = nil
92+
ctx := context.Background()
9893

9994
log.Printf("[DEBUG] Removing branch default: %s (%s/%s)", defaultBranch, owner, repoName)
100-
_, _, err = client.Repositories.Edit(ctx, owner, repoName, repository)
95+
_, _, err := client.Repositories.Edit(ctx, owner, repoName, repository)
10196
return err
10297
}
10398

@@ -108,14 +103,11 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{})
108103
repoName := d.Id()
109104
defaultBranch := d.Get("branch").(string)
110105

111-
ctx := context.Background()
112-
113-
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
114-
if err != nil {
115-
return err
106+
repository := &github.Repository{
107+
DefaultBranch: &defaultBranch,
116108
}
117109

118-
repository.DefaultBranch = &defaultBranch
110+
ctx := context.Background()
119111

120112
log.Printf("[DEBUG] Updating branch default: %s (%s/%s)", defaultBranch, owner, repoName)
121113
if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {

github/resource_github_branch_default_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,30 @@ func TestAccGithubBranchDefault(t *testing.T) {
6565

6666
})
6767

68-
t.Run("can be configured to override the default_branch of the repository", func(t *testing.T) {
68+
t.Run("replaces the default_branch of a repository", func(t *testing.T) {
6969

7070
config := fmt.Sprintf(`
7171
resource "github_repository" "test" {
7272
name = "tf-acc-test-%s"
73-
default_branch = "main"
7473
auto_init = true
7574
}
76-
77-
resource "github_branch_default" "test" {
78-
repository = github_repository.test.name
79-
branch = "override"
75+
76+
resource "github_branch" "test" {
77+
repository = github_repository.test.name
78+
branch = "test"
79+
}
80+
81+
resource "github_branch_default" "test"{
82+
repository = github_repository.test.name
83+
branch = github_branch.test.branch
8084
}
8185
8286
`, randomID)
8387

8488
check := resource.ComposeTestCheckFunc(
8589
resource.TestCheckResourceAttr(
8690
"github_branch_default.test", "branch",
87-
"override",
88-
),
89-
resource.TestCheckResourceAttr(
90-
"github_branch_default.test", "repository",
91-
fmt.Sprintf("tf-acc-test-%s", randomID),
91+
"test",
9292
),
9393
)
9494

website/docs/r/branch_default.html.markdown

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@ Provides a GitHub branch default resource.
1111

1212
This resource allows you to set the default branch for a given repository.
1313

14+
Note that use of this resource is incompatible with the `default_branch` option of the `github_repository` resource. Using both will result in plans always showing a diff.
15+
1416
## Example Usage
1517

1618
```hcl
1719
resource "github_repository" "example" {
1820
name = "example"
1921
description = "My awesome codebase"
20-
21-
visibility = "private"
22-
23-
template {
24-
owner = "github"
25-
repository = "terraform-module-template"
26-
}
22+
auto_init = true
2723
}
2824
2925
resource "github_branch" "development" {

0 commit comments

Comments
 (0)