Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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