Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 25 additions & 2 deletions github/resource_github_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
return &schema.Resource{
Create: resourceGithubBranchCreate,
Read: resourceGithubBranchRead,
Update: resourceGithubBranchUpdate,
Delete: resourceGithubBranchDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubBranchImport,
Expand All @@ -31,7 +32,6 @@
"branch": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The repository branch to create.",
},
"source_branch": {
Expand Down Expand Up @@ -186,7 +186,30 @@
return nil
}

func resourceGithubBranchImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
func resourceGithubBranchUpdate(d *schema.ResourceData, meta interface{}) error {

Check failure on line 189 in github/resource_github_branch.go

View workflow job for this annotation

GitHub Actions / Continuous Integration

File is not properly formatted (gofmt)
if !d.HasChange("branch") {
return resourceGithubBranchRead(d, meta)
}

ctx := context.WithValue(context.Background(), ctxId, d.Id())
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName, oldBranchName, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
}
newBranchName := d.Get("branch").(string)

if _, _, err := client.Repositories.RenameBranch(ctx, orgName, repoName, oldBranchName, newBranchName); err != nil {
return fmt.Errorf("error renaming GitHub branch %s/%s (%s -> %s): %w", orgName, repoName, oldBranchName, newBranchName, err)
}

d.SetId(buildTwoPartID(repoName, newBranchName))

return resourceGithubBranchRead(d, meta)
}

func resourceGithubBranchImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
repoName, branchName, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return nil, err
Expand Down
58 changes: 58 additions & 0 deletions github/resource_github_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,62 @@ func TestAccGithubBranch(t *testing.T) {
testCase(t, organization)
})
})

t.Run("renames a branch without replacement", func(t *testing.T) {
initialConfig := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
resource "github_branch" "test" {
repository = github_repository.test.id
branch = "initial"
}
`, randomID)

renamedConfig := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
resource "github_branch" "test" {
repository = github_repository.test.id
branch = "renamed"
}
`, randomID)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: initialConfig,
},
{
Config: renamedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_branch.test", "branch", "renamed",
),
),
},
},
})
}

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)
})
})
}
Loading