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

How to use REST API in bitbucket to populate a csv file in python ?

Ali Hussaini September 21, 2020

Hi I am currently new in using bitbucket REST api , while there are documentations out there I am currently unable to link it with my current problem. I would like to use the REST API to be able to write a script in python that will then populate a csv file with fields such as commit id , commit author , the repository where it was commited , and the branch where the commit happened in the repository ? I followed an article called https://blog.developer.atlassian.com/bitbucket-oauth-with-python/ while this did help me but I am unable to write a single script where the OAuth happens and I am then able to parse the json into a csv file with the above listed headers . I am looking for some suggestions currently I am using requests and json to convert the output in a dictionary to be able to loop through it but I am still unable to achieve my above listed target . I really hope if anyone could guide me for the steps I should take what end point I should ideally use and how to perform the authentication through the script (the article link requires me to copy and paste ) and then be able to extract commit id , repository , branch , commit author . Thankyou !

1 answer

0 votes
Joao Sgreccia
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 22, 2020

Hi @Ali Hussaini ,


You can use app passwords to authenticate and use the rest api.
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/


For example, to list branches from repository you can use this endpoint:

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/refs/branches


response = requests.get('https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches', auth=("my-username","my-password"))

response.json()



It is also possible to get all commit information for a repository/branch using this endpoint

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commits

Don't hesitate to reach out if you have any further questions

Cheers

Ali Hussaini September 22, 2020

Hi @Joao Sgreccia  I really appreciate your quick response while this may get the commits in a repository it does not let me see the commits in all repositories at a glance (code committed to a repository and the branch associated with it ) what I have in mind is to create csv file which only contains the latest commits from any repository and also the branch name associated with that commit . I would like to generate graphs from my csv file and use it to analyse key parameters such as how frequently code is being committed to a particular branch of a particular repository and who is committing and generate graphs once this csv file is ready I would like to use it for business analytics.

Joao Sgreccia
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 23, 2020

@Ali Hussaini ,

Unfortunately, there isn't an API that returns the desired combination in one request/response.
It is necessary to use a combination of rest calls.

For example,

- Get repositories
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories

- Get the branches 
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/refs/branches

- Get commits per branch in a repository
https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commits

Cheers

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, team '25, conference, certifications, bootcamps, training experience, anaheim ca,

Want to make the most of Team ‘25?

Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.

Learn more
AUG Leaders

Upcoming Bitbucket Events