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!
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 :
pip install atlassian-python-api
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 :
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.
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.