I am working on a VUE project where I'd like to be able to create a list of files which are in my private repo and then use that list to copy the matching files from my private repo into a zip folder for the user to download.
I have the create list and download zip folder functionality figured out so far.
However I am totally new to using APIs so I'm looking for advice on whether this is possible or not and what the best way to go about it would be.
Hi Christopher,
Do you need help with getting a list of files via API or the contents of these files? Or perhaps both?
Could you also please let us know whether this concerns repositories hosted in Bitbucket Cloud or Bitbucket Server?
Kind regards,
Theodora
Both for different purposes.
I'm not sure what the difference between cloud and server is. It's a repository on bitbucket.org.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Christopher,
Thank you for your reply. If the repository is hosted on bitbucket.org, then this is Bitbucket Cloud (Bitbucket Server is a product you install on your own servers, so this is self-hosted).
For Bitbucket Cloud, you can use this endpoint to get a list of the root directory on the main branch of the repo (at the latest commit):
An example call could be the following:
curl -sL -X GET --user username:password http://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src
where
username is the username of a Bitbucket account with access to the repo
password is this account's password or an app-password with appropriate permissions
repo-owner is the workspace ID of the workspace that owns the repo
repo-slug is the slug for this repository
If you want to get a list of the files/directories at a specific commit, you can specify the commit as follows:
curl -sL -X GET --user username:password http://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/
In order to get the contents of a specific file or a directory, you can use the following endpoint:
An example call would be the following:
curl -sL -u username:password -H "Content-Type: application/x-www-form-urlencoded" https://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/file_path
where
commit-hash replace with the hash of the specific commit you are interested in
file_path is either the path+name of a specific file, OR the path of a directory in the repo
In the call above, if file_path is the path of a directory in the repo, then the call will return the contents of this directory.
If file_path is the path+name for a specific file, you will get the contents of this file. You can then redirect the output to a file named e.g. file1 on your machine as follows:
curl -sL -u username:password -H "Content-Type: application/x-www-form-urlencoded" https://api.bitbucket.org/2.0/repositories/repo-owner/repo-slug/src/commit-hash/file_path > file1
I hope this helps, please feel free to let me know if you have any questions.
Kind regards,
Theodora
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your help.
I am looking forward to testing this myself.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are very welcome, please feel free to reach out if you have any questions!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi together,
it's possible to use the latest commit instead of the commit-hash ?
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.