Merge changes Ie7a1b820,If986af3a,Iaf4016c7 am: e8d1c1adeb
am: 092a492f4d
Change-Id: I18117f6d6f0ce30a759c49f3f2cdd0a1bd0a5809
This commit is contained in:
@@ -138,6 +138,23 @@ def query_change_lists(url_opener, gerrit, query_string, limits):
|
||||
response_file.close()
|
||||
|
||||
|
||||
def _make_json_post_request(url_opener, url, data, method='POST'):
|
||||
data = json.dumps(data).encode('utf-8')
|
||||
headers = {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
}
|
||||
|
||||
request = Request(url, data, headers)
|
||||
request.get_method = lambda: method
|
||||
response_file = url_opener.open(request)
|
||||
try:
|
||||
res_code = response_file.getcode()
|
||||
res_json = _decode_xssi_json(response_file.read())
|
||||
return (res_code, res_json)
|
||||
finally:
|
||||
response_file.close()
|
||||
|
||||
|
||||
def set_review(url_opener, gerrit_url, change_id, labels, message):
|
||||
"""Set review votes to a change list."""
|
||||
|
||||
@@ -149,20 +166,8 @@ def set_review(url_opener, gerrit_url, change_id, labels, message):
|
||||
data['labels'] = labels
|
||||
if message:
|
||||
data['message'] = message
|
||||
data = json.dumps(data).encode('utf-8')
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
}
|
||||
|
||||
request = Request(url, data, headers)
|
||||
response_file = url_opener.open(request)
|
||||
try:
|
||||
res_code = response_file.getcode()
|
||||
res_json = _decode_xssi_json(response_file.read())
|
||||
return (res_code, res_json)
|
||||
finally:
|
||||
response_file.close()
|
||||
return _make_json_post_request(url_opener, url, data)
|
||||
|
||||
|
||||
def abandon(url_opener, gerrit_url, change_id, message):
|
||||
@@ -173,22 +178,46 @@ def abandon(url_opener, gerrit_url, change_id, message):
|
||||
data = {}
|
||||
if message:
|
||||
data['message'] = message
|
||||
data = json.dumps(data).encode('utf-8')
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
}
|
||||
return _make_json_post_request(url_opener, url, data)
|
||||
|
||||
request = Request(url, data, headers)
|
||||
|
||||
def set_topic(url_opener, gerrit_url, change_id, name):
|
||||
"""Set the topic name."""
|
||||
|
||||
url = '{}/a/changes/{}/topic'.format(gerrit_url, change_id)
|
||||
data = {'topic': name}
|
||||
return _make_json_post_request(url_opener, url, data, method='PUT')
|
||||
|
||||
|
||||
def delete_topic(url_opener, gerrit_url, change_id):
|
||||
"""Delete the topic name."""
|
||||
|
||||
url = '{}/a/changes/{}/topic'.format(gerrit_url, change_id)
|
||||
request = Request(url)
|
||||
request.get_method = lambda: 'DELETE'
|
||||
response_file = url_opener.open(request)
|
||||
try:
|
||||
res_code = response_file.getcode()
|
||||
res_json = _decode_xssi_json(response_file.read())
|
||||
return (res_code, res_json)
|
||||
return (response_file.getcode(), response_file.read())
|
||||
finally:
|
||||
response_file.close()
|
||||
|
||||
|
||||
def set_hashtags(url_opener, gerrit_url, change_id, add_tags=None,
|
||||
remove_tags=None):
|
||||
"""Add or remove hash tags."""
|
||||
|
||||
url = '{}/a/changes/{}/hashtags'.format(gerrit_url, change_id)
|
||||
|
||||
data = {}
|
||||
if add_tags:
|
||||
data['add'] = add_tags
|
||||
if remove_tags:
|
||||
data['remove'] = remove_tags
|
||||
|
||||
return _make_json_post_request(url_opener, url, data)
|
||||
|
||||
|
||||
def get_patch(url_opener, gerrit_url, change_id, revision_id='current'):
|
||||
"""Download the patch file."""
|
||||
|
||||
|
||||
@@ -31,7 +31,8 @@ except ImportError:
|
||||
from urllib2 import HTTPError # PY2
|
||||
|
||||
from gerrit import (
|
||||
create_url_opener_from_args, query_change_lists, set_review, abandon)
|
||||
abandon, create_url_opener_from_args, delete_topic, query_change_lists,
|
||||
set_hashtags, set_review, set_topic)
|
||||
|
||||
|
||||
def _get_labels_from_args(args):
|
||||
@@ -100,15 +101,41 @@ def _parse_args():
|
||||
|
||||
parser.add_argument('--abandon', help='Abandon a CL with a message')
|
||||
|
||||
parser.add_argument('--add-hashtag', action='append', help='Add hashtag')
|
||||
parser.add_argument('--remove-hashtag', action='append',
|
||||
help='Remove hashtag')
|
||||
parser.add_argument('--delete-hashtag', action='append',
|
||||
help='Remove hashtag', dest='remove_hashtag')
|
||||
|
||||
parser.add_argument('--set-topic', help='Set topic name')
|
||||
parser.add_argument('--delete-topic', action='store_true',
|
||||
help='Delete topic name')
|
||||
parser.add_argument('--remove-topic', action='store_true',
|
||||
help='Delete topic name', dest='delete_topic')
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def _has_task(args):
|
||||
"""Determine whether a task has been specified in the arguments."""
|
||||
if args.label is not None or args.message is not None:
|
||||
return True
|
||||
if args.abandon is not None:
|
||||
return True
|
||||
if args.add_hashtag or args.remove_hashtag:
|
||||
return True
|
||||
if args.set_topic or args.delete_topic:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
_SEP_SPLIT = '=' * 79
|
||||
_SEP = '-' * 79
|
||||
|
||||
|
||||
def _report_error(change, res_code, res_json):
|
||||
def _print_error(change, res_code, res_json):
|
||||
"""Print the error message"""
|
||||
|
||||
change_id = change['change_id']
|
||||
project = change['project']
|
||||
revision_sha1 = change['current_revision']
|
||||
@@ -128,15 +155,32 @@ def _report_error(change, res_code, res_json):
|
||||
print(_SEP_SPLIT, file=sys.stderr)
|
||||
|
||||
|
||||
def _do_task(change, func, *args, **kwargs):
|
||||
"""Process a task and report errors when necessary."""
|
||||
try:
|
||||
res_code, res_json = func(*args)
|
||||
except HTTPError as error:
|
||||
res_code = error.code
|
||||
res_json = None
|
||||
|
||||
if res_code != kwargs.get('expected_http_code', 200):
|
||||
_print_error(change, res_code, res_json)
|
||||
|
||||
errors = kwargs.get('errors')
|
||||
if errors is not None:
|
||||
errors['num_errors'] += 1
|
||||
|
||||
|
||||
def main():
|
||||
"""Set review labels to selected change lists"""
|
||||
|
||||
# Parse and check the command line options
|
||||
args = _parse_args()
|
||||
|
||||
# Check the command line options
|
||||
if args.label is None and args.message is None and args.abandon is None:
|
||||
print('error: Either --label, --message, or --abandon must be ',
|
||||
if not _has_task(args):
|
||||
print('error: Either --label, --message, --abandon, --add-hashtag, '
|
||||
'--remove-hashtag, --set-topic, or --delete-topic must be ',
|
||||
'specified', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Convert label arguments
|
||||
labels = _get_labels_from_args(args)
|
||||
@@ -158,33 +202,26 @@ def main():
|
||||
_confirm('Do you want to continue?')
|
||||
|
||||
# Post review votes
|
||||
has_error = False
|
||||
errors = {'num_errors': 0}
|
||||
for change in change_lists:
|
||||
if args.label or args.message:
|
||||
try:
|
||||
res_code, res_json = set_review(
|
||||
url_opener, args.gerrit, change['id'], labels, args.message)
|
||||
except HTTPError as error:
|
||||
res_code = error.code
|
||||
res_json = None
|
||||
|
||||
if res_code != 200:
|
||||
has_error = True
|
||||
_report_error(change, res_code, res_json)
|
||||
|
||||
_do_task(change, set_review, url_opener, args.gerrit, change['id'],
|
||||
labels, args.message, errors=errors)
|
||||
if args.add_hashtag or args.remove_hashtag:
|
||||
_do_task(change, set_hashtags, url_opener, args.gerrit,
|
||||
change['id'], args.add_hashtag, args.remove_hashtag,
|
||||
errors=errors)
|
||||
if args.set_topic:
|
||||
_do_task(change, set_topic, url_opener, args.gerrit, change['id'],
|
||||
args.set_topic, errors=errors)
|
||||
if args.delete_topic:
|
||||
_do_task(change, delete_topic, url_opener, args.gerrit,
|
||||
change['id'], expected_http_code=204, errors=errors)
|
||||
if args.abandon:
|
||||
try:
|
||||
res_code, res_json = abandon(
|
||||
url_opener, args.gerrit, change['id'], args.abandon)
|
||||
except HTTPError as error:
|
||||
res_code = error.code
|
||||
res_json = None
|
||||
_do_task(change, abandon, url_opener, args.gerrit, change['id'],
|
||||
args.abandon, errors=errors)
|
||||
|
||||
if res_code != 200:
|
||||
has_error = True
|
||||
_report_error(change, res_code, res_json)
|
||||
|
||||
if has_error:
|
||||
if errors['num_errors']:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user