import requests
import datetime
import json
travis_token = 'your_travis_token'
def saveListToFile(list, filename):
with open(filename, 'w') as filehandle:
json.dump(list, filehandle)
def getBuildList(slug: str,buildNumber: int):
headers = {
'Authorization': 'token '+travis_token,
'User-Agent': 'Core3Client/1.0.0',
'Accept': 'application/vnd.travis-ci.2.1+json',
'Content-Type': 'application/json'
}
urlparams = {}
if buildNumber !=0:
urlparams = {
'after_number': buildNumber,
}
url = str.format('https://api.travis-ci.com/repos/{}/builds', slug)
response = requests.get(url, headers=headers,params=urlparams)
if response.status_code != 200:
print('Error', response.status_code, response.text)
return {}
else:
return response.json()
def getJob(jobid: int):
headers = {
'Authorization': 'token '+travis_token,
'User-Agent': 'Core3Client/1.0.0',
'Accept': 'application/vnd.travis-ci.2.1+json',
'Content-Type': 'application/json'
}
urlparams = {}
url = str.format('https://api.travis-ci.com/jobs/{}', jobid)
response = requests.get(url, headers=headers,params=urlparams)
if response.status_code != 200:
print('Error', response.status_code, response.text)
return {}
else:
return response.json()
def getBuildMeta(builds):
minBuildId = 0
minDateStr = ''
arraOfJobs = []
for build in builds['builds']:
if minBuildId == 0 or build['number'] < minBuildId:
minBuildId = build['number']
if build['started_at'] != None:
minDateStr = build['started_at']
arraOfJobs.extend(build['job_ids'])
minDate = datetime.datetime.strptime(minDateStr,'%Y-%m-%dT%H:%M:%SZ')
return minBuildId,minDate,arraOfJobs
def serializeAllJobsForTravis():
date1 = datetime.datetime(2020, 5, 1, 0, 0, 0)
minBuildTime = datetime.datetime.now()
minBuildNum = 0
i = 0
arrayOfJobsTotal = []
while minBuildTime > date1:
i = i + 1
builds = getBuildList('wixplosives/component-studio', minBuildNum)
minBuildNum, minBuildTime, arraOfJobs = getBuildMeta(builds)
arrayOfJobsTotal.extend(arraOfJobs)
print(i, minBuildNum, minBuildTime)
saveListToFile(arrayOfJobsTotal, 'jobs.txt')