Bitbucket Server Api Python Code Example

Srinivas Kolaparthi
2 min readNov 11, 2022

--

Bitbucket is a Git-based source code repository hosting service owned by Atlassian.

Bitbucket Server is a combination Git server and web interface product written in Java and built with Apache Maven.

It allows users to do basic Git operations while controlling read and write access to the code. It also provides integration with other Atlassian tools.

The Bitbucket Data Center and Bitbucket Server API enable you to interact with Bitbucket programmatically.

REST APIs provide access to resources via URI paths. To use a REST API, your application will make an HTTP request and parse the response.

Your methods will be the standard HTTP methods like GET, PUT, POST, and DELETE. REST APIs operate over HTTPS making them easy to use with any programming language or framework.

Bitbucket Server uses the Atlassian REST plugin to implement the Bitbucket Server APIs. The REST plugin is bundled with Bitbucket Server.

Nowadays the majority of companies are using Bitbucket server API to generate reports to track their progress. We can use Java, Node, and python to extract the data from bitbucket.

In this post, I am going to share a sample code snippet to extract the data from bitbucket using Python libraries.

For generating Excel reports, I used Pandas in the below code snippet. Once extracted will push the data to an Excel sheet.

Code Snippet

import requests
import json
import pandas as pd
import io
url = ‘https://api.bitbucket.org/2.0/repositories/Abcd’

headers = {‘Content-Type’: ‘application/json’}
USERNAME = ‘xxxxxx’
PASSWORD = ‘yyyyyy’
response = requests.get(url, auth=(USERNAME, PASSWORD), headers=headers)
if response.status_code != 200 :
print(‘Status:’ , response.status_code, ‘Headers:’, response.headers, ‘Error Response:’ , response.json())
exit()
df = pd.read_json(io.StringIO(response.text))
# j = a.assigng().assigng()
dk = pd.json_normalize(df[‘values’])
dk.to_excel(‘Bitbucket_INC_Report.xlsx’ , sheet_name=’SLA_Report’, index=’False’)

Below are some of the Rest URLs:

List the deployments https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/deployments
List Environments https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/environments/
List pipelines https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pipelines/
List issues https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/issue
Delete an issue https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/issues/{issue_id}
List comments https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/issues/{issue_id}/comments
List commits https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/commits
Get a User https://api.bitbucket.org/2.0/users/{selected_user}

Reference:

https://developer.atlassian.com/server/bitbucket/how-tos/command-line-rest/

--

--

Srinivas Kolaparthi

I am a Trainer By Profession. I deliver DevOps, Microservices, Cloud, and Salesforce. Website: https://skolaparthi.com