Skip to content

Commit

Permalink
github_branch_default: send only fields that changed. Fixes integrati…
Browse files Browse the repository at this point in the history
…ons#625 integrations#620. (integrations#666)

* github_branch_default: send only fields that changed. Fixes integrations#625 integrations#620.

* fix failing test and update docs

Co-authored-by: Jeremy Udit <jcudit@github.com>
  • Loading branch information
dee-kryvenko and Jeremy Udit authored Jan 22, 2021
1 parent f3dc32a commit 79bfe9a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
30 changes: 11 additions & 19 deletions github/resource_github_branch_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

"github.com/google/go-github/v32/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions github/resource_github_branch_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,30 @@ 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)

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",
),
)

Expand Down
10 changes: 3 additions & 7 deletions website/docs/r/branch_default.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 79bfe9a

Please sign in to comment.