Skip to content

Instantly share code, notes, and snippets.

@randomsync
Created March 8, 2019 20:20
Show Gist options
  • Save randomsync/98c19c36daed9f338a6dab609a5cb848 to your computer and use it in GitHub Desktop.
Save randomsync/98c19c36daed9f338a6dab609a5cb848 to your computer and use it in GitHub Desktop.
BAMBOO_URL = "https://<bamboo host>/rest/api/latest"
DASHBOARD_URL = BAMBOO_URL + "/deploy/dashboard"
RESULTS_URL = BAMBOO_URL + "/deploy/environment/{}/results"
def get_weeks_since_friday_deploy(user, passwd):
weeks_since_friday_deploy = sys.maxint
r = requests.get(DASHBOARD_URL, auth=(user, passwd))
r.raise_for_status()
prod_env_ids = {}
# first get all production environments' ids which will
# be used to get deployments
for project in r.json():
name = project['deploymentProject']['name']
envs = project['environmentStatuses']
for env in envs:
if env['environment']['name'] == 'Production':
if 'deploymentResult' not in env:
continue
else:
prod_env_ids[name] = env['environment']['id']
for key, value in prod_env_ids.iteritems():
# no need to continue if it's already 0
if weeks_since_friday_deploy == 0:
break
r = requests.get(RESULTS_URL.format(value), auth=(user, passwd))
r.raise_for_status()
deployments = r.json()['results']
for dep in deployments:
start = datetime.datetime.fromtimestamp(dep['startedDate'] / 1e3)
end = datetime.datetime.fromtimestamp(dep['finishedDate'] / 1e3)
if start.isoweekday() == 5 or end.isoweekday() == 5:
weeks = (datetime.datetime.now() - start).days / 7
if weeks < weeks_since_friday_deploy:
weeks_since_friday_deploy = weeks
weeks = (datetime.datetime.now() - end).days / 7
if weeks < weeks_since_friday_deploy:
weeks_since_friday_deploy = weeks
# it is possible to get a negative number in an edge case,
# if the script runs very close to the time of deployment
if weeks_since_friday_deploy < 0:
weeks_since_friday_deploy = 0
break
# once it's 0, no need to continue further
if weeks_since_friday_deploy == 0:
break
return weeks_since_friday_deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment