|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# |
| 18 | +# This script checks build status of given pullrequest identified by author and commit hash. |
| 19 | +# |
| 20 | +# usage) |
| 21 | +# python travis_check.py [author] [commit hash] [check interval (optional)] |
| 22 | +# |
| 23 | +# example) |
| 24 | +# # full hash |
| 25 | +# python travis_check.py Leemoonsoo 1f2549a38f440ebfbfe2d32a041684e3e39b496c |
| 26 | +# |
| 27 | +# # with short hash |
| 28 | +# python travis_check.py Leemoonsoo 1f2549a |
| 29 | +# |
| 30 | +# # with custom check interval |
| 31 | +# python travis_check.py Leemoonsoo 1f2549a 5,60,60 |
| 32 | + |
| 33 | +import os, sys, getopt, traceback, json, requests, time |
| 34 | + |
| 35 | +author = sys.argv[1] |
| 36 | +commit = sys.argv[2] |
| 37 | + |
| 38 | +# check interval in sec |
| 39 | +check = [5, 60, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 600, 600, 600, 600, 600, 600] |
| 40 | + |
| 41 | +if len(sys.argv) > 3: |
| 42 | + check = map(lambda x: int(x), sys.argv[3].split(",")) |
| 43 | + |
| 44 | +def info(msg): |
| 45 | + print("[" + time.strftime("%Y-%m-%d %H:%M:%S") + "] " + msg) |
| 46 | + sys.stdout.flush() |
| 47 | + |
| 48 | +info("Author: " + author + ", commit: " + commit) |
| 49 | + |
| 50 | + |
| 51 | +def getBuildStatus(author, commit): |
| 52 | + travisApi = "https://api.travis-ci.org/" |
| 53 | + |
| 54 | + # get latest 25 builds |
| 55 | + resp = requests.get(url=travisApi + "/repos/" + author + "/zeppelin/builds") |
| 56 | + data = json.loads(resp.text) |
| 57 | + build = None |
| 58 | + |
| 59 | + if len(data) == 0: |
| 60 | + return build; |
| 61 | + |
| 62 | + for b in data: |
| 63 | + if b["commit"][:len(commit)] == commit: |
| 64 | + resp = requests.get(url=travisApi + "/repos/" + author + "/zeppelin/builds/" + str(b["id"])) |
| 65 | + build = json.loads(resp.text) |
| 66 | + break |
| 67 | + |
| 68 | + return build |
| 69 | + |
| 70 | +def status(index, msg, jobId): |
| 71 | + return '{:20}'.format("[" + str(index+1) + "] " + msg) + "https://travis-ci.org/" + author + "/zeppelin/jobs/" + str(jobId) |
| 72 | + |
| 73 | +def printBuildStatus(build): |
| 74 | + failure = 0 |
| 75 | + running = 0 |
| 76 | + |
| 77 | + for index, job in enumerate(build["matrix"]): |
| 78 | + result = job["result"] |
| 79 | + jobId = job["id"] |
| 80 | + |
| 81 | + if job["started_at"] == None and result == None: |
| 82 | + print(status(index, "Not started", jobId)) |
| 83 | + running = running + 1 |
| 84 | + elif job["started_at"] != None and job["finished_at"] == None: |
| 85 | + print(status(index, "Running ...", jobId)) |
| 86 | + running = running + 1 |
| 87 | + elif job["started_at"] != None and job["finished_at"] != None: |
| 88 | + if result == None: |
| 89 | + print(status(index, "Not completed", jobId)) |
| 90 | + failure = failure + 1 |
| 91 | + elif result == 0: |
| 92 | + print(status(index, "OK", jobId)) |
| 93 | + else: |
| 94 | + print(status(index, "Error " + str(result), jobId)) |
| 95 | + failure = failure + 1 |
| 96 | + else: |
| 97 | + print(status(index, "Unknown state", jobId)) |
| 98 | + failure = failure + 1 |
| 99 | + |
| 100 | + return failure, running |
| 101 | + |
| 102 | + |
| 103 | +for sleep in check: |
| 104 | + info("--------------------------------") |
| 105 | + time.sleep(sleep); |
| 106 | + info("Get build status ...") |
| 107 | + build = getBuildStatus(author, commit) |
| 108 | + if build == None: |
| 109 | + info("Can't find build for commit " + commit + " from " + author) |
| 110 | + sys.exit(2) |
| 111 | + |
| 112 | + print("Build https://travis-ci.org/" + author + "/zeppelin/builds/" + str(build["id"])) |
| 113 | + failure, running = printBuildStatus(build) |
| 114 | + |
| 115 | + print(str(failure) + " job(s) failed, " + str(running) + " job(s) running/pending") |
| 116 | + |
| 117 | + if failure != 0: |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + if failure == 0 and running == 0: |
| 121 | + info("CI Green!") |
| 122 | + sys.exit(0) |
| 123 | + |
| 124 | +info("Timeout") |
| 125 | +sys.exit(1) |
0 commit comments