Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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.
Hi @Tran Tien dung,
The JSON in the payload is malformed:
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.