Skip to content

[BUG]: github_branch_default attempts to rename to the same name on import #2613

@randy-coburn-zeam

Description

@randy-coburn-zeam

Expected Behavior

When you create a repo and assign the default branch, the code should check to see if it needs to do work.
If the default branch supplied is the same as the current branch, then nothing needs to be done.

This is visible when importing a repo settings.

Actual Behavior

I have a repo called x, x repo has a branch called main that is the primary branch. I created x repo outside of terraform.

I import the repo and assign the default branch using a github_branch_default resource which is also imported.

The import of the repo is done as expected, but the github_branch_default is imported and the rename config is set to additive and true. This then triggers a rename to happen, which then fails because the source and destination name are the same.

This is even more difficult to deal with since I happen to use the github_branch_default within a module that manages the repos for me.

Terraform Version

Terraform 1.11.3
Provider version: 6.6.0

Affected Resource(s)

  • github_branch_default

Terraform Configuration Files

import {
  to = module.generic_repos["junk-1"].github_repo.current
  id = "randy-junk-1"
}

import {
  to = module.generic_repos["junk-1"].github_branch_default.main
  id = "randy-junk-1"
}

# -- Within the module 'generic_repos' ---

variable "name" {
  description = "Repo name"
  type = string
}

variable "description" {
  description = "Repo description"
  type = string
}

variable "visibility" {
  description = "Repo visibility"
  type = string
  default = "internal"
}
variable "primary_branch" {
  description = "Default branch"
  type = string
  default = "main"
}

resource github_repository "current" {
  name        = var.name
  description = var.description
  visibility  = var.visibility
  auto_init   = true
  # Failsafe to prevent accidental deletion of the repo.
  archive_on_destroy = true
  
  vulnerability_alerts = true
}

resource "github_branch_default" "main" {
  repository = resource.github_repository.current.name
  branch     = var.primary_branch
  rename     = true
}

Steps to Reproduce

Create a repo outside of terraform.
Set anything you want as the primary branch.

Create a repo using code above, making sure to set the primary branch the same as default branch in your repo. Before you apply do the imports like below.

Import both the repo and the default branch like in the code snip it.

When the apply happens you'll see that it tries to rename the primary branch to the same name that you imported.

Debug Output

Panic Output

Code of Conduct

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

Metadata

Metadata

Assignees

Labels

Type: BugSomething isn't working as documented

Type

No type

Projects

Status

🔥 Backlog

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions