From 79bfe9a62bae0c91954a5344bf5480d8864172c7 Mon Sep 17 00:00:00 2001 From: Dee Kryvenko <109895+dee-kryvenko@users.noreply.github.com> Date: Fri, 22 Jan 2021 14:41:42 -0800 Subject: [PATCH] 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 --- github/resource_github_branch_default.go | 30 +++++++------------ github/resource_github_branch_default_test.go | 22 +++++++------- website/docs/r/branch_default.html.markdown | 10 ++----- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/github/resource_github_branch_default.go b/github/resource_github_branch_default.go index 893209796b..279b2de766 100644 --- a/github/resource_github_branch_default.go +++ b/github/resource_github_branch_default.go @@ -4,6 +4,7 @@ import ( "context" "log" + "github.com/google/go-github/v32/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -38,14 +39,11 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{}) repoName := d.Get("repository").(string) defaultBranch := d.Get("branch").(string) - ctx := context.Background() - - repository, _, err := client.Repositories.Get(ctx, owner, repoName) - if err != nil { - return err + repository := &github.Repository{ + DefaultBranch: &defaultBranch, } - repository.DefaultBranch = &defaultBranch + ctx := context.Background() log.Printf("[DEBUG] Creating branch default: %s (%s/%s)", defaultBranch, owner, repoName) if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil { @@ -87,17 +85,14 @@ func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta interface{}) repoName := d.Id() defaultBranch := d.Get("branch").(string) - ctx := context.Background() - - repository, _, err := client.Repositories.Get(ctx, owner, repoName) - if err != nil { - return err + repository := &github.Repository{ + DefaultBranch: nil, } - repository.DefaultBranch = nil + ctx := context.Background() log.Printf("[DEBUG] Removing branch default: %s (%s/%s)", defaultBranch, owner, repoName) - _, _, err = client.Repositories.Edit(ctx, owner, repoName, repository) + _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository) return err } @@ -108,14 +103,11 @@ func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) repoName := d.Id() defaultBranch := d.Get("branch").(string) - ctx := context.Background() - - repository, _, err := client.Repositories.Get(ctx, owner, repoName) - if err != nil { - return err + repository := &github.Repository{ + DefaultBranch: &defaultBranch, } - repository.DefaultBranch = &defaultBranch + ctx := context.Background() log.Printf("[DEBUG] Updating branch default: %s (%s/%s)", defaultBranch, owner, repoName) if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil { diff --git a/github/resource_github_branch_default_test.go b/github/resource_github_branch_default_test.go index f01b67c87b..5ab1b8a3a1 100644 --- a/github/resource_github_branch_default_test.go +++ b/github/resource_github_branch_default_test.go @@ -65,18 +65,22 @@ func TestAccGithubBranchDefault(t *testing.T) { }) - t.Run("can be configured to override the default_branch of the repository", func(t *testing.T) { + t.Run("replaces the default_branch of a repository", func(t *testing.T) { config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" - default_branch = "main" auto_init = true } - - resource "github_branch_default" "test" { - repository = github_repository.test.name - branch = "override" + + resource "github_branch" "test" { + repository = github_repository.test.name + branch = "test" + } + + resource "github_branch_default" "test"{ + repository = github_repository.test.name + branch = github_branch.test.branch } `, randomID) @@ -84,11 +88,7 @@ func TestAccGithubBranchDefault(t *testing.T) { check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "github_branch_default.test", "branch", - "override", - ), - resource.TestCheckResourceAttr( - "github_branch_default.test", "repository", - fmt.Sprintf("tf-acc-test-%s", randomID), + "test", ), ) diff --git a/website/docs/r/branch_default.html.markdown b/website/docs/r/branch_default.html.markdown index cfc11623ce..bc8056ede1 100644 --- a/website/docs/r/branch_default.html.markdown +++ b/website/docs/r/branch_default.html.markdown @@ -11,19 +11,15 @@ Provides a GitHub branch default resource. This resource allows you to set the default branch for a given repository. +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. + ## Example Usage ```hcl resource "github_repository" "example" { name = "example" description = "My awesome codebase" - - visibility = "private" - - template { - owner = "github" - repository = "terraform-module-template" - } + auto_init = true } resource "github_branch" "development" {