I'm trying to use the bitbucket REST api to create a branch restriction rule on a repository according https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branch-restrictions/#api-repositories-workspace-repo-slug-branch-restrictions-post.
Here is the python code I'm using:
url = f"https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}/branch-restrictions"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}
payload = json.dumps(
{
"type": "branchrestriction",
"kind": "push",
"branch_match_kind": "branching_model",
"branch_type": "development",
"users": [ALLOWED_USER], # ALLOWED_USER = "{asdf-asdf-asdf-asdf-asdf}"
}
)
response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=(BBCLI_USERNAME, BBCLI_PASSWORD),
)
The response I get is:
500
{'error': {'id': '8f1cbdfd520e48c8b362078b9420c139',
'message': 'Something went wrong'},
'type': 'error'}
This happens for repositories that don't have any branch restrictions set up and for ones that have some but not the rule I'm trying to set up.
Listing branch permissions works fine.
What's wrong with my request?
Hey @Nick de Palézieux ,
Thank you for reaching out to Atlassian Support.
Accordingly, to the API reference for the Create branch restriction endpoint, I think the syntax you have used for defining the users might be incorrect, and thus the error you are facing.
I went ahead and did some tests in my repository and was able to successfully create a branch restriction using the following example request body :
{
"kind": "push",
"branch_match_kind": "branching_model",
"branch_type": "development",
"users": [
{
"username": "bitbucket_username_1"
},
{
"username": "bitbucket_username_2"
}
]
}
The resulting complete curl command is the below :
curl --request POST --user USERNAME:APP_PASSWORD --url https://api.bitbucket.org/2.0/repositories/<WORKSPACE>/<REPOSITORY>/branch-restrictions --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{
"kind": "push",
"branch_match_kind": "branching_model",
"branch_type": "development",
"users": [
{
"username": "bitbucket_username_1"
},
{
"username": "bitbucket_username_2"
}
]
}'
You can try using the command above and check how it goes, and then adapt it to your python script syntax.
Hope that helps! Let me know in case you run into any issues.
Thank you, @Nick de Palézieux .
Kind regards,
Patrik S
Hi @Patrik S
Thanks for your response. You're right that the problem was the way I was specifying the user. I don't find the documentation to be clear enough.
Trying your suggestion with a single user works. However, your example of specifying two users seems wrong, as you are providing a dictionary with two entries that have the same key `username`.
Should it instead be
"users": [
{ "username": "bitbucket_username_1" },
{ "username": "bitbucket_username_2" }
]
?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Nick de Palézieux ,
Happy to hear that it worked!
As for the syntax to add more than one user, you're right! I've updated my previous answer to fix the syntax, thank you for the heads-up!
Kind regards,
Patrik S
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.