microsoft / terraform-provider-azuredevops

Terraform Azure DevOps provider
https://www.terraform.io/docs/providers/azuredevops/
MIT License
372 stars 260 forks source link

Unable to change default_branch of azuredevops_git_repository that was originally Uninitialized #1016

Closed carlosjourdan closed 2 months ago

carlosjourdan commented 3 months ago

Community Note

Terraform (and Azure DevOps Provider) Version

Terraform v1.7.5 on windows_amd64 + provider registry.terraform.io/microsoft/azuredevops v1.0.1

Affected Resource(s)

Terraform Configuration Files

#First pass creates unitialized repository
resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id = azuredevops_project.example.id
  name       = "Example Empty Git Repository"
  initialization {
    init_type = "Uninitialized"
  }

  lifecycle {
    ignore_changes = [
      # Ignore changes to initialization to support importing existing repositories
      # Given that a repo now exists, either imported into terraform state or created by terraform,
      # we don't care for the configuration of initialization against the existing resource
      initialization,
    ]
  }
}
#Second pass, after manually initializing a repository and creating a branch named "default", tries to set it as the default branch

resource "azuredevops_project" "example" {
  name               = "Example Project"
  visibility         = "private"
  version_control    = "Git"
  work_item_template = "Agile"
}

resource "azuredevops_git_repository" "example" {
  project_id = azuredevops_project.example.id
  name       = "Example Empty Git Repository"
  initialization {
    init_type = "Uninitialized"
  }

  default_branch = "refs/heads/default"

  lifecycle {
    ignore_changes = [
      # Ignore changes to initialization to support importing existing repositories
      # Given that a repo now exists, either imported into terraform state or created by terraform,
      # we don't care for the configuration of initialization against the existing resource
      initialization,
    ]
  }
}

Expected Behavior

The repository should have set the "default_branch" property to "ref/heads/default" after the second apply

Actual Behavior

Error: Error converting terraform data model to AzDO project reference: Repository 'initialization.init_type = Uninitialized', there will be no branches, 'default_branch' cannt not be set.

Steps to Reproduce

  1. terraform apply without the default_branch property, with init_type = "Uninitialized"
  2. manually initialize the repo and create a branch named 'default'
  3. terraform apply after adding the default_branch property
xuzhang3 commented 3 months ago

@carlosjourdan this is a tech debt that the initialization of the repo is hard coded in azuredevops_git_repository. This constraint is used to prevent the default branch from being set when the repo is not initialized. Can you initialize the repository with a different initial type?

carlosjourdan commented 3 months ago

No. This repo is in a production environment where all the access is done through terraform, we have no access as project owners. If I try to change the initialization type through the code, terraform plan says that the resource needs to be replaced, which is unacceptable because it would delete the entire git history.

On Wed, Apr 3, 2024, 05:46 xuzhang3 @.***> wrote:

@carlosjourdan https://github.com/carlosjourdan this is a tech debt that the initialization of the repo is hard coded in azuredevops_git_repository. This constraint is used to prevent the default branch from being set when the repo is not initialized. Can you initialize the repository with a different initial type?

— Reply to this email directly, view it on GitHub https://github.com/microsoft/terraform-provider-azuredevops/issues/1016#issuecomment-2033940257, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEF5Z5EGJTRGJCDIDH52QLLY3O6UVAVCNFSM6AAAAABFUFXTF2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZTHE2DAMRVG4 . You are receiving this because you were mentioned.Message ID: @.*** com>

xuzhang3 commented 2 months ago

@carlosjourdan I see, the second apply should not block update, this is different from the create.