Josip Sokcevic | 848a536 | 2021-03-22 22:58:00 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 2 | # Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Simple client for the Gerrit REST API. |
| 7 | |
| 8 | Example usage: |
| 9 | ./gerrit_client.py [command] [args]"" |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import json |
| 15 | import logging |
| 16 | import optparse |
| 17 | import subcommand |
| 18 | import sys |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 19 | |
| 20 | if sys.version_info.major == 2: |
| 21 | import urlparse |
| 22 | from urllib import quote_plus |
| 23 | else: |
| 24 | from urllib.parse import quote_plus |
| 25 | import urllib.parse as urlparse |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 26 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 27 | import fix_encoding |
| 28 | import gerrit_util |
| 29 | import setup_color |
| 30 | |
| 31 | __version__ = '0.1' |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def write_result(result, opt): |
| 35 | if opt.json_file: |
| 36 | with open(opt.json_file, 'w') as json_file: |
| 37 | json_file.write(json.dumps(result)) |
| 38 | |
| 39 | |
| 40 | @subcommand.usage('[args ...]') |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 41 | def CMDmovechanges(parser, args): |
| 42 | parser.add_option('-p', '--param', dest='params', action='append', |
| 43 | help='repeatable query parameter, format: -p key=value') |
| 44 | parser.add_option('--destination_branch', dest='destination_branch', |
| 45 | help='where to move changes to') |
| 46 | |
| 47 | (opt, args) = parser.parse_args(args) |
| 48 | assert opt.destination_branch, "--destination_branch not defined" |
Mike Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 49 | for p in opt.params: |
| 50 | assert '=' in p, '--param is key=value, not "%s"' % p |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 51 | host = urlparse.urlparse(opt.host).netloc |
| 52 | |
| 53 | limit = 100 |
| 54 | while True: |
| 55 | result = gerrit_util.QueryChanges( |
| 56 | host, |
| 57 | list(tuple(p.split('=', 1)) for p in opt.params), |
| 58 | limit=limit, |
| 59 | ) |
| 60 | for change in result: |
| 61 | gerrit_util.MoveChange(host, change['id'], opt.destination_branch) |
| 62 | |
| 63 | if len(result) < limit: |
| 64 | break |
| 65 | logging.info("Done") |
| 66 | |
| 67 | |
| 68 | @subcommand.usage('[args ...]') |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 69 | def CMDbranchinfo(parser, args): |
| 70 | parser.add_option('--branch', dest='branch', help='branch name') |
| 71 | |
| 72 | (opt, args) = parser.parse_args(args) |
| 73 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 74 | project = quote_plus(opt.project) |
| 75 | branch = quote_plus(opt.branch) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 76 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 77 | logging.info(result) |
| 78 | write_result(result, opt) |
| 79 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 80 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 81 | @subcommand.usage('[args ...]') |
| 82 | def CMDbranch(parser, args): |
| 83 | parser.add_option('--branch', dest='branch', help='branch name') |
| 84 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 85 | |
| 86 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 87 | assert opt.project, "--project not defined" |
| 88 | assert opt.branch, "--branch not defined" |
| 89 | assert opt.commit, "--commit not defined" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 90 | |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 91 | project = quote_plus(opt.project) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 92 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 93 | branch = quote_plus(opt.branch) |
| 94 | commit = quote_plus(opt.commit) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 95 | result = gerrit_util.CreateGerritBranch(host, project, branch, commit) |
| 96 | logging.info(result) |
| 97 | write_result(result, opt) |
| 98 | |
| 99 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 100 | @subcommand.usage('[args ...]') |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 101 | def CMDhead(parser, args): |
| 102 | parser.add_option('--branch', dest='branch', help='branch name') |
| 103 | |
| 104 | (opt, args) = parser.parse_args(args) |
| 105 | assert opt.project, "--project not defined" |
| 106 | assert opt.branch, "--branch not defined" |
| 107 | |
| 108 | project = quote_plus(opt.project) |
| 109 | host = urlparse.urlparse(opt.host).netloc |
| 110 | branch = quote_plus(opt.branch) |
| 111 | result = gerrit_util.UpdateHead(host, project, branch) |
| 112 | logging.info(result) |
| 113 | write_result(result, opt) |
| 114 | |
| 115 | |
| 116 | @subcommand.usage('[args ...]') |
| 117 | def CMDheadinfo(parser, args): |
| 118 | |
| 119 | (opt, args) = parser.parse_args(args) |
| 120 | assert opt.project, "--project not defined" |
| 121 | |
| 122 | project = quote_plus(opt.project) |
| 123 | host = urlparse.urlparse(opt.host).netloc |
| 124 | result = gerrit_util.GetHead(host, project) |
| 125 | logging.info(result) |
| 126 | write_result(result, opt) |
| 127 | |
| 128 | |
| 129 | @subcommand.usage('[args ...]') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 130 | def CMDchanges(parser, args): |
| 131 | parser.add_option('-p', '--param', dest='params', action='append', |
| 132 | help='repeatable query parameter, format: -p key=value') |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 133 | parser.add_option('-o', '--o-param', dest='o_params', action='append', |
| 134 | help='gerrit output parameters, e.g. ALL_REVISIONS') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 135 | parser.add_option('--limit', dest='limit', type=int, |
| 136 | help='maximum number of results to return') |
| 137 | parser.add_option('--start', dest='start', type=int, |
| 138 | help='how many changes to skip ' |
| 139 | '(starting with the most recent)') |
| 140 | |
| 141 | (opt, args) = parser.parse_args(args) |
Mike Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 142 | for p in opt.params: |
| 143 | assert '=' in p, '--param is key=value, not "%s"' % p |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 144 | |
| 145 | result = gerrit_util.QueryChanges( |
| 146 | urlparse.urlparse(opt.host).netloc, |
| 147 | list(tuple(p.split('=', 1)) for p in opt.params), |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 148 | start=opt.start, # Default: None |
| 149 | limit=opt.limit, # Default: None |
| 150 | o_params=opt.o_params, # Default: None |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 151 | ) |
| 152 | logging.info('Change query returned %d changes.', len(result)) |
| 153 | write_result(result, opt) |
| 154 | |
| 155 | |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 156 | @subcommand.usage('') |
| 157 | def CMDabandon(parser, args): |
| 158 | parser.add_option('-c', '--change', type=int, help='change number') |
| 159 | parser.add_option('-m', '--message', default='', help='reason for abandoning') |
| 160 | |
| 161 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 162 | assert opt.change, "-c not defined" |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 163 | result = gerrit_util.AbandonChange( |
| 164 | urlparse.urlparse(opt.host).netloc, |
| 165 | opt.change, opt.message) |
| 166 | logging.info(result) |
| 167 | write_result(result, opt) |
| 168 | |
| 169 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 170 | class OptionParser(optparse.OptionParser): |
| 171 | """Creates the option parse and add --verbose support.""" |
| 172 | def __init__(self, *args, **kwargs): |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 173 | optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 174 | self.add_option( |
| 175 | '--verbose', action='count', default=0, |
| 176 | help='Use 2 times for more debugging info') |
| 177 | self.add_option('--host', dest='host', help='Url of host.') |
| 178 | self.add_option('--project', dest='project', help='project name') |
| 179 | self.add_option( |
| 180 | '--json_file', dest='json_file', help='output json filepath') |
| 181 | |
| 182 | def parse_args(self, args=None, values=None): |
| 183 | options, args = optparse.OptionParser.parse_args(self, args, values) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 184 | # Host is always required |
| 185 | assert options.host, "--host not defined." |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 186 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 187 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 188 | return options, args |
| 189 | |
| 190 | |
| 191 | def main(argv): |
| 192 | if sys.hexversion < 0x02060000: |
| 193 | print('\nYour python version %s is unsupported, please upgrade.\n' |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 194 | % (sys.version.split(' ', 1)[0],), |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 195 | file=sys.stderr) |
| 196 | return 2 |
| 197 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 198 | return dispatcher.execute(OptionParser(), argv) |
| 199 | |
| 200 | |
| 201 | if __name__ == '__main__': |
| 202 | # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 203 | # unit testing. |
| 204 | fix_encoding.fix_encoding() |
| 205 | setup_color.init() |
| 206 | try: |
| 207 | sys.exit(main(sys.argv[1:])) |
| 208 | except KeyboardInterrupt: |
| 209 | sys.stderr.write('interrupted\n') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 210 | sys.exit(1) |