Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bad request when adding Branch Restrictions using the Rest API

Tran Tien dung August 9, 2019

Hi everyone,

I am trying to add branch restrictions on a repository in Bitbucket Cloud using the Rest Api V2. Below is my script

read -r -d '' JSON <<'EOF'
{
{
"kind": "require_passing_builds_to_merge",
"pattern": "*",
"value": 1,
"branch_match_kind": "glob",
"groups": [
],
}
}
EOF
curl -i --request POST --data "$JSON" \
--url "https://api.bitbucket.org/2.0/repositories/aanict/rconfig/branch-restrictions/" \
--header "authorization: Bearer $ACCESS_TOKEN" --header "Content-Type: application/json"

Unfortunately, it return a error: 400 Bad request.

Any idea of what might causing this?

Thanks.

 

1 answer

1 accepted

1 vote
Answer accepted
Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 10, 2019

Hi @Tran Tien dung,

The JSON in the payload is malformed:

  • extra wrapping curly braces
  • comma after groups array

This works for me:

curl -u dpenkin:app_password -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/dpenkin/test-repo/branch-restrictions \
-d '{
"kind": "require_passing_builds_to_merge",
"pattern": "*",
"value": 1,
"branch_match_kind": "glob",
"groups": []
}'

Hope this helps.

Cheers,
Daniil

Tran Tien dung August 11, 2019

Thanks @Daniil Penkin 

It's work well. However, how I can create a new branch restriction with multi kinds?

read -r -d '' JSON <<'EOF'
[
{
"kind": "force",
"value": null,
"pattern": "*",
"branch_match_kind": "glob"
},
{
"kind": "delete",
"value": null,
"pattern": "*",
"branch_match_kind": "glob"
},
{
"kind": "require_passing_builds_to_merge",
"pattern": "*",
"value": 1,
"branch_match_kind": "glob"
},
{
"kind": "require_approvals_to_merge",
"pattern": "*",
"value": 2,
"branch_match_kind": "glob"
}
]
EOF

 I tried with this json file, and got an 500 "error": {"message": "Something went wrong", "id", please advice me how to fix this.

Thanks.

Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 11, 2019

This endpoint doesn't accept lists, it can only create one branch restriction at a time. What you have in the payload above is actually 4 branch restrictions, not one. They're shown in UI in one dialog, but each setting there is independent.

The combination of kind and match must be unique (there are
two ways to match a branch, the concrete one is configured in branch_match_kind). This means that two glob restrictions in a repository cannot have the same kind and pattern. Additionally, two branching_model restrictions in a repository cannot have the same kind and branch_type.

So in your case there should be four API calls. As for the 500, I noted it down and will fix it in one of the next releases: it should be returning 400 in this case instead.

Let me know if you have questions.

Cheers,
Daniil

Tran Tien dung August 12, 2019

Thanks for your help, I can create branch permission right now. However, It would be great if Bitbucket api docs including some example which is more visual.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events