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

bitbucket_project_branching_model: Setting named branch results in 400

Open bartje321 opened this issue 1 year ago • 3 comments

Hi there,

Terraform Version

Terraform v1.4.6

  • provider registry.terraform.io/drfaust92/bitbucket v2.33.0

Affected Resource(s)

Please list the resources as a list, for example:

  • bitbucket_project_branching_model

Terraform Configuration Files

resource "bitbucket_project_branching_model" "model" {

  workspace = "foo"
  project   = "BAR"

  development {
    name = "new"
    use_mainbranch = false
  }
}

Panic Output

module.workspace.bitbucket_project_branching_model.model[0]: Modifying... [id=foo/BAR]
╷
│ Error: API Error: 400 2.0/workspaces/foo/projects/BAR/branching-model/settings Bad request
│ 
│   with module.workspace.bitbucket_project_branching_model.model,
│   on modules/workspace/main.tf line 15, in resource "bitbucket_project_branching_model" "model":
│   15: resource "bitbucket_project_branching_model" "model" {

Expected Behavior

The project branching model should have "new" as the development branch

Actual Behavior

400 Bad request.

Important Factoids

The following scenarios all result in the 400 error:

  • omiting use_mainbranch = false
  • creating the resource without a name and then setting the name

What does work is setting a name in Bitbucket and updating it using Terraform

  # module.workspace.bitbucket_project_branching_model.model will be updated in-place
 ~ resource "bitbucket_project_branching_model" "model" {
       id        = "foo/BAR"
       # (2 unchanged attributes hidden)

     ~ development {
         ~ name                  = "old" -> "new"
           # (3 unchanged attributes hidden)
       }

       # (4 unchanged blocks hidden)
   }

bartje321 avatar Jun 07 '23 08:06 bartje321

isnt the issue that it should be 2.0/workspaces/foo/projects/BAR/settings/branching-model instead of 2.0/workspaces/foo/projects/BAR/branching-model/settings

DLangenberg avatar Jul 07 '23 10:07 DLangenberg

@DLangenberg Interesting thought. I checked the API docs and the correct path seems to be: https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/branching-model/settings

So I think the the endpoint is right overall, but Atlassian doesn't have a sample payload to show, only a response, so something must be sideways.

Here's the payload when setting it via the browser:

{
  "branch_types": [
    {
      "enabled": true,
      "kind": "bugfix",
      "prefix": "bugfix/"
    },
    {
      "enabled": true,
      "kind": "feature",
      "prefix": "feature/"
    },
    {
      "enabled": false,
      "kind": "hotfix",
      "prefix": "hotfix/"
    },
    {
      "enabled": false,
      "kind": "release",
      "prefix": "release/"
    }
  ],
  "development": {
    "name": "dev",
    "use_mainbranch": false,
    "is_valid": true
  },
  "production": {
    "name": "main",
    "use_mainbranch": false,
    "enabled": true,
    "is_valid": true
  }
}

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branching-model/#api-repositories-workspace-repo-slug-branching-model-settings-put

will-sargent-dbtlabs avatar Oct 20 '23 16:10 will-sargent-dbtlabs

I found the issue -- the Bitbucket API requires the use_mainbranch field to be present in the request body, but the BranchModel specifies omitempty, which omits it from the JSON if it's empty (i.e. false).

type BranchModel struct {
	IsValid            bool    `json:"is_valid,omitempty"`
	Name               *string `json:"name"`
	UseMainbranch      bool    `json:"use_mainbranch,omitempty"`
	BranchDoesNotExist bool    `json:"branch_does_not_exist,omitempty"`
	Enabled            bool    `json:"enabled,omitempty"`
}

I fixed and tested this locally and confirmed that removing ,omitempty from this field fixes the issue. I'll submit a PR.

barrywhart avatar May 14 '24 13:05 barrywhart