From ec40d02c8a16c9bc03a8eddddb71979244f5f1fd Mon Sep 17 00:00:00 2001 From: John Budorick Date: Mon, 10 Dec 2018 17:59:09 +0000 Subject: [PATCH] buildbucket: add --response-json. Bug: 912391 Change-Id: I8302918397977e03fe7c10179f854ce385ad92e4 Reviewed-on: https://chromium-review.googlesource.com/c/1368884 Reviewed-by: Sergiy Belozorov Reviewed-by: Nodir Turakulov Commit-Queue: John Budorick --- buildbucket.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/buildbucket.py b/buildbucket.py index 9f5a3e00a5..bbb899730d 100755 --- a/buildbucket.py +++ b/buildbucket.py @@ -34,6 +34,16 @@ BUILDBUCKET_API_URL = urlparse.urljoin( ) +def add_common_arguments(parser): + parser.add_argument( + '--response-json', + help=( + 'A path to which the response JSON will be written. ' + 'If no valid JSON is received, nothing will be written.' + ) + ) + + def main(argv): parser = argparse.ArgumentParser() parser.add_argument( @@ -42,13 +52,17 @@ def main(argv): action='store_true', ) subparsers = parser.add_subparsers(dest='command') + get_parser = subparsers.add_parser('get') + add_common_arguments(get_parser) get_parser.add_argument( '--id', help='The ID of the build to get the status of.', required=True, ) + put_parser = subparsers.add_parser('put') + add_common_arguments(put_parser) put_parser.add_argument( '-b', '--bucket', @@ -77,7 +91,9 @@ def main(argv): 'from another command.' ), ) + retry_parser = subparsers.add_parser('retry') + add_common_arguments(retry_parser) retry_parser.add_argument( '--id', help='The ID of the build to retry.', @@ -153,11 +169,15 @@ def main(argv): print 'Content:', content try: - build_url = json.loads(content)['build']['url'] + content_json = json.loads(content) + if args.response_json: + with open(args.response_json, 'w') as response_json_file: + response_json_file.write(content) + build_url = content_json['build']['url'] except (ValueError, TypeError, KeyError): pass else: - print 'Build triggered on: %s' % build_url + print 'Build: %s' % build_url return response.status != 200