Skip to content
Open
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
43 changes: 21 additions & 22 deletions github/resource_github_branch_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func resourceGithubBranchDefault() *schema.Resource {
}
}

func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{}) error {

func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
Expand All @@ -60,21 +59,24 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{})

ctx := context.Background()

if rename {
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return err
}
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
return err
}
} else {
repository := &github.Repository{
DefaultBranch: &defaultBranch,
}
repository, _, err := client.Repositories.Get(ctx, owner, repoName)
if err != nil {
return err
}

if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
return err
if *repository.DefaultBranch != defaultBranch {
if rename {
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
return err
}
} else {
repository := &github.Repository{
DefaultBranch: &defaultBranch,
}

if _, _, err := client.Repositories.Edit(ctx, owner, repoName, repository); err != nil {
return err
}
}
}

Expand All @@ -83,8 +85,7 @@ func resourceGithubBranchDefaultCreate(d *schema.ResourceData, meta interface{})
return resourceGithubBranchDefaultRead(d, meta)
}

func resourceGithubBranchDefaultRead(d *schema.ResourceData, meta interface{}) error {

func resourceGithubBranchDefaultRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()
Expand Down Expand Up @@ -121,8 +122,7 @@ func resourceGithubBranchDefaultRead(d *schema.ResourceData, meta interface{}) e
return nil
}

func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta interface{}) error {

func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()
Expand All @@ -137,8 +137,7 @@ func resourceGithubBranchDefaultDelete(d *schema.ResourceData, meta interface{})
return err
}

func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta interface{}) error {

func resourceGithubBranchDefaultUpdate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()
Expand Down
63 changes: 54 additions & 9 deletions github/resource_github_branch_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
)

func TestAccGithubBranchDefault(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("creates and manages branch defaults", func(t *testing.T) {

config := fmt.Sprintf(`

resource "github_repository" "test" {
Expand Down Expand Up @@ -62,22 +60,20 @@ func TestAccGithubBranchDefault(t *testing.T) {
t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})

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"
auto_init = true
}

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
Expand Down Expand Up @@ -116,17 +112,67 @@ func TestAccGithubBranchDefault(t *testing.T) {
t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("creates and manages branch defaults even if rename is set", func(t *testing.T) {
config := fmt.Sprintf(`

resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}

resource "github_branch_default" "test" {
repository = github_repository.test.name
branch = "main"
rename = true
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch_default.test", "branch",
"main",
),
resource.TestCheckResourceAttr(
"github_branch_default.test", "repository",
fmt.Sprintf("tf-acc-test-%s", randomID),
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("replaces the default_branch of a repository without creating a branch resource prior to", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
auto_init = true
}

resource "github_branch_default" "test"{
repository = github_repository.test.name
branch = "development"
Expand Down Expand Up @@ -166,6 +212,5 @@ func TestAccGithubBranchDefault(t *testing.T) {
t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}