Skip to content

Instantly share code, notes, and snippets.

@ndavison
Last active June 26, 2019 07:23
Show Gist options
  • Save ndavison/159fce60a186e6d57b77d725d808a279 to your computer and use it in GitHub Desktop.
Save ndavison/159fce60a186e6d57b77d725d808a279 to your computer and use it in GitHub Desktop.
A script to find the CircleCI build associated with a particular VCS tag value
import requests
import json
import os
from argparse import ArgumentParser
parser = ArgumentParser(description="Queries the circleci API for the build associated with a VCS tag.")
parser.add_argument("-p", "--project", help="project to request circleci build logs for")
parser.add_argument("-r", "--repo", help="repo to request circleci build logs for")
parser.add_argument("-t", "--tag", help="the VCS tag value to look for")
parser.add_argument("-a", "--authtoken", default=None, help="API token for non public readable builds")
args = parser.parse_args()
if not args.repo:
print('Must supply a repo value')
exit(1)
if not args.project:
print('Must supply a project value')
exit(1)
if not args.tag:
print('Must supply a tag value')
exit(1)
project = args.project
repo = args.repo
token = args.authtoken
tag = args.tag
url = 'https://circleci.com/api/v1.1/project/github/{}/{}'.format(project, repo)
params = {}
if token:
params['circle-token'] = token
r = requests.get(url, params=params)
if not r.status_code == 200:
print('API request failed with code: {}'.format(r.status_code))
exit(1)
builds = r.json()
latest = builds[0]['build_num']
i = latest
found = False
print('Checking {}...'.format(url))
while i > 0 and not found:
r_2 = requests.get('{}/{}'.format(url, i), params=params)
build_details = r_2.json()
if 'vcs_tag' in build_details:
if build_details['vcs_tag'] == tag:
found = True
print('Found tag: {}'.format(build_details["build_url"]))
i -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment