Using python API to get branch_restrictions / pullrequests
Asked by Or Shalom:

Hello,

I am trying to use the Python API to automate some processes in the bitbucket cloud.

I am establishing a connection via:

self.cloud = Cloud(username=self.username, password=self.password, token=self.token)

I am able to perform the following:

1.Get all the workspaces.

2. Get all the projects inside a workspace.

3. Get all the repos inside a project.

4. Get all the branches inside a repo.

5. Create new branches (based on that hash of the mainbranch read from the repo using get_data("mainbranch")\["name"\]

What I couldn't figure out yet:

1. When trying to get a list of branch_restrictions for a repo (by repo.branch_restrictions.data) I get an empty string {}

2. When trying to get a list of branch_restrictions using repo.branch_restrictions.each() I get error 403 (I am admin for the workspace, project and repo).

3. When trying to get a list of pullrequests for a repo (repo.pullrequests.data) I also get a blank string {}.

Can you share an example on how to get branch_restrictions / pullrequests for a given repo?

Thank you!


Answers:
author: Patrik S
body: Hello [@Or Shalom](/t5/user/viewprofilepage/user-id/4981995) ,

Welcome to Atlassian Community!

I'm assuming you are talking about the following python API wrapper :

* <https://atlassian-python-api.readthedocs.io/>

I went ahead and tried to build a script on my side using this wrapper, and was able to get it working by following below steps :

1. Make sure you have the latest version of the python wrapper installed :

   ```
   pip?install?atlassian-python-api
   ```

2. Use the below code as an example:

   ```
   from textwrap import indent
   from atlassian.bitbucket import Cloud

   cloud = Cloud(url="https://api.bitbucket.org/", username="bitbucket_username", password="app_password")

   w = cloud.workspaces.get("workspace_name")

   for p in w.projects.each():

       p = w.projects.get(p.key)

       print("Project key " + p.key)

       for r in p.repositories.each():

           print("   Repository name " + r.name)

           for pr in r.pullrequests.each():

               print("   PR ID: " + str(pr.id))

               print("      Title: " + str(pr.title))

               print("      Source branch: " + str(pr.source_branch))

               print("      Destination branch: " + str(pr.destination_branch))

           for br in r.branch_restrictions.each():

               print("   Branch restriction ID: " + str(br.id))

               print("      Kind: " + br.kind)

               print("      Pattern: " + br.pattern)
   ```

This example will print all the projects for a given workspace, the repositories within each project, and all the pull requests and branch permissions if any. For the script to run, you will have to provide the values for :

* username: the bitbucket username (not the e-mail address), which you can find in the [personal settings](https://bitbucket.org/account/settings/) of your account
* password: an [App Password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) ( not your account password) with required privileges. If you get 403 from the API call, I would suggest creating a new app password with full permissions checked and test how it goes.
* workspace_name: the name of your workspace.

You can use this example and adapt it to your use case. I would also recommend checking the [atlassian-python-api documentation](https://atlassian-python-api.readthedocs.io/bitbucket.html) for more examples of usage.

Also, please note that this python wrapper is developed by a third party and is not officially supported by Atlassian, so in case you find any issue, we suggest reporting it directly in [their git hub repository.](https://github.com/atlassian-api/atlassian-python-api)

Alternatively, you also have the option to call the API endpoints manually from your python script, without using a wrapper. Please find below the Bitbucket Cloud API reference documentation :

* <https://developer.atlassian.com/cloud/bitbucket/rest/>

Hope that helps! Let me know in case you have any questions.

Thank you, [@Or Shalom](/t5/user/viewprofilepage/user-id/4981995) .

Kind regards,

Patrik S

comments: author: Or Shalom
body: Thank you, it did work.


Tags: bitbucket-cloud

cloud