DrFaust92 / terraform-provider-bitbucket

Terraform Bitbucket Cloud provider.
https://registry.terraform.io/providers/drfaust92/bitbucket
Mozilla Public License 2.0
38 stars 30 forks source link

`bitbucket_project_branching_model`: apply specific branch model to `use_mainbranch` results in panic #104

Closed Nkmol closed 1 year ago

Nkmol commented 1 year ago

Terraform Version

Terraform v1.3.5

Affected Resource(s)

Terraform Configuration Files

resource "bitbucket_project_branching_model" "project_branching_model" {
  workspace = local.workspace.name
  project   = bitbucket_project.project.key

  development {
    use_mainbranch = true
  }
  production {
    use_mainbranch = true
    enabled        = true
  }
}

Debug Output

  # bitbucket_project_branching_model.project_branching_model will be updated in-place
  ~ resource "bitbucket_project_branching_model" "project_branching_model" {
        id        = "<...>"
        # (2 unchanged attributes hidden)

      ~ development {
          - name                  = "a" -> null
          ~ use_mainbranch        = false -> true
            # (2 unchanged attributes hidden)
        }

      + production {
          + use_mainbranch = true
        }

        # (4 unchanged blocks hidden)
    }

Panic Output

│ Error: API Error: 400 <..>/branching-model/settings Bad request
│ 
│   with bitbucket_project_branching_model.project_branching_model,
│   on main.tf line 44, in resource "bitbucket_project_branching_model" "project_branching_model":
│   44: resource "bitbucket_project_branching_model" "project_branching_model" {

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Set your project branching model as follows manually through the Bitbucket UI
afbeelding
  1. Set the HCL described in this issue
  2. terraform apply
Nkmol commented 1 year ago

Bitbucket API Error The underlying issue is that the API request is done through a PUT request (as the only existing modification endpoint), where the following JSON body is created when using the above config:

{
  "development": {
    "use_mainbranch": true
  },
  "production": {
    "use_mainbranch": true,
    "enabled": true
  },
...
}

This will result in the following underlying Bitbucket API error:

{
  "type": "error",
  "error": {
    "message": "Bad request",
    "fields": {
      "development.name": "Trying to update development branch to track 'a' and main branch at the same time."
    }
  }
}

Solution The API solution is to pass along a name: null in order to reset the branch model type.

In-code I have a WIP ready (very beginner in Go :)), but making sure that the name property is null when not set in the HCL definition will fix this.