DrFaust92 / terraform-provider-bitbucket

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

bitbucket_project_branching_model: Setting named branch results in 400 #167

Closed bartje321 closed 1 month ago

bartje321 commented 1 year ago

Hi there,

Terraform Version

Terraform v1.4.6

Affected Resource(s)

Please list the resources as a list, for example:

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:

DLangenberg commented 12 months ago

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

will-sargent-dbtlabs commented 8 months ago

@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

barrywhart commented 1 month ago

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.