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