from atlassian import Bamboo
import os
BAMBOO_URL = os.environ.get('bamboo_bamboo_url', '')
ATLASSIAN_USER = os.environ.get('bamboo_build_user', '')
ATLASSIAN_PASSWORD = os.environ.get('bamboo_build_user_password', '')
countdown = os.environ.get('bamboo_countdown', '')
EXCLUDED_PROJECTS = os.environ.get('bamboo_excluded_projects', '').split(',')
def get_all_projects():
return [x['key'] for x in bamboo.projects(max_results=1000)]
def get_plans_from_project(proj):
return [x['key'] for x in bamboo.project_plans(proj)]
def get_branches_from_plan(plan_key):
return [x['id'] for x in bamboo.search_branches(plan_key, max_results=1000, start=0)]
def get_results_from_plan(plan_key):
return [x for x in bamboo.results(plan_key, expand='results.result', include_all_states=True)]
def project_build_cleanup(plans):
for plan in plans:
print("INFO: Inspecting {} plan".format(plan))
build_results = get_results_from_plan(plan)
build_results.reverse()
counter=1
for build in build_results:
build_key = build.get('buildResultKey') or None
build_number = build.get('planResultKey').get('resultNumber') or None
if build_number:
while counter < build_number:
build_key = plan + "-" + str(counter)
try:
print ("INFO: deleting build " + build_key)
bamboo.delete_build_result(build_key=build_key)
except Exception as inst:
pass
counter += 1
print ("INFO: real result detected " + plan + "-" + str(counter))
counter += 1
last_counter=counter
while counter < int(countdown):
build_key = plan + "-" + str(counter)
try:
print ("INFO: deleting build " + build_key)
bamboo.delete_build_result(build_key=build_key)
last_counter=counter
except Exception as inst:
pass
counter += 1
if __name__ == '__main__':
bamboo = Bamboo(
url=BAMBOO_URL,
username=ATLASSIAN_USER,
password=ATLASSIAN_PASSWORD,
timeout=180)
projects = get_all_projects()
for project in projects:
if project in EXCLUDED_PROJECTS:
print ("INFO: Skipping project - {}".format(project))
continue
print ("INFO: Inspecting project - {}".format(project))
results = []
all_plans_of_project = get_plans_from_project(project)
project_build_cleanup(plans=all_plans_of_project)
Code workaround requires python rest api libs.
We run this task every week.