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,463,664
Community Members
 
Community Events
176
Community Groups

Push and Pull Code with Bitbucket using API ways

Dear All,

I am looking for to perform pushing and pulling code with Bitbucket using APIs ways(Python or Rest) urgently.

I found out 

https://atlassian-python-api.readthedocs.io/bitbucket.html#manage-code

but here is latest news

https://community.atlassian.com/t5/Bitbucket-articles/Announcement-Bitbucket-Cloud-account-password-usage-for-Git-over/ba-p/1948231

Below is my draft Rest and Python Code

----------

import requests


auth = HTTPBasicAuth("user email", "app password")

headers = {
   "Accept": "application/json"
}

response = requests.request(
   "GET",
   url,
   headers=headers,
   auth=auth
)

print(response.text)

----------

# Get pull requests
bitbucket.get_pull_requests(project, repository, state='OPEN', order='newest', limit=100, start=0)

# Get pull request activities
bitbucket.get_pull_requests_activities(project, repository, pull_request_id)

# Get pull request changes
bitbucket.get_pull_requests_changes(project, repository, pull_request_id)

# Get pull request commits
bitbucket.get_pull_requests_commits(project, repository, pull_request_id)

# Add comment into pull request
bitbucket.add_pull_request_comment(project, repository, pull_request_id, text)

# Create a new pull request between two branches.
bitbucket.open_pull_request(source_project, source_repo, dest_project, dest_repo, source_branch, destination_branch, title, description)

# Create a new pull request between two branches with one reviewer
bitbucket.open_pull_request(source_project, source_repo, dest_project, dest_repo, source_branch, destination_branch, title, description, reviewers='name')

# Create a new pull request between two branches with multiple reviewers.
bitbucket.open_pull_request(source_project, source_repo, dest_project, dest_repo, source_branch, destination_branch, title, description, reviewers=['name1', 'name2'])

# Delete a pull request
bitbucket.delete_pull_request(project, repository, pull_request_id, pull_request_version)

# Get tags for related repo
bitbucket.get_tags(project, repository, filter='', limit=99999)

# Get project tags
# The authenticated user must have REPO_READ permission for the context repository to call this resource
bitbucket.get_project_tags(project, repository, tag_name)

# Set tag
# The authenticated user must have REPO_WRITE permission for the context repository to call this resource
bitbucket.set_tag(project, repository, tag_name, commit_revision, description=None)

# Delete tag
# The authenticated user must have REPO_WRITE permission for the context repository to call this resource
bitbucket.delete_tag(project, repository, tag_name)

# Get diff
bitbucket.get_diff(project, repository, path, hash_oldest, hash_newest)

# Get commit list from repo
bitbucket.get_commits(project, repository, hash_oldest, hash_newest, limit=99999)

# Get change log between 2 refs
bitbucket.get_changelog(project, repository, ref_from, ref_to, limit=99999)

# Get raw content of the file from repo
bitbucket.get_content_of_file(project, repository, filename, at=None, markup=None)
"""
    Retrieve the raw content for a file path at a specified revision.
    The authenticated user must have REPO_READ permission for the specified repository to call this resource.
"""

----------

I hope some one will advise me quickly and correctly with a working sample(code snippets) steps due to my rushing POC.

Really appreciate your kind help and time.

 

Best regards

1 answer

0 votes
Norbert C Atlassian Team Mar 18, 2022

Hi @Kyaw 

Thank you for contacting Atlassian Community, my name is Norbert and I'm a Bitbucket Cloud Support Engineer, it's nice to meet with you! 

I would like to inform you that unfortunately I'm not familiar with Python, but in case you wish to commit a file with REST API by following the steps here

In case you would like to get a file from a commit with REST API, you can also that by following the steps here

Please let us know how it goes.

Best Regards,
Norbert
Atlassian Bitbucket Cloud Support

Hi @Norbert C ,

 

Thank you for your advice.

I already found and experimented some of these Rest APIs.

 

But I have some security issue like HTTPError: The requested repository you do not have access. If you believe this repository you have access, make sure you're authenticated.

Since I wish to push and pull Jupyter Notebook into the Bitbucket via API ways instead of UI click.

To do the above, I may have the business logical flow below

1. Connect Bitbucket(OK already)

2. Configured App Password(OK already)

3. Setup more security to connect Repositories, Projects, Pull Requests and so on due to Bitbucket's permission(still not succeeded yet)

4. Get List of Workspaces(Ok already) , Get List of Repositories(still not succeeded yet), Get List of Projects(still not succeeded yet), Get List of Pull Requests(still not succeeded yet)

5. Get Workspace_ID(still not succeeded yet) , Get Repositories_ID(still not succeeded yet), Get Projects_ID(still not succeeded yet), Get Pull Requests_ID(still not succeeded yet)

6. Post Push and Pull Jupyter Notebook in the Repositories(still not succeeded yet)

 

Really appreciate your kind advice soon since the project is rushing with tight dead line.

Norbert C Atlassian Team Mar 21, 2022

Hi @Kyaw 

Reading this error message, I believe you might not have permission to the repository/workspace.

HTTPError: The requested repository you do not have access. If you believe this repository you have access, make sure you're authenticated.

Can you verify whether if your account has access to that specific Workspace and/or repository? 

3. Setup more security to connect Repositories, Projects, Pull Requests and so on due to Bitbucket's permission(still not succeeded yet)
- Could you please verify what do you mean by this? Basically the best way to setup the security is to create an app password, which has permissions only the necessary scopes (for example, repository:read, account:read, etc.)

Get List of Repositories(still not succeeded yet): You can get the list repositories which are owned by a specified workspace, the call is: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-get

Get List of Projects(still not succeeded yet): https://developer.atlassian.com/cloud/bitbucket/rest/api-group-projects/#api-workspaces-workspace-projects-project-key-get

Get List of Pull Requests(still not succeeded yet): https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-group-pullrequests

You can list the workspace where you have access with the following call: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-workspaces/#api-workspaces-get

You can get the repository ID with this call: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/

Projects_ID: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-projects/#api-workspaces-workspace-projects-project-key-get

Pull request ID by listing all the pull requests associated with a workspace/repo: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get

Pushing a file with API call: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-get

Best Regards,
Norbert
Atlassian Bitbucket Cloud Support

Hi @Norbert C ,

 

Thanks for your great support where all are usable and nice for me.

However, I still need to evaluate few things below with the Rest APIs

1. How to access the Bitbucket with SSO\SAAS Token ways ?

2. How to Push multiple files or folders to the Bitbucket from Local Drive?

3. How to Pull multiple files or folders from the Bitbucket to Local Drive ?

 

Really appreciate your kind help and looking forward to hearing from you soon.

Hi @Kyaw,

Please allow me to step in as Norbert is out of office. I am answering your questions below:

 

1. How to access the Bitbucket with SSO\SAAS Token ways ?

I am not sure what SSO token you are referring to?

There are two ways to authenticate when using Bitbucket Cloud API. The first one is with Basic auth, where you need to use Bitbucket username and app password:

The other one is by using OAuth (the OAuth consumer can be generated in a workspace's Settings):

 

2. How to Push multiple files or folders to the Bitbucket from Local Drive?

Below is an example API call with curl that creates a commit in the remote repo, with multiple files from your local drive. This call uses basic authentication, with username and app password.

curl -X POST -u username:app-password https://api.bitbucket.org/2.0/repositories/my-workspace/my-repo/src -F folderA/testA.txt=@folder1/test1.txt -F folderB/testB.txt=@folder2/test2.txt -F branch=master -F message="uploading via API"

where:

username replace with your Bitbucket username
app-password is an app password for your account that has at least Repositories:Write permissions
my-workspace replace with the workspace id of the workspace that owns the repo
my-repo the repository slug, you can find it in the repo URL on your browser

folder1/test1.txt is the path and file name of a file in your local drive, that you want to commit
folderA/testA.txt is the path and file name of the above file in the repo (in case you want the file to be uploaded in a different folder and with a different name than locally)

folder2/test2.txt is the path and file name of another file in your local drive, that you want to commit
folderB/testB.txt is the path and file name of the above file in the repo (in case you want the file to be uploaded in a different folder and with a different name than locally)

-F branch=master specifies the branch where you want to commit, you can set it to something other than master

-F message="uploading files via API" specifies the commit message

 

3. How to Pull multiple files or folders from the Bitbucket to Local Drive ?

There is no API endpoint to pull files and folders, however, there is an endpoint to get a file's or directory's contents at a specified revision:

If you use to get the content of a file, you can then write the output of the call in a file in your local drive.

An example call with curl is the following:

curl -u username:app-password https://api.bitbucket.org/2.0/repositories/my-workspace/my-repo/src/af83e73/file1.txt > myFile.txt

Replace username, app-password, my-workspace, and my-repo as I explained above.
Replace af83e73 with a revision in your repo
file1.txt is the name of the file in the remote repo
myFile.txt is the name of the file to create locally (it can also have the same name as the one in your repo)

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events