Skip to content

Instantly share code, notes, and snippets.

@ndavison
Last active September 24, 2022 05:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndavison/55af5da7b950c1172963c6be0391a321 to your computer and use it in GitHub Desktop.
Save ndavison/55af5da7b950c1172963c6be0391a321 to your computer and use it in GitHub Desktop.
Downloads build logs from travisci for a particular project.
import requests
import json
import os
import urllib.parse
from argparse import ArgumentParser
parser = ArgumentParser(description="Downloads build logs from travisci for a particular project.")
parser.add_argument("-p", "--project", help="project to request travisci build logs for")
parser.add_argument("-r", "--repo", default=None, help="repo to request travisci build logs for")
args = parser.parse_args()
if not args.project:
print('Must supply a project value')
exit(1)
project = args.project
repo = args.repo
headers = {
'Travis-API-Version': '3'
}
s_travis = requests.session()
slugs = []
if repo:
slugs.append('{}/{}'.format(project, repo))
else:
repos_url = 'https://api.travis-ci.org/owner/{}/repos'.format(urllib.parse.quote(project, safe=''))
has_repos = True
i = 0
while has_repos:
repos_r = s_travis.get(repos_url, headers=headers, params={ 'limit': '100', 'offset': 100 * i })
repos = repos_r.json()['repositories']
if len(repos) == 0:
has_repos = False
continue
for repo in repos:
if 'slug' in repo:
slugs.append(repo['slug'])
i += 1
for slug in slugs:
outfiles = 'out/travisci/{}'.format(slug)
builds_url = 'https://api.travis-ci.org/repo/{}/builds'.format(urllib.parse.quote(slug, safe=''))
try:
os.makedirs(outfiles)
except FileExistsError as e:
pass
has_builds = True
i = 0
while has_builds:
r = s_travis.get(builds_url, headers=headers, params={ 'limit': '100', 'offset': 100 * i })
builds = r.json()['builds']
if len(builds) == 0:
has_builds = False
continue
print('Downloading build logs for {}...'.format(slug))
for build in builds:
if os.path.exists('{}/{}'.format(outfiles, build['id'])):
print('Skipping build {} ...'.format(build['id']))
else:
os.mkdir('{}/{}'.format(outfiles, build['id']))
print('Checking job logs for build {} ...'.format(build['id']))
if 'jobs' in build and len(build['jobs']) > 0:
for job in build['jobs']:
if 'id' in job:
job_log_url = 'https://api.travis-ci.org/v3/job/{}/log.txt'.format(job['id'])
print('Downloading {} ...'.format(job_log_url))
dl = s_travis.get(job_log_url)
filename = '{}/{}/job-{}'.format(outfiles, build['id'], job['id'])
content = dl.content.decode().replace('\\n', "\n")
content = content.replace('\\r', "\r")
open(filename, 'w').write(content)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment