Skip to content

Instantly share code, notes, and snippets.

@samueljackson92
Created August 19, 2016 06:45
Show Gist options
  • Save samueljackson92/54017a1ba2139e5b0476d37029c53239 to your computer and use it in GitHub Desktop.
Save samueljackson92/54017a1ba2139e5b0476d37029c53239 to your computer and use it in GitHub Desktop.
Apply patch from clang-format job
#!/usr/bin/env python
import requests
import sys
import subprocess
import os
import tempfile
def get_build_list():
contents = get_json_data("http://builds.mantidproject.org/job/pull_requests-clang-format/api/json?pretty=true")
if 'builds' in contents.keys():
return [build['url'] for build in contents['builds']]
else:
print "Could not retrieve build list from server"
sys.exit(-1)
def get_json_data(url):
r = requests.get(url)
if r.status_code == 200:
return r.json()
else:
print "Could not retrieve JSON from server"
sys.exit(-1)
def get_last_commit():
try:
return subprocess.check_output(['git', 'rev-list', '-1', '--no-merges', '--abbrev-commit', 'HEAD']).strip()
except Exception:
sys.exit(-1)
def git_apply_patch(patch):
try:
return subprocess.check_output(['git', 'apply', patch]).strip()
except Exception:
sys.exit(-1)
def find_correct_build():
last_commit = get_last_commit()
print "Last commit was: " + last_commit
print "Searching for build..."
found = False
build_urls = get_build_list()
for url in build_urls:
data = get_json_data(url + '/api/json')
if data['result'] == 'FAILURE':
patch_name = data['artifacts'][0]['fileName']
patch_base = os.path.splitext(patch_name)[0]
sha = patch_base.split('-')[-1]
if sha == last_commit:
found = True
break
if not found:
print "Could not find failed build with patch to apply."
sys.exit(-1)
r = requests.get(data['url'] + '/artifact/' + patch_name)
if r.status_code == 200:
print "Applying path " + patch_name
with tempfile.NamedTemporaryFile() as temp:
temp.write(r.text)
temp.flush()
git_apply_patch(temp.name)
if __name__ == "__main__":
find_correct_build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment