Merge changes Ie7a1b820,If986af3a,Iaf4016c7
am: e8d1c1adeb
Change-Id: I5e53d1202ba0fcbfd08cd4798af572f57ea74941
This commit is contained in:
@@ -138,6 +138,23 @@ def query_change_lists(url_opener, gerrit, query_string, limits):
|
|||||||
response_file.close()
|
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):
|
def set_review(url_opener, gerrit_url, change_id, labels, message):
|
||||||
"""Set review votes to a change list."""
|
"""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
|
data['labels'] = labels
|
||||||
if message:
|
if message:
|
||||||
data['message'] = message
|
data['message'] = message
|
||||||
data = json.dumps(data).encode('utf-8')
|
|
||||||
|
|
||||||
headers = {
|
return _make_json_post_request(url_opener, url, data)
|
||||||
'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()
|
|
||||||
|
|
||||||
|
|
||||||
def abandon(url_opener, gerrit_url, change_id, message):
|
def abandon(url_opener, gerrit_url, change_id, message):
|
||||||
@@ -173,22 +178,46 @@ def abandon(url_opener, gerrit_url, change_id, message):
|
|||||||
data = {}
|
data = {}
|
||||||
if message:
|
if message:
|
||||||
data['message'] = message
|
data['message'] = message
|
||||||
data = json.dumps(data).encode('utf-8')
|
|
||||||
|
|
||||||
headers = {
|
return _make_json_post_request(url_opener, url, data)
|
||||||
'Content-Type': 'application/json; charset=UTF-8',
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
response_file = url_opener.open(request)
|
||||||
try:
|
try:
|
||||||
res_code = response_file.getcode()
|
return (response_file.getcode(), response_file.read())
|
||||||
res_json = _decode_xssi_json(response_file.read())
|
|
||||||
return (res_code, res_json)
|
|
||||||
finally:
|
finally:
|
||||||
response_file.close()
|
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'):
|
def get_patch(url_opener, gerrit_url, change_id, revision_id='current'):
|
||||||
"""Download the patch file."""
|
"""Download the patch file."""
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ except ImportError:
|
|||||||
from urllib2 import HTTPError # PY2
|
from urllib2 import HTTPError # PY2
|
||||||
|
|
||||||
from gerrit import (
|
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):
|
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('--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()
|
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_SPLIT = '=' * 79
|
||||||
_SEP = '-' * 79
|
_SEP = '-' * 79
|
||||||
|
|
||||||
|
|
||||||
def _report_error(change, res_code, res_json):
|
def _print_error(change, res_code, res_json):
|
||||||
"""Print the error message"""
|
"""Print the error message"""
|
||||||
|
|
||||||
change_id = change['change_id']
|
change_id = change['change_id']
|
||||||
project = change['project']
|
project = change['project']
|
||||||
revision_sha1 = change['current_revision']
|
revision_sha1 = change['current_revision']
|
||||||
@@ -128,15 +155,32 @@ def _report_error(change, res_code, res_json):
|
|||||||
print(_SEP_SPLIT, file=sys.stderr)
|
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():
|
def main():
|
||||||
"""Set review labels to selected change lists"""
|
"""Set review labels to selected change lists"""
|
||||||
|
|
||||||
|
# Parse and check the command line options
|
||||||
args = _parse_args()
|
args = _parse_args()
|
||||||
|
if not _has_task(args):
|
||||||
# Check the command line options
|
print('error: Either --label, --message, --abandon, --add-hashtag, '
|
||||||
if args.label is None and args.message is None and args.abandon is None:
|
'--remove-hashtag, --set-topic, or --delete-topic must be ',
|
||||||
print('error: Either --label, --message, or --abandon must be ',
|
|
||||||
'specified', file=sys.stderr)
|
'specified', file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Convert label arguments
|
# Convert label arguments
|
||||||
labels = _get_labels_from_args(args)
|
labels = _get_labels_from_args(args)
|
||||||
@@ -158,33 +202,26 @@ def main():
|
|||||||
_confirm('Do you want to continue?')
|
_confirm('Do you want to continue?')
|
||||||
|
|
||||||
# Post review votes
|
# Post review votes
|
||||||
has_error = False
|
errors = {'num_errors': 0}
|
||||||
for change in change_lists:
|
for change in change_lists:
|
||||||
if args.label or args.message:
|
if args.label or args.message:
|
||||||
try:
|
_do_task(change, set_review, url_opener, args.gerrit, change['id'],
|
||||||
res_code, res_json = set_review(
|
labels, args.message, errors=errors)
|
||||||
url_opener, args.gerrit, change['id'], labels, args.message)
|
if args.add_hashtag or args.remove_hashtag:
|
||||||
except HTTPError as error:
|
_do_task(change, set_hashtags, url_opener, args.gerrit,
|
||||||
res_code = error.code
|
change['id'], args.add_hashtag, args.remove_hashtag,
|
||||||
res_json = None
|
errors=errors)
|
||||||
|
if args.set_topic:
|
||||||
if res_code != 200:
|
_do_task(change, set_topic, url_opener, args.gerrit, change['id'],
|
||||||
has_error = True
|
args.set_topic, errors=errors)
|
||||||
_report_error(change, res_code, res_json)
|
if args.delete_topic:
|
||||||
|
_do_task(change, delete_topic, url_opener, args.gerrit,
|
||||||
|
change['id'], expected_http_code=204, errors=errors)
|
||||||
if args.abandon:
|
if args.abandon:
|
||||||
try:
|
_do_task(change, abandon, url_opener, args.gerrit, change['id'],
|
||||||
res_code, res_json = abandon(
|
args.abandon, errors=errors)
|
||||||
url_opener, args.gerrit, change['id'], args.abandon)
|
|
||||||
except HTTPError as error:
|
|
||||||
res_code = error.code
|
|
||||||
res_json = None
|
|
||||||
|
|
||||||
if res_code != 200:
|
if errors['num_errors']:
|
||||||
has_error = True
|
|
||||||
_report_error(change, res_code, res_json)
|
|
||||||
|
|
||||||
if has_error:
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user