Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active January 1, 2016 20:09
Show Gist options
  • Save chmouel/8195145 to your computer and use it in GitHub Desktop.
Save chmouel/8195145 to your computer and use it in GitHub Desktop.
Script that will get the failed jobs and save the console.html (to /tmp) This probably will work only on OpenStack Gerrit/Jenkins infra.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Simple launch gerrit-jenkins-error inside a git repo would get the failed
# job for the current commit (using the changeId) or you can add an
# argument be it the change-id the review number or even something
# like https://review.openstack.org/#/c/101010/ and it would do the
# right thing™
#
# Use the -t option to specify the output to something than /tmp
#
# Pro Tip: grep for failures and errors on the file:
# egrep '(\[ERROR\]|FAIL)' /tmp/console*html
# Get the python tracebacks :
# sed -n '/Traceback/,/-----/ { p;}' /tmp/console*html
__author__ = "Chmouel Boudjnah <[email protected]>"
import argparse
import commands
import json
import re
import requests
import sys
def parse_failures(output):
json_output = json.loads(output)
# Only read the first line
reg = re.compile(
'^- ((gate|check)-[^ ]*).*(http://logs.openstack.org/[^ ]*).*FAILURE')
failures = []
if 'rowCount' in json_output:
print("Seems like your change hasn't been tested yet.")
sys.exit(1)
for comment in json_output['comments']:
if comment['reviewer']['username'] != 'jenkins':
continue
failures = []
for line in re.split('\n', comment['message']):
match = reg.match(line)
if match:
failures.append(match.group(1, 3))
return failures
def save_error(output_file, url):
r = requests.get(url + "/console.html")
try:
if r.status_code == 404:
r = requests.get(url + "/console.html.gz")
r.raise_for_status()
except(requests.exceptions.HTTPError), e:
print(url)
print(e.message)
sys.exit(1)
open(output_file, 'w').write(
r.text.encode('ascii', 'ignore'))
print(output_file)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-t', dest='tmpdir', default="/tmp",
help="Temporary Directory")
parser.add_argument('change_id', nargs="?",
help="The ChangeID or review ID or review URL")
args = parser.parse_args()
if not args.change_id:
status, output = commands.getstatusoutput(
"git log --no-merges -n1|"
"sed -n '/Change-Id: / { s/.*: //;p;}'")
if status != 0 or not output.startswith('I'):
print(output)
sys.exit(1)
change_id = output
elif args.change_id.startswith("https://review.openstack.org/#/c/"):
change_id = args.change_id.replace(
"https://review.openstack.org/#/c/", '').replace('/', '')
else:
change_id = args.change_id
status, output = commands.getstatusoutput(
"ssh -x -p 29418 review.openstack.org "
"'gerrit query --format=JSON --comments --current-patch-set "
"change: %s'" % change_id)
if status != 0:
print(output)
sys.exit(1)
output = output.split('\n')
if len(output) == 1:
print("Seems like an invalid change")
sys.exit(1)
failures = parse_failures(output[0])
if not failures:
print("No failures with Jenkins!! Yay!")
return
print("Failed Jobs: ")
for (name, url) in failures:
save_error("%s/%s-%s.html" % (
args.tmpdir, name, change_id[1:6]), url)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment