How to use REST API in bitbucket to populate a csv file in python ?
Asked by Ali Hussaini:

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 !


Answers:
author: Joao Sgreccia
body: Hi [@Ali Hussaini](/t5/user/viewprofilepage/user-id/4092985) ,

<br />

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

<br />

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()


```

<br />

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  

comments: author: Ali Hussaini
body: Hi [@Joao Sgreccia](/t5/user/viewprofilepage/user-id/3362925) 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.


author: Joao Sgreccia
body: [@Ali Hussaini](/t5/user/viewprofilepage/user-id/4092985) ,  

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


Tags: bitbucket-cloud

cloud

rest-api