Bamboo Rest API - Export of specific information for the execution of plans in the project
Asked by Michael Kretschmer:

Hello Community, I have already tried a few things but unfortunately I can't get a result. I wanted to use a Python script to load and export certain data from Bamboo via Rest API. Authentication to Bamboo via Bearer Token. I wanted to load the data for plans under a project. I am interested in the plan name, the build number and the completed time. If possible, the status of the build. The export in csv should then only contain this data. Thank you in advance.


Answers:
author: Ayrijit Swain
body: Hi Michael,

Welcome to Atlassian community.

For your requirement where you would need Plan name, build number, completed time and build status, you may try to use below API:

```
curl -H "Authorization: Bearer <Bearer_token>" 'http://<Bamboo_URL/rest/api/latest/result/<plan_key>?expand=results.result'
```

The response will have details of all the builds for the plan.

If you need to get the details for a specific build, you can replace plan-key with build-key which will fetch only the specific build details.

Regards,

Ayrijit Swain

**\*\*please don't forget to Accept the answer if your query was answered\*\***

comments: author: Michael Kretschmer
body: i have this script and this found not the bulidcompleteddate in the Json Response.

import http.client  
import json  
import csv

def get_build_data(bamboo_url, project_key, build_keys, token):  
conn = http.client.HTTPSConnection(bamboo_url)

headers = {  
'Authorization': 'Bearer ' + token,  
'Content-Type': 'application/json'  
}

build_data = \[\]  
for build_key in build_keys:  
# F?ge den Query-Parameter 'expand' zur Anfrage hinzu  
conn.request("GET", f"/bamboo/rest/api/latest/result/{project_key}-{build_key}.json?expand=results.result.metadata", headers=headers)  
res = conn.getresponse()  
data = res.read()  
json_data = json.loads(data.decode("utf-8"))  

try:  
with open('c:/Users/\*\*\*/Documents/Code/ausgabe.txt', 'w') as f:  
print(json_data, file=f)  
except Exception as e:  
print(f"Fehler beim Schreiben der Datei 'ausgabe.txt': {e}")

if 'results' in json_data and 'result' in json_data\['results'\] and '0' in json_data\['results'\]\['result'\]and 'buildCompletedDate' in json_data\['results'\]\['result'\]\['0'\]:  
build_data.append({  
'buildKey': build_key,  
'buildCompletedDate': json_data\['results'\]\['result'\]\['0'\]\['buildCompletedDate'\]  
})  
else:  
print(f"'buildCompletedDate' nicht gefunden in den Daten f?r {build_key}")

return build_data

def write_to_csv(build_data, filename):  
if build_data:  
keys = build_data\[0\].keys()  
with open(filename, 'w', newline='') as output_file:  
dict_writer = csv.DictWriter(output_file, keys)  
dict_writer.writeheader()  
dict_writer.writerows(build_data)  
else:  
print("Keine Build-Daten zum Schreiben in die CSV-Datei")

bamboo_url = "Url zum Bamboo"  
project_key = "Key"  
build_keys = \["Key", "Key", "Key"\] # F?gen Sie Ihre Build-Keys hier ein  
token = "Bearer-Token"

build_data = get_build_data(bamboo_url, project_key, build_keys, token)  
write_to_csv(build_data, 'output.csv')


author: Ayrijit Swain
body: Hi Michael,

Can you try to use the API outside of the script with curl command to check if you are able to get the 'buildCompletedDate' data in the json response?

```
curl -H "Authorization: Bearer <Bearer_token>" 'http://<Bamboo_URL/rest/api/latest/result/<plan_key>?expand=results.result'
```

Regards,

Ayrijit Swain

**\*\*please don't forget to Accept the answer if your query was answered\*\***


author: Michael Kretschmer
body: Yes, it is included in the output. See example:

\<buildCompletedTime\>2024-04-10T09:41:55.468+02:00\</buildCompletedTime\>  
\<buildCompletedDate\>2024-04-10T09:41:55.468+02:00\</buildCompletedDate\>


Tags: csv

export

plan

project

python

rest-api

token