Josip Sokcevic | 84434e8 | 2021-06-09 22:48:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
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: |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 9 | ./gerrit_client.py [command] [args] |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 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): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 42 | """Move changes to a different destination branch.""" |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 43 | parser.add_option('-p', '--param', dest='params', action='append', |
| 44 | help='repeatable query parameter, format: -p key=value') |
| 45 | parser.add_option('--destination_branch', dest='destination_branch', |
| 46 | help='where to move changes to') |
| 47 | |
| 48 | (opt, args) = parser.parse_args(args) |
| 49 | assert opt.destination_branch, "--destination_branch not defined" |
Mike Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 50 | for p in opt.params: |
| 51 | assert '=' in p, '--param is key=value, not "%s"' % p |
Josip Sokcevic | c39ab99 | 2020-09-24 20:09:15 +0000 | [diff] [blame] | 52 | host = urlparse.urlparse(opt.host).netloc |
| 53 | |
| 54 | limit = 100 |
| 55 | while True: |
| 56 | result = gerrit_util.QueryChanges( |
| 57 | host, |
| 58 | list(tuple(p.split('=', 1)) for p in opt.params), |
| 59 | limit=limit, |
| 60 | ) |
| 61 | for change in result: |
| 62 | gerrit_util.MoveChange(host, change['id'], opt.destination_branch) |
| 63 | |
| 64 | if len(result) < limit: |
| 65 | break |
| 66 | logging.info("Done") |
| 67 | |
| 68 | |
| 69 | @subcommand.usage('[args ...]') |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 70 | def CMDbranchinfo(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 71 | """Get information on a gerrit branch.""" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 72 | parser.add_option('--branch', dest='branch', help='branch name') |
| 73 | |
| 74 | (opt, args) = parser.parse_args(args) |
| 75 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 76 | project = quote_plus(opt.project) |
| 77 | branch = quote_plus(opt.branch) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 78 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 79 | logging.info(result) |
| 80 | write_result(result, opt) |
| 81 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 82 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 83 | @subcommand.usage('[args ...]') |
Michael Moss | 9c28af4 | 2021-10-25 16:59:05 +0000 | [diff] [blame] | 84 | def CMDrawapi(parser, args): |
| 85 | """Call an arbitrary Gerrit REST API endpoint.""" |
| 86 | parser.add_option('--path', dest='path', help='HTTP path of the API endpoint') |
| 87 | parser.add_option('--method', dest='method', |
| 88 | help='HTTP method for the API (default: GET)') |
| 89 | parser.add_option('--body', dest='body', help='API JSON body contents') |
| 90 | parser.add_option('--accept_status', |
| 91 | dest='accept_status', |
| 92 | help='Comma-delimited list of status codes for success.') |
| 93 | |
| 94 | (opt, args) = parser.parse_args(args) |
| 95 | assert opt.path, "--path not defined" |
| 96 | |
| 97 | host = urlparse.urlparse(opt.host).netloc |
| 98 | kwargs = {} |
| 99 | if opt.method: |
| 100 | kwargs['reqtype'] = opt.method.upper() |
| 101 | if opt.body: |
| 102 | kwargs['body'] = json.loads(opt.body) |
| 103 | if opt.accept_status: |
| 104 | kwargs['accept_statuses'] = [int(x) for x in opt.accept_status.split(',')] |
| 105 | result = gerrit_util.CallGerritApi(host, opt.path, **kwargs) |
| 106 | logging.info(result) |
| 107 | write_result(result, opt) |
| 108 | |
| 109 | |
| 110 | @subcommand.usage('[args ...]') |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 111 | def CMDbranch(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 112 | """Create a branch in a gerrit project.""" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 113 | parser.add_option('--branch', dest='branch', help='branch name') |
| 114 | parser.add_option('--commit', dest='commit', help='commit hash') |
Xinan Lin | dbcecc9 | 2023-05-03 18:29:00 +0000 | [diff] [blame] | 115 | parser.add_option('--allow-existent-branch', |
| 116 | action='store_true', |
| 117 | help=('Accept that the branch alread exists as long as the' |
| 118 | ' branch head points the given commit')) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 119 | |
| 120 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 121 | assert opt.project, "--project not defined" |
| 122 | assert opt.branch, "--branch not defined" |
| 123 | assert opt.commit, "--commit not defined" |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 124 | |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 125 | project = quote_plus(opt.project) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 126 | host = urlparse.urlparse(opt.host).netloc |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 127 | branch = quote_plus(opt.branch) |
Xinan Lin | dbcecc9 | 2023-05-03 18:29:00 +0000 | [diff] [blame] | 128 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 129 | if result: |
| 130 | if not opt.allow_existent_branch: |
| 131 | raise gerrit_util.GerritError(200, 'Branch already exists') |
| 132 | if result.get('revision') != opt.commit: |
| 133 | raise gerrit_util.GerritError( |
| 134 | 200, ('Branch already exists but ' |
| 135 | 'the branch head is not at the given commit')) |
| 136 | else: |
| 137 | try: |
| 138 | result = gerrit_util.CreateGerritBranch(host, project, branch, opt.commit) |
| 139 | except gerrit_util.GerritError as e: |
| 140 | result = gerrit_util.GetGerritBranch(host, project, branch) |
| 141 | if not result: |
| 142 | raise e |
| 143 | # If reached here, we hit a real conflict error, because the |
| 144 | # branch just created is pointing a different commit. |
| 145 | if result.get('revision') != opt.commit: |
| 146 | raise gerrit_util.GerritError( |
| 147 | 200, ('Conflict: branch was created but ' |
| 148 | 'the branch head is not at the given commit')) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 149 | logging.info(result) |
| 150 | write_result(result, opt) |
| 151 | |
| 152 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 153 | @subcommand.usage('[args ...]') |
Michael Moss | b6ce244 | 2021-10-20 04:36:24 +0000 | [diff] [blame] | 154 | def CMDtag(parser, args): |
| 155 | """Create a tag in a gerrit project.""" |
| 156 | parser.add_option('--tag', dest='tag', help='tag name') |
| 157 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 158 | |
| 159 | (opt, args) = parser.parse_args(args) |
| 160 | assert opt.project, "--project not defined" |
| 161 | assert opt.tag, "--tag not defined" |
| 162 | assert opt.commit, "--commit not defined" |
| 163 | |
| 164 | project = quote_plus(opt.project) |
| 165 | host = urlparse.urlparse(opt.host).netloc |
| 166 | tag = quote_plus(opt.tag) |
Michael Moss | 5c9e8b4 | 2021-10-26 16:54:16 +0000 | [diff] [blame] | 167 | result = gerrit_util.CreateGerritTag(host, project, tag, opt.commit) |
Michael Moss | b6ce244 | 2021-10-20 04:36:24 +0000 | [diff] [blame] | 168 | logging.info(result) |
| 169 | write_result(result, opt) |
| 170 | |
| 171 | |
| 172 | @subcommand.usage('[args ...]') |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 173 | def CMDhead(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 174 | """Update which branch the project HEAD points to.""" |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 175 | parser.add_option('--branch', dest='branch', help='branch name') |
| 176 | |
| 177 | (opt, args) = parser.parse_args(args) |
| 178 | assert opt.project, "--project not defined" |
| 179 | assert opt.branch, "--branch not defined" |
| 180 | |
| 181 | project = quote_plus(opt.project) |
| 182 | host = urlparse.urlparse(opt.host).netloc |
| 183 | branch = quote_plus(opt.branch) |
| 184 | result = gerrit_util.UpdateHead(host, project, branch) |
| 185 | logging.info(result) |
| 186 | write_result(result, opt) |
| 187 | |
| 188 | |
| 189 | @subcommand.usage('[args ...]') |
| 190 | def CMDheadinfo(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 191 | """Retrieves the current HEAD of the project.""" |
Josip Sokcevic | df9a802 | 2020-12-08 00:10:19 +0000 | [diff] [blame] | 192 | |
| 193 | (opt, args) = parser.parse_args(args) |
| 194 | assert opt.project, "--project not defined" |
| 195 | |
| 196 | project = quote_plus(opt.project) |
| 197 | host = urlparse.urlparse(opt.host).netloc |
| 198 | result = gerrit_util.GetHead(host, project) |
| 199 | logging.info(result) |
| 200 | write_result(result, opt) |
| 201 | |
| 202 | |
| 203 | @subcommand.usage('[args ...]') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 204 | def CMDchanges(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 205 | """Queries gerrit for matching changes.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 206 | parser.add_option('-p', '--param', dest='params', action='append', |
| 207 | help='repeatable query parameter, format: -p key=value') |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 208 | parser.add_option('-o', '--o-param', dest='o_params', action='append', |
| 209 | help='gerrit output parameters, e.g. ALL_REVISIONS') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 210 | parser.add_option('--limit', dest='limit', type=int, |
| 211 | help='maximum number of results to return') |
| 212 | parser.add_option('--start', dest='start', type=int, |
| 213 | help='how many changes to skip ' |
| 214 | '(starting with the most recent)') |
| 215 | |
| 216 | (opt, args) = parser.parse_args(args) |
Mike Frysinger | 8820ab8 | 2020-11-25 00:52:31 +0000 | [diff] [blame] | 217 | for p in opt.params: |
| 218 | assert '=' in p, '--param is key=value, not "%s"' % p |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 219 | |
| 220 | result = gerrit_util.QueryChanges( |
| 221 | urlparse.urlparse(opt.host).netloc, |
| 222 | list(tuple(p.split('=', 1)) for p in opt.params), |
Paweł Hajdan, Jr | 24025d3 | 2017-07-11 16:38:21 +0200 | [diff] [blame] | 223 | start=opt.start, # Default: None |
| 224 | limit=opt.limit, # Default: None |
| 225 | o_params=opt.o_params, # Default: None |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 226 | ) |
| 227 | logging.info('Change query returned %d changes.', len(result)) |
| 228 | write_result(result, opt) |
| 229 | |
| 230 | |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 231 | @subcommand.usage('[args ...]') |
Marco Georgaklis | 85557a0 | 2021-06-03 15:56:54 +0000 | [diff] [blame] | 232 | def CMDrelatedchanges(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 233 | """Gets related changes for a given change and revision.""" |
Marco Georgaklis | 85557a0 | 2021-06-03 15:56:54 +0000 | [diff] [blame] | 234 | parser.add_option('-c', '--change', type=str, help='change id') |
| 235 | parser.add_option('-r', '--revision', type=str, help='revision id') |
| 236 | |
| 237 | (opt, args) = parser.parse_args(args) |
| 238 | |
| 239 | result = gerrit_util.GetRelatedChanges( |
| 240 | urlparse.urlparse(opt.host).netloc, |
| 241 | change=opt.change, |
| 242 | revision=opt.revision, |
| 243 | ) |
| 244 | logging.info(result) |
| 245 | write_result(result, opt) |
| 246 | |
| 247 | |
| 248 | @subcommand.usage('[args ...]') |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 249 | def CMDcreatechange(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 250 | """Create a new change in gerrit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 251 | parser.add_option('-s', '--subject', help='subject for change') |
| 252 | parser.add_option('-b', |
| 253 | '--branch', |
| 254 | default='main', |
| 255 | help='target branch for change') |
| 256 | parser.add_option( |
| 257 | '-p', |
| 258 | '--param', |
| 259 | dest='params', |
| 260 | action='append', |
| 261 | help='repeatable field value parameter, format: -p key=value') |
| 262 | |
Xinan Lin | 91d2a5d | 2022-02-04 21:18:32 +0000 | [diff] [blame] | 263 | parser.add_option('--cc', |
| 264 | dest='cc_list', |
| 265 | action='append', |
| 266 | help='CC address to notify, format: --cc foo@example.com') |
| 267 | |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 268 | (opt, args) = parser.parse_args(args) |
| 269 | for p in opt.params: |
| 270 | assert '=' in p, '--param is key=value, not "%s"' % p |
| 271 | |
Xinan Lin | 91d2a5d | 2022-02-04 21:18:32 +0000 | [diff] [blame] | 272 | params = list(tuple(p.split('=', 1)) for p in opt.params) |
| 273 | |
| 274 | if opt.cc_list: |
| 275 | params.append(('notify_details', {'CC': {'accounts': opt.cc_list}})) |
| 276 | |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 277 | result = gerrit_util.CreateChange( |
| 278 | urlparse.urlparse(opt.host).netloc, |
| 279 | opt.project, |
| 280 | branch=opt.branch, |
| 281 | subject=opt.subject, |
Xinan Lin | 91d2a5d | 2022-02-04 21:18:32 +0000 | [diff] [blame] | 282 | params=params, |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 283 | ) |
| 284 | logging.info(result) |
| 285 | write_result(result, opt) |
| 286 | |
| 287 | |
| 288 | @subcommand.usage('[args ...]') |
| 289 | def CMDchangeedit(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 290 | """Puts content of a file into a change edit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 291 | parser.add_option('-c', '--change', type=int, help='change number') |
| 292 | parser.add_option('--path', help='path for file') |
| 293 | parser.add_option('--file', help='file to place at |path|') |
| 294 | |
| 295 | (opt, args) = parser.parse_args(args) |
| 296 | |
| 297 | with open(opt.file) as f: |
| 298 | data = f.read() |
| 299 | result = gerrit_util.ChangeEdit( |
| 300 | urlparse.urlparse(opt.host).netloc, opt.change, opt.path, data) |
| 301 | logging.info(result) |
| 302 | write_result(result, opt) |
| 303 | |
| 304 | |
| 305 | @subcommand.usage('[args ...]') |
| 306 | def CMDpublishchangeedit(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 307 | """Publish a Gerrit change edit.""" |
LaMont Jones | 9eed423 | 2021-04-02 16:29:49 +0000 | [diff] [blame] | 308 | parser.add_option('-c', '--change', type=int, help='change number') |
| 309 | parser.add_option('--notify', help='whether to notify') |
| 310 | |
| 311 | (opt, args) = parser.parse_args(args) |
| 312 | |
| 313 | result = gerrit_util.PublishChangeEdit( |
| 314 | urlparse.urlparse(opt.host).netloc, opt.change, opt.notify) |
| 315 | logging.info(result) |
| 316 | write_result(result, opt) |
| 317 | |
| 318 | |
Xinan Lin | 6ec7cd8 | 2021-07-21 00:53:42 +0000 | [diff] [blame] | 319 | @subcommand.usage('[args ...]') |
| 320 | def CMDsubmitchange(parser, args): |
| 321 | """Submit a Gerrit change.""" |
| 322 | parser.add_option('-c', '--change', type=int, help='change number') |
Xinan Lin | 6ec7cd8 | 2021-07-21 00:53:42 +0000 | [diff] [blame] | 323 | (opt, args) = parser.parse_args(args) |
Xinan Lin | 1bd4ffa | 2021-07-28 00:54:22 +0000 | [diff] [blame] | 324 | result = gerrit_util.SubmitChange( |
| 325 | urlparse.urlparse(opt.host).netloc, opt.change) |
Xinan Lin | 6ec7cd8 | 2021-07-21 00:53:42 +0000 | [diff] [blame] | 326 | logging.info(result) |
| 327 | write_result(result, opt) |
| 328 | |
| 329 | |
Xinan Lin | c2fb26a | 2021-07-27 18:01:55 +0000 | [diff] [blame] | 330 | @subcommand.usage('[args ...]') |
Xinan Lin | 2b4ec95 | 2021-08-20 17:35:29 +0000 | [diff] [blame] | 331 | def CMDchangesubmittedtogether(parser, args): |
| 332 | """Get all changes submitted with the given one.""" |
| 333 | parser.add_option('-c', '--change', type=int, help='change number') |
| 334 | (opt, args) = parser.parse_args(args) |
| 335 | result = gerrit_util.GetChangesSubmittedTogether( |
| 336 | urlparse.urlparse(opt.host).netloc, opt.change) |
| 337 | logging.info(result) |
| 338 | write_result(result, opt) |
| 339 | |
| 340 | |
| 341 | @subcommand.usage('[args ...]') |
Xinan Lin | c2fb26a | 2021-07-27 18:01:55 +0000 | [diff] [blame] | 342 | def CMDgetcommitincludedin(parser, args): |
| 343 | """Retrieves the branches and tags for a given commit.""" |
| 344 | parser.add_option('--commit', dest='commit', help='commit hash') |
| 345 | (opt, args) = parser.parse_args(args) |
| 346 | result = gerrit_util.GetCommitIncludedIn( |
| 347 | urlparse.urlparse(opt.host).netloc, opt.project, opt.commit) |
| 348 | logging.info(result) |
| 349 | write_result(result, opt) |
| 350 | |
| 351 | |
Xinan Lin | 0b0738d | 2021-07-27 19:13:49 +0000 | [diff] [blame] | 352 | @subcommand.usage('[args ...]') |
| 353 | def CMDsetbotcommit(parser, args): |
| 354 | """Sets bot-commit+1 to a bot generated change.""" |
| 355 | parser.add_option('-c', '--change', type=int, help='change number') |
| 356 | (opt, args) = parser.parse_args(args) |
| 357 | result = gerrit_util.SetReview( |
| 358 | urlparse.urlparse(opt.host).netloc, |
| 359 | opt.change, |
| 360 | labels={'Bot-Commit': 1}, |
| 361 | ready=True) |
| 362 | logging.info(result) |
| 363 | write_result(result, opt) |
| 364 | |
| 365 | |
Ben Pastene | 281edf7 | 2021-10-06 01:23:24 +0000 | [diff] [blame] | 366 | @subcommand.usage('[args ...]') |
| 367 | def CMDsetlabel(parser, args): |
| 368 | """Sets a label to a specific value on a given change.""" |
| 369 | parser.add_option('-c', '--change', type=int, help='change number') |
| 370 | parser.add_option('-l', |
| 371 | '--label', |
| 372 | nargs=2, |
| 373 | metavar=('label_name', 'label_value')) |
| 374 | (opt, args) = parser.parse_args(args) |
| 375 | result = gerrit_util.SetReview(urlparse.urlparse(opt.host).netloc, |
| 376 | opt.change, |
| 377 | labels={opt.label[0]: opt.label[1]}) |
| 378 | logging.info(result) |
| 379 | write_result(result, opt) |
| 380 | |
| 381 | |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 382 | @subcommand.usage('') |
| 383 | def CMDabandon(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 384 | """Abandons a Gerrit change.""" |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 385 | parser.add_option('-c', '--change', type=int, help='change number') |
| 386 | parser.add_option('-m', '--message', default='', help='reason for abandoning') |
| 387 | |
| 388 | (opt, args) = parser.parse_args(args) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 389 | assert opt.change, "-c not defined" |
Sergiy Belozorov | fe34723 | 2019-02-27 15:07:33 +0000 | [diff] [blame] | 390 | result = gerrit_util.AbandonChange( |
| 391 | urlparse.urlparse(opt.host).netloc, |
| 392 | opt.change, opt.message) |
| 393 | logging.info(result) |
| 394 | write_result(result, opt) |
| 395 | |
| 396 | |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 397 | @subcommand.usage('') |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 398 | def CMDmass_abandon(parser, args): |
Michael Moss | 5eebf6f | 2021-07-16 13:18:34 +0000 | [diff] [blame] | 399 | """Mass abandon changes |
| 400 | |
| 401 | Abandons CLs that match search criteria provided by user. Before any change is |
| 402 | actually abandoned, user is presented with a list of CLs that will be affected |
| 403 | if user confirms. User can skip confirmation by passing --force parameter. |
| 404 | |
| 405 | The script can abandon up to 100 CLs per invocation. |
| 406 | |
| 407 | Examples: |
| 408 | gerrit_client.py mass-abandon --host https://HOST -p 'project=repo2' |
| 409 | gerrit_client.py mass-abandon --host https://HOST -p 'message=testing' |
| 410 | gerrit_client.py mass-abandon --host https://HOST -p 'is=wip' -p 'age=1y' |
| 411 | """ |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 412 | parser.add_option('-p', |
| 413 | '--param', |
| 414 | dest='params', |
| 415 | action='append', |
| 416 | default=[], |
| 417 | help='repeatable query parameter, format: -p key=value') |
| 418 | parser.add_option('-m', '--message', default='', help='reason for abandoning') |
| 419 | parser.add_option('-f', |
| 420 | '--force', |
| 421 | action='store_true', |
| 422 | help='Don\'t prompt for confirmation') |
| 423 | |
| 424 | opt, args = parser.parse_args(args) |
| 425 | |
| 426 | for p in opt.params: |
| 427 | assert '=' in p, '--param is key=value, not "%s"' % p |
| 428 | search_query = list(tuple(p.split('=', 1)) for p in opt.params) |
| 429 | if not any(t for t in search_query if t[0] == 'owner'): |
| 430 | # owner should always be present when abandoning changes |
| 431 | search_query.append(('owner', 'me')) |
| 432 | search_query.append(('status', 'open')) |
| 433 | logging.info("Searching for: %s" % search_query) |
| 434 | |
| 435 | host = urlparse.urlparse(opt.host).netloc |
| 436 | |
| 437 | result = gerrit_util.QueryChanges( |
| 438 | host, |
| 439 | search_query, |
| 440 | # abandon at most 100 changes as not all Gerrit instances support |
| 441 | # unlimited results. |
| 442 | limit=100, |
| 443 | ) |
| 444 | if len(result) == 0: |
Nico Weber | 6b33f85 | 2023-06-21 17:14:24 +0000 | [diff] [blame] | 445 | logging.warning("Nothing to abandon") |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 446 | return |
| 447 | |
Nico Weber | 6b33f85 | 2023-06-21 17:14:24 +0000 | [diff] [blame] | 448 | logging.warning("%s CLs match search query: " % len(result)) |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 449 | for change in result: |
Nico Weber | 6b33f85 | 2023-06-21 17:14:24 +0000 | [diff] [blame] | 450 | logging.warning("[ID: %d] %s" % (change['_number'], change['subject'])) |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 451 | |
| 452 | if not opt.force: |
Josip Sokcevic | 284fbdd | 2021-10-08 18:26:30 +0000 | [diff] [blame] | 453 | q = input( |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 454 | 'Do you want to move forward with abandoning? [y to confirm] ').strip() |
| 455 | if q not in ['y', 'Y']: |
Nico Weber | 6b33f85 | 2023-06-21 17:14:24 +0000 | [diff] [blame] | 456 | logging.warning("Aborting...") |
Josip Sokcevic | e1a9894 | 2021-04-07 21:35:29 +0000 | [diff] [blame] | 457 | return |
| 458 | |
| 459 | for change in result: |
| 460 | logging.warning("Abandoning: %s" % change['subject']) |
| 461 | gerrit_util.AbandonChange(host, change['id'], opt.message) |
| 462 | |
| 463 | logging.warning("Done") |
| 464 | |
| 465 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 466 | class OptionParser(optparse.OptionParser): |
| 467 | """Creates the option parse and add --verbose support.""" |
| 468 | def __init__(self, *args, **kwargs): |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 469 | optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 470 | self.add_option( |
| 471 | '--verbose', action='count', default=0, |
| 472 | help='Use 2 times for more debugging info') |
| 473 | self.add_option('--host', dest='host', help='Url of host.') |
| 474 | self.add_option('--project', dest='project', help='project name') |
| 475 | self.add_option( |
| 476 | '--json_file', dest='json_file', help='output json filepath') |
| 477 | |
| 478 | def parse_args(self, args=None, values=None): |
| 479 | options, args = optparse.OptionParser.parse_args(self, args, values) |
Josip Sokcevic | c99efb2 | 2020-03-17 00:35:34 +0000 | [diff] [blame] | 480 | # Host is always required |
| 481 | assert options.host, "--host not defined." |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 482 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 483 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 484 | return options, args |
| 485 | |
| 486 | |
| 487 | def main(argv): |
| 488 | if sys.hexversion < 0x02060000: |
| 489 | print('\nYour python version %s is unsupported, please upgrade.\n' |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 490 | % (sys.version.split(' ', 1)[0],), |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 491 | file=sys.stderr) |
| 492 | return 2 |
| 493 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 494 | return dispatcher.execute(OptionParser(), argv) |
| 495 | |
| 496 | |
| 497 | if __name__ == '__main__': |
| 498 | # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 499 | # unit testing. |
| 500 | fix_encoding.fix_encoding() |
| 501 | setup_color.init() |
| 502 | try: |
| 503 | sys.exit(main(sys.argv[1:])) |
| 504 | except KeyboardInterrupt: |
| 505 | sys.stderr.write('interrupted\n') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 506 | sys.exit(1) |