Skip to content

Commit

Permalink
Merge pull request dmakhno#1 from dlabs/master
Browse files Browse the repository at this point in the history
merge from dlabs
  • Loading branch information
ssbarnea committed Jan 12, 2015
2 parents b7172bc + 5e2960c commit 4b709cb
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions travis_after_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import time
import logging
import requests

try:
import urllib.request as urllib2
Expand All @@ -15,9 +16,11 @@
TRAVIS_JOB_NUMBER = 'TRAVIS_JOB_NUMBER'
TRAVIS_BUILD_ID = 'TRAVIS_BUILD_ID'
POLLING_INTERVAL = 'LEADER_POLLING_INTERVAL'
GITHUB_TOKEN = 'GITHUB_TOKEN'

build_id = os.getenv(TRAVIS_BUILD_ID)
polling_interval = int(os.getenv(POLLING_INTERVAL, '5'))
gh_token = os.getenv(GITHUB_TOKEN)

#assume, first job is the leader
is_leader = lambda job_number: job_number.endswith('.1')
Expand All @@ -44,24 +47,24 @@ def __init__(self, json_raw):
self.is_leader = is_leader(self.number)


def matrix_snapshot():
def matrix_snapshot(token):
"""
:return: Matrix List
"""
response = urllib2.build_opener().open("https://api.travis-ci.org/builds/{0}".format(build_id)).read()
response = urllib2.build_opener().open("https://api.travis-ci.com/builds/{0}?access_token={1}".format(build_id, token)).read()
raw_json = json.loads(response)
matrix_without_leader = [MatrixElement(element) for element in raw_json["matrix"]]
return matrix_without_leader


def wait_others_to_finish():
def wait_others_to_finish(token):
def others_finished():
"""
Dumps others to finish
Leader cannot finish, it is working now
:return: tuple(True or False, List of not finished jobs)
"""
snapshot = matrix_snapshot()
snapshot = matrix_snapshot(token)
finished = [el.is_finished for el in snapshot if not el.is_leader]
return reduce(lambda a, b: a and b, finished), [el.number for el in snapshot if
not el.is_leader and not el.is_finished]
Expand All @@ -72,11 +75,18 @@ def others_finished():
log.info("Leader waits for minions {0}...".format(waiting_list)) # just in case do not get "silence timeout"
time.sleep(polling_interval)

def getToken():
data = {"github_token":gh_token}
headers = {'content-type': 'application/json'}
response = requests.post('https://api.travis-ci.com/auth/github', data=json.dumps(data), headers=headers).json()
token = response.get('access_token')
return token

try:
wait_others_to_finish()
token = getToken()
wait_others_to_finish(token)

final_snapshot = matrix_snapshot()
final_snapshot = matrix_snapshot(token)
log.info("Final Results: {0}".format([(e.number, e.is_succeeded) for e in final_snapshot]))

BUILD_AGGREGATE_STATUS = 'BUILD_AGGREGATE_STATUS'
Expand Down

0 comments on commit 4b709cb

Please sign in to comment.