terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

[BUG]: `github_branch_default` resource fails on creation with `rename` set to `true`

Open stefan-hartmann-lgs opened this issue 1 year ago • 1 comments

Expected Behavior

If I have the following terraform config ...

resource "github_repository" "example" {
  name        = "example"
  auto_init   = true
}

resource "github_branch_default" "default"{
  repository = github_repository.example.name
  branch     = "main"
  rename     = true
}

... I expect the github_default_branch resource being created without issues.

Actual Behavior

The terraform plan succeeds without issues BUT on the terraform apply aka the creation of the github_default_branch resource an API call is made to rename the default branch from main to main. This API call fails with status code 422.

Doing an API call to rename main to main does not really make sense, fails and is an additional call which is not required.

A small condition only doing the rename-call if it makes sense would fix the issue aka if current_name != new_name.

See:

  • https://github.com/integrations/terraform-provider-github/blob/main/github/resource_github_branch_default.go#L57
  • https://github.com/google/go-github/blob/fd33b81221685302178ba197dbabe71326ca9051/github/repos.go#L1306
  • https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch

Terraform Version

Terraform v1.5.7 on linux_amd64

  • provider registry.terraform.io/integrations/github v5.36.0

Affected Resource(s)

  • github_branch_default

Terraform Configuration Files

No response

Steps to Reproduce

No response

Debug Output

No response

Panic Output

image

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

stefan-hartmann-lgs avatar Sep 18 '23 13:09 stefan-hartmann-lgs

Any updates on this one?

stefan-hartmann-lgs avatar Feb 28 '24 13:02 stefan-hartmann-lgs

This currently blocks you from adding the rename attribute to an existing default branch.

dreinhardt89 avatar Apr 22 '24 21:04 dreinhardt89