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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,466,091
Community Members
 
Community Events
176
Community Groups

Using python API to get branch_restrictions / pullrequests

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!

 

1 answer

1 accepted

1 vote
Answer accepted

Hello @Or Shalom ,

Welcome to Atlassian Community!

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

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 of your account
  • password: an App Password ( 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 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.

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 : 

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

Thank you, @Or Shalom .

Kind regards,

Patrik S

Thank you, it did work.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS

Atlassian Community Events