Created
March 7, 2014 18:33
-
-
Save vmx/9417133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Prints all the builds in the queue that are from the same Gerrit change | |
number, but have another more recent patch set in the queue. It also | |
prints all changes with a ref change which isn't a new patch set. | |
You can abort all the builds that are returned in case you don't want to | |
test outdated patch sets. The return value is the queue ID, e.g. '1234'. | |
Such a build in the queue can then be cancled with a POST to | |
http://example.com/queue/cancelItem?id | |
The script can be used as: | |
python abortqueue.py |xargs -i curl -X POST -v 'http://example.com/queue/cancelItem?id={}' | |
""" | |
import collections | |
import json | |
import urllib2 as request | |
import sys | |
JENKINS_ROOT = 'http://example.com/' | |
def get_changes_in_queue(): | |
url = JENKINS_ROOT + 'queue/api/json' | |
response = json.loads(request.urlopen(url).read()) | |
changes = collections.defaultdict(dict) | |
for item in response['items']: | |
#print 'url: {}'.format(item['url']) | |
for action in item['actions']: | |
if 'parameters' in action: | |
patchset_numbers = [] | |
patchset_number = None | |
ref_updated = False | |
ref_newrev = None | |
for parameter in action['parameters']: | |
if parameter['name'] == 'GERRIT_CHANGE_NUMBER': | |
change_number = parameter['value'] | |
#print '{}'.format(parameter['value']) | |
if parameter['name'] == 'GERRIT_PATCHSET_NUMBER': | |
patchset_number = int(parameter['value']) | |
if (parameter['name'] == 'GERRIT_EVENT_TYPE' and | |
parameter['value'] == 'ref-updated'): | |
ref_updated = True | |
if (parameter['name'] == 'GERRIT_NEWREV'): | |
ref_newrev = parameter['value'] | |
if patchset_number: | |
changes[change_number][patchset_number] = item['url'] | |
elif ref_updated: | |
changes['ref-updated'][ref_newrev] = item['url'] | |
return changes | |
def process_changes(changes): | |
result = [] | |
for key, change in changes.items(): | |
if key == 'ref-updated': | |
queue_ids = [s.split('/')[2] for s in change.values()] | |
result += queue_ids | |
elif len(change) > 1: | |
# XXX vmx 2013-02-19: THIS CODE IS COMPLETELY UNTESTED | |
newest_patchset = max(change.keys()) | |
for patchset in change: | |
if patchset < newest_patchset: | |
result.append(change[patchset].split('/')[2]) | |
return result | |
def main(): | |
if len(sys.argv) != 1: | |
print ('Prints all the builds in the queue that have another build ' | |
'with a newer patch set in the queue') | |
print 'usage: ./abortqueue.py'.format() | |
exit(1) | |
changes = get_changes_in_queue() | |
#print '{}'.format(changes) | |
to_abort = process_changes(changes) | |
#print '{}'.format(to_abort) | |
for abort in to_abort: | |
print '{}'.format(abort) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment