Is this Branch Restriction - Merge Settings API available?
Asked by Karthikraj:

Hi Team,

I used branch restriction API and created the branch restrictions and then for merge settings I couldn't find the API. Is this available ?


Answers:
author: Karthikraj
body: Hi [@Ben](/t5/user/viewprofilepage/user-id/4537226) ,

Thanks for the reply.

my requirement is, in merge settings how to define the Merge checks \& Merge Conditions through API?

Thanks \& Regards.

Karthikraj M


author: Ben
body: Hey Karthikraj!

Welcome to the Bitbucket Cloud community! :)

The merge restriction option is included within the same /2.0/repositories API endpoint that you have been using and can be passed as a JSON argument.

For example, if I was to restrict merges for the master branch to only the admin user group - the JSON argument would look like this:

```
{
"branch_match_kind":"glob",
"pattern":"master",
"kind":"restrict_merges",
"groups":[
{
"slug":"admin"
}
]
}

```

Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

comments: author: Karthikraj
body: "kind": "restrict_merges",  
"branch_match_kind": "glob",  
"pattern":"develop",  
"groups": \[  
{  
"slug": "administrators",  
"full_slug": "fii:administrators",  
"name": "Administrators",  
"default_permission": "admin",  
"account_privilege": "admin"  
}  
\]  
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: [https://api.bitbucket.org/2.0/repositories/\*\*\*\*\*/\*\*\*\*\*/branch-restrictions](https://api.bitbucket.org/2.0/repositories/fii/pipeline-utils/branch-restrictions)


author: Ben
body: Hey Karthikraj,

Just letting you know firstly that I have edited your comment to censor out your workspaceID/reposlug as this is a public forum.

May I ask if you are using AppPassword to authenticate? If you are not already doing so, please follow our guide below for configuring an AppPassword:  
<https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/>

I have been able to successfully execute the cURL command with JSON that you have listed above on my own repository, please ensure the format is correct and then attempt to execute:

```
curl -X POST \
  --url 'https://api.bitbucket.org/2.0/repositories/{workspaceID}/{reposlug}/branch-restrictions?=' \
  --user {username}:{AppPassword} \
  --header 'Content-Type: application/json' \
  --data '{
"kind": "restrict_merges",
"branch_match_kind": "glob",
"pattern":"develop",
   "groups":[
      {
        "slug":"administrators",
"full_slug": "{workspaceID}:administrators",
        "name": "Administrators",
"default_permission": "admin",
        "account_privilege": "admin"
      }
   ]
}
'
```

**NOTE:** You will need to replace {workspaceID}, {reposlug}, {username}, {AppPassword} with actual values without the curly braces

Please try this and let me know how it goes.

Cheers!

- Ben (Bitbucket Cloud Support)


author: Karthikraj
body: Hello [@Ben](/t5/user/viewprofilepage/user-id/4537226) ,

for cURL its working for me too. But, I try to use it in python its shows bad request error. How to define the "groups" in python code ?

the same JSON format should work right?

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: [https://api.bitbucket.org/2.0/repositories/\*\*\*\*\*\*/\*\*\*\*\*\*/branch-restrictions](https://api.bitbucket.org/2.0/repositories/fii-analytics/pipeline-utils/branch-restrictions)

Thanks \& Regards,

Karthikraj M


author: Ben
body: Hi Karthikraj,

To be honest - I am not very well versed in Python to give you a definitive answer as to how to format the JSON correctly.

I have found the following article on StackOverflow that addresses the 400 HTTP response error by using the `json` parameter in `requests.post` for json data that may be useful for you:  
<https://stackoverflow.com/questions/45206380/converting-curl-to-python-requests-error>  

Cheers!

- Ben (Bitbucket Cloud Support)


Tags: bitbucket-cloud

cloud