Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | 0873751 | 2014-02-07 22:58:26 -0500 | [diff] [blame] | 5 | """A command line interface to Gerrit-on-borg instances. |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 6 | |
| 7 | Internal Note: |
| 8 | To expose a function directly to the command line interface, name your function |
| 9 | with the prefix "UserAct". |
| 10 | """ |
| 11 | |
Mike Frysinger | 8037f75 | 2020-02-29 20:47:09 -0500 | [diff] [blame] | 12 | import argparse |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 13 | import collections |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 14 | import configparser |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 15 | import functools |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 16 | import inspect |
Mike Frysinger | 87c74ce | 2017-04-04 16:12:31 -0400 | [diff] [blame] | 17 | import json |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 18 | from pathlib import Path |
Vadim Bendebury | dcfe232 | 2013-05-23 10:54:49 -0700 | [diff] [blame] | 19 | import re |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 20 | import shlex |
Mike Frysinger | 87c74ce | 2017-04-04 16:12:31 -0400 | [diff] [blame] | 21 | import sys |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 22 | |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 23 | from chromite.lib import chromite_config |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 24 | from chromite.lib import config_lib |
| 25 | from chromite.lib import constants |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 26 | from chromite.lib import commandline |
| 27 | from chromite.lib import cros_build_lib |
Ralph Nathan | 446aee9 | 2015-03-23 14:44:56 -0700 | [diff] [blame] | 28 | from chromite.lib import cros_logging as logging |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 29 | from chromite.lib import gerrit |
Mike Frysinger | c85d816 | 2014-02-08 00:45:21 -0500 | [diff] [blame] | 30 | from chromite.lib import gob_util |
Mike Frysinger | 254f33f | 2019-12-11 13:54:29 -0500 | [diff] [blame] | 31 | from chromite.lib import parallel |
Mike Frysinger | 7f2018d | 2021-02-04 00:10:58 -0500 | [diff] [blame] | 32 | from chromite.lib import pformat |
Mike Frysinger | a9751c9 | 2021-04-30 10:12:37 -0400 | [diff] [blame] | 33 | from chromite.lib import retry_util |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 34 | from chromite.lib import terminal |
Mike Frysinger | 479f119 | 2017-09-14 22:36:30 -0400 | [diff] [blame] | 35 | from chromite.lib import uri_lib |
Alex Klein | 337fee4 | 2019-07-08 11:38:26 -0600 | [diff] [blame] | 36 | from chromite.utils import memoize |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 37 | |
| 38 | |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 39 | class Config: |
| 40 | """Manage the user's gerrit config settings. |
| 41 | |
| 42 | This is entirely unique to this gerrit command. Inspiration for naming and |
| 43 | layout is taken from ~/.gitconfig settings. |
| 44 | """ |
| 45 | |
| 46 | def __init__(self, path: Path = chromite_config.GERRIT_CONFIG): |
| 47 | self.cfg = configparser.ConfigParser(interpolation=None) |
| 48 | if path.exists(): |
| 49 | self.cfg.read(chromite_config.GERRIT_CONFIG) |
| 50 | |
| 51 | def expand_alias(self, action): |
| 52 | """Expand any aliases.""" |
| 53 | alias = self.cfg.get('alias', action, fallback=None) |
| 54 | if alias is not None: |
| 55 | return shlex.split(alias) |
| 56 | return action |
| 57 | |
| 58 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 59 | class UserAction(object): |
| 60 | """Base class for all custom user actions.""" |
| 61 | |
| 62 | # The name of the command the user types in. |
| 63 | COMMAND = None |
| 64 | |
| 65 | @staticmethod |
| 66 | def init_subparser(parser): |
| 67 | """Add arguments to this action's subparser.""" |
| 68 | |
| 69 | @staticmethod |
| 70 | def __call__(opts): |
| 71 | """Implement the action.""" |
Mike Frysinger | 62178ae | 2020-03-20 01:37:43 -0400 | [diff] [blame] | 72 | raise RuntimeError('Internal error: action missing __call__ implementation') |
Mike Frysinger | 108eda2 | 2018-06-06 18:45:12 -0400 | [diff] [blame] | 73 | |
| 74 | |
Mike Frysinger | 254f33f | 2019-12-11 13:54:29 -0500 | [diff] [blame] | 75 | # How many connections we'll use in parallel. We don't want this to be too high |
| 76 | # so we don't go over our per-user quota. Pick 10 somewhat arbitrarily as that |
| 77 | # seems to be good enough for users. |
| 78 | CONNECTION_LIMIT = 10 |
| 79 | |
| 80 | |
Mike Frysinger | 031ad0b | 2013-05-14 18:15:34 -0400 | [diff] [blame] | 81 | COLOR = None |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 82 | |
| 83 | # Map the internal names to the ones we normally show on the web ui. |
| 84 | GERRIT_APPROVAL_MAP = { |
Vadim Bendebury | 5057183 | 2013-11-12 10:43:19 -0800 | [diff] [blame] | 85 | 'COMR': ['CQ', 'Commit Queue ',], |
| 86 | 'CRVW': ['CR', 'Code Review ',], |
| 87 | 'SUBM': ['S ', 'Submitted ',], |
Vadim Bendebury | 5057183 | 2013-11-12 10:43:19 -0800 | [diff] [blame] | 88 | 'VRIF': ['V ', 'Verified ',], |
Jason D. Clinton | 729f81f | 2019-05-02 20:24:33 -0600 | [diff] [blame] | 89 | 'LCQ': ['L ', 'Legacy ',], |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | # Order is important -- matches the web ui. This also controls the short |
| 93 | # entries that we summarize in non-verbose mode. |
| 94 | GERRIT_SUMMARY_CATS = ('CR', 'CQ', 'V',) |
| 95 | |
Mike Frysinger | 4aea5dc | 2019-07-17 13:39:56 -0400 | [diff] [blame] | 96 | # Shorter strings for CL status messages. |
| 97 | GERRIT_SUMMARY_MAP = { |
| 98 | 'ABANDONED': 'ABD', |
| 99 | 'MERGED': 'MRG', |
| 100 | 'NEW': 'NEW', |
| 101 | 'WIP': 'WIP', |
| 102 | } |
| 103 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 104 | |
| 105 | def red(s): |
| 106 | return COLOR.Color(terminal.Color.RED, s) |
| 107 | |
| 108 | |
| 109 | def green(s): |
| 110 | return COLOR.Color(terminal.Color.GREEN, s) |
| 111 | |
| 112 | |
| 113 | def blue(s): |
| 114 | return COLOR.Color(terminal.Color.BLUE, s) |
| 115 | |
| 116 | |
Mike Frysinger | 254f33f | 2019-12-11 13:54:29 -0500 | [diff] [blame] | 117 | def _run_parallel_tasks(task, *args): |
| 118 | """Small wrapper around BackgroundTaskRunner to enforce job count.""" |
Mike Frysinger | a9751c9 | 2021-04-30 10:12:37 -0400 | [diff] [blame] | 119 | # When we run in parallel, we can hit the max requests limit. |
| 120 | def check_exc(e): |
| 121 | if not isinstance(e, gob_util.GOBError): |
| 122 | raise e |
| 123 | return e.http_status == 429 |
| 124 | |
| 125 | @retry_util.WithRetry(5, handler=check_exc, sleep=1, backoff_factor=2) |
| 126 | def retry(*args): |
| 127 | try: |
| 128 | task(*args) |
| 129 | except gob_util.GOBError as e: |
| 130 | if e.http_status != 429: |
| 131 | logging.warning('%s: skipping due: %s', args, e) |
| 132 | else: |
| 133 | raise |
| 134 | |
| 135 | with parallel.BackgroundTaskRunner(retry, processes=CONNECTION_LIMIT) as q: |
Mike Frysinger | 254f33f | 2019-12-11 13:54:29 -0500 | [diff] [blame] | 136 | for arg in args: |
| 137 | q.put([arg]) |
| 138 | |
| 139 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 140 | def limits(cls): |
| 141 | """Given a dict of fields, calculate the longest string lengths |
| 142 | |
| 143 | This allows you to easily format the output of many results so that the |
| 144 | various cols all line up correctly. |
| 145 | """ |
| 146 | lims = {} |
| 147 | for cl in cls: |
| 148 | for k in cl.keys(): |
Mike Frysinger | f16b8f0 | 2013-10-21 22:24:46 -0400 | [diff] [blame] | 149 | # Use %s rather than str() to avoid codec issues. |
| 150 | # We also do this so we can format integers. |
| 151 | lims[k] = max(lims.get(k, 0), len('%s' % cl[k])) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 152 | return lims |
| 153 | |
| 154 | |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 155 | # TODO: This func really needs to be merged into the core gerrit logic. |
| 156 | def GetGerrit(opts, cl=None): |
| 157 | """Auto pick the right gerrit instance based on the |cl| |
| 158 | |
| 159 | Args: |
| 160 | opts: The general options object. |
| 161 | cl: A CL taking one of the forms: 1234 *1234 chromium:1234 |
| 162 | |
| 163 | Returns: |
| 164 | A tuple of a gerrit object and a sanitized CL #. |
| 165 | """ |
| 166 | gob = opts.gob |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 167 | if cl is not None: |
Jason D. Clinton | eb1073d | 2019-04-13 02:33:20 -0600 | [diff] [blame] | 168 | if cl.startswith('*') or cl.startswith('chrome-internal:'): |
Alex Klein | 2ab29cc | 2018-07-19 12:01:00 -0600 | [diff] [blame] | 169 | gob = config_lib.GetSiteParams().INTERNAL_GOB_INSTANCE |
Jason D. Clinton | eb1073d | 2019-04-13 02:33:20 -0600 | [diff] [blame] | 170 | if cl.startswith('*'): |
| 171 | cl = cl[1:] |
| 172 | else: |
| 173 | cl = cl[16:] |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 174 | elif ':' in cl: |
| 175 | gob, cl = cl.split(':', 1) |
| 176 | |
| 177 | if not gob in opts.gerrit: |
| 178 | opts.gerrit[gob] = gerrit.GetGerritHelper(gob=gob, print_cmd=opts.debug) |
| 179 | |
| 180 | return (opts.gerrit[gob], cl) |
| 181 | |
| 182 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 183 | def GetApprovalSummary(_opts, cls): |
| 184 | """Return a dict of the most important approvals""" |
| 185 | approvs = dict([(x, '') for x in GERRIT_SUMMARY_CATS]) |
Aviv Keshet | ad30cec | 2018-09-27 18:12:15 -0700 | [diff] [blame] | 186 | for approver in cls.get('currentPatchSet', {}).get('approvals', []): |
| 187 | cats = GERRIT_APPROVAL_MAP.get(approver['type']) |
| 188 | if not cats: |
| 189 | logging.warning('unknown gerrit approval type: %s', approver['type']) |
| 190 | continue |
| 191 | cat = cats[0].strip() |
| 192 | val = int(approver['value']) |
| 193 | if not cat in approvs: |
| 194 | # Ignore the extended categories in the summary view. |
| 195 | continue |
| 196 | elif approvs[cat] == '': |
| 197 | approvs[cat] = val |
| 198 | elif val < 0: |
| 199 | approvs[cat] = min(approvs[cat], val) |
| 200 | else: |
| 201 | approvs[cat] = max(approvs[cat], val) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 202 | return approvs |
| 203 | |
| 204 | |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 205 | def PrettyPrintCl(opts, cl, lims=None, show_approvals=True): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 206 | """Pretty print a single result""" |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 207 | if lims is None: |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 208 | lims = {'url': 0, 'project': 0} |
| 209 | |
| 210 | status = '' |
Mike Frysinger | 4aea5dc | 2019-07-17 13:39:56 -0400 | [diff] [blame] | 211 | |
| 212 | if opts.verbose: |
| 213 | status += '%s ' % (cl['status'],) |
| 214 | else: |
| 215 | status += '%s ' % (GERRIT_SUMMARY_MAP.get(cl['status'], cl['status']),) |
| 216 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 217 | if show_approvals and not opts.verbose: |
Mike Frysinger | b4a3e3c | 2017-04-05 16:06:53 -0400 | [diff] [blame] | 218 | approvs = GetApprovalSummary(opts, cl) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 219 | for cat in GERRIT_SUMMARY_CATS: |
Mike Frysinger | a0313d0 | 2017-07-10 16:44:43 -0400 | [diff] [blame] | 220 | if approvs[cat] in ('', 0): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 221 | functor = lambda x: x |
| 222 | elif approvs[cat] < 0: |
| 223 | functor = red |
| 224 | else: |
| 225 | functor = green |
| 226 | status += functor('%s:%2s ' % (cat, approvs[cat])) |
| 227 | |
Mike Frysinger | b4a3e3c | 2017-04-05 16:06:53 -0400 | [diff] [blame] | 228 | print('%s %s%-*s %s' % (blue('%-*s' % (lims['url'], cl['url'])), status, |
| 229 | lims['project'], cl['project'], cl['subject'])) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 230 | |
| 231 | if show_approvals and opts.verbose: |
Mike Frysinger | b4a3e3c | 2017-04-05 16:06:53 -0400 | [diff] [blame] | 232 | for approver in cl['currentPatchSet'].get('approvals', []): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 233 | functor = red if int(approver['value']) < 0 else green |
| 234 | n = functor('%2s' % approver['value']) |
| 235 | t = GERRIT_APPROVAL_MAP.get(approver['type'], [approver['type'], |
| 236 | approver['type']])[1] |
Mike Frysinger | 31ff6f9 | 2014-02-08 04:33:03 -0500 | [diff] [blame] | 237 | print(' %s %s %s' % (n, t, approver['by']['email'])) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 238 | |
| 239 | |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 240 | def PrintCls(opts, cls, lims=None, show_approvals=True): |
Mike Frysinger | 87c74ce | 2017-04-04 16:12:31 -0400 | [diff] [blame] | 241 | """Print all results based on the requested format.""" |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 242 | if opts.raw: |
Alex Klein | 2ab29cc | 2018-07-19 12:01:00 -0600 | [diff] [blame] | 243 | site_params = config_lib.GetSiteParams() |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 244 | pfx = '' |
| 245 | # Special case internal Chrome GoB as that is what most devs use. |
| 246 | # They can always redirect the list elsewhere via the -g option. |
Alex Klein | 2ab29cc | 2018-07-19 12:01:00 -0600 | [diff] [blame] | 247 | if opts.gob == site_params.INTERNAL_GOB_INSTANCE: |
| 248 | pfx = site_params.INTERNAL_CHANGE_PREFIX |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 249 | for cl in cls: |
| 250 | print('%s%s' % (pfx, cl['number'])) |
| 251 | |
Mike Frysinger | 87c74ce | 2017-04-04 16:12:31 -0400 | [diff] [blame] | 252 | elif opts.json: |
| 253 | json.dump(cls, sys.stdout) |
| 254 | |
Mike Frysinger | a1b4b27 | 2017-04-05 16:11:00 -0400 | [diff] [blame] | 255 | else: |
| 256 | if lims is None: |
| 257 | lims = limits(cls) |
| 258 | |
| 259 | for cl in cls: |
| 260 | PrettyPrintCl(opts, cl, lims=lims, show_approvals=show_approvals) |
| 261 | |
| 262 | |
Mike Frysinger | 5f938ca | 2017-07-19 18:29:02 -0400 | [diff] [blame] | 263 | def _Query(opts, query, raw=True, helper=None): |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 264 | """Queries Gerrit with a query string built from the commandline options""" |
Vadim Bendebury | 6e057b3 | 2014-12-29 09:41:36 -0800 | [diff] [blame] | 265 | if opts.branch is not None: |
| 266 | query += ' branch:%s' % opts.branch |
Mathieu Olivari | edc45b8 | 2015-01-12 19:43:20 -0800 | [diff] [blame] | 267 | if opts.project is not None: |
| 268 | query += ' project: %s' % opts.project |
Mathieu Olivari | 14645a1 | 2015-01-16 15:41:32 -0800 | [diff] [blame] | 269 | if opts.topic is not None: |
| 270 | query += ' topic: %s' % opts.topic |
Vadim Bendebury | 6e057b3 | 2014-12-29 09:41:36 -0800 | [diff] [blame] | 271 | |
Mike Frysinger | 5f938ca | 2017-07-19 18:29:02 -0400 | [diff] [blame] | 272 | if helper is None: |
| 273 | helper, _ = GetGerrit(opts) |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 274 | return helper.Query(query, raw=raw, bypass_cache=False) |
| 275 | |
| 276 | |
Mike Frysinger | 5f938ca | 2017-07-19 18:29:02 -0400 | [diff] [blame] | 277 | def FilteredQuery(opts, query, helper=None): |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 278 | """Query gerrit and filter/clean up the results""" |
| 279 | ret = [] |
| 280 | |
Mike Frysinger | 2cd5602 | 2017-01-12 20:56:27 -0500 | [diff] [blame] | 281 | logging.debug('Running query: %s', query) |
Mike Frysinger | 5f938ca | 2017-07-19 18:29:02 -0400 | [diff] [blame] | 282 | for cl in _Query(opts, query, raw=True, helper=helper): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 283 | # Gerrit likes to return a stats record too. |
| 284 | if not 'project' in cl: |
| 285 | continue |
| 286 | |
| 287 | # Strip off common leading names since the result is still |
| 288 | # unique over the whole tree. |
| 289 | if not opts.verbose: |
Mike Frysinger | 1d50828 | 2018-06-07 16:59:44 -0400 | [diff] [blame] | 290 | for pfx in ('aosp', 'chromeos', 'chromiumos', 'external', 'overlays', |
| 291 | 'platform', 'third_party'): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 292 | if cl['project'].startswith('%s/' % pfx): |
| 293 | cl['project'] = cl['project'][len(pfx) + 1:] |
| 294 | |
Mike Frysinger | 479f119 | 2017-09-14 22:36:30 -0400 | [diff] [blame] | 295 | cl['url'] = uri_lib.ShortenUri(cl['url']) |
| 296 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 297 | ret.append(cl) |
| 298 | |
Mike Frysinger | b62313a | 2017-06-30 16:38:58 -0400 | [diff] [blame] | 299 | if opts.sort == 'unsorted': |
| 300 | return ret |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 301 | if opts.sort == 'number': |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 302 | key = lambda x: int(x[opts.sort]) |
| 303 | else: |
| 304 | key = lambda x: x[opts.sort] |
| 305 | return sorted(ret, key=key) |
| 306 | |
| 307 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 308 | class _ActionSearchQuery(UserAction): |
| 309 | """Base class for actions that perform searches.""" |
| 310 | |
| 311 | @staticmethod |
| 312 | def init_subparser(parser): |
| 313 | """Add arguments to this action's subparser.""" |
| 314 | parser.add_argument('--sort', default='number', |
| 315 | help='Key to sort on (number, project); use "unsorted" ' |
| 316 | 'to disable') |
| 317 | parser.add_argument('-b', '--branch', |
| 318 | help='Limit output to the specific branch') |
| 319 | parser.add_argument('-p', '--project', |
| 320 | help='Limit output to the specific project') |
| 321 | parser.add_argument('-t', '--topic', |
| 322 | help='Limit output to the specific topic') |
| 323 | |
| 324 | |
| 325 | class ActionTodo(_ActionSearchQuery): |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 326 | """List CLs needing your review""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 327 | |
| 328 | COMMAND = 'todo' |
| 329 | |
| 330 | @staticmethod |
| 331 | def __call__(opts): |
| 332 | """Implement the action.""" |
Mike Frysinger | 242d292 | 2021-02-09 14:31:50 -0500 | [diff] [blame] | 333 | cls = FilteredQuery(opts, 'attention:self') |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 334 | PrintCls(opts, cls) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 335 | |
| 336 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 337 | class ActionSearch(_ActionSearchQuery): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 338 | """List CLs matching the search query""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 339 | |
| 340 | COMMAND = 'search' |
| 341 | |
| 342 | @staticmethod |
| 343 | def init_subparser(parser): |
| 344 | """Add arguments to this action's subparser.""" |
| 345 | _ActionSearchQuery.init_subparser(parser) |
| 346 | parser.add_argument('query', |
| 347 | help='The search query') |
| 348 | |
| 349 | @staticmethod |
| 350 | def __call__(opts): |
| 351 | """Implement the action.""" |
| 352 | cls = FilteredQuery(opts, opts.query) |
| 353 | PrintCls(opts, cls) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 354 | |
| 355 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 356 | class ActionMine(_ActionSearchQuery): |
Mike Frysinger | a1db2c4 | 2014-06-15 00:42:48 -0700 | [diff] [blame] | 357 | """List your CLs with review statuses""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 358 | |
| 359 | COMMAND = 'mine' |
| 360 | |
| 361 | @staticmethod |
| 362 | def init_subparser(parser): |
| 363 | """Add arguments to this action's subparser.""" |
| 364 | _ActionSearchQuery.init_subparser(parser) |
| 365 | parser.add_argument('--draft', default=False, action='store_true', |
| 366 | help='Show draft changes') |
| 367 | |
| 368 | @staticmethod |
| 369 | def __call__(opts): |
| 370 | """Implement the action.""" |
| 371 | if opts.draft: |
| 372 | rule = 'is:draft' |
| 373 | else: |
| 374 | rule = 'status:new' |
| 375 | cls = FilteredQuery(opts, 'owner:self %s' % (rule,)) |
| 376 | PrintCls(opts, cls) |
Mike Frysinger | a1db2c4 | 2014-06-15 00:42:48 -0700 | [diff] [blame] | 377 | |
| 378 | |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 379 | def _BreadthFirstSearch(to_visit, children, visited_key=lambda x: x): |
| 380 | """Runs breadth first search starting from the nodes in |to_visit| |
| 381 | |
| 382 | Args: |
| 383 | to_visit: the starting nodes |
| 384 | children: a function which takes a node and returns the nodes adjacent to it |
| 385 | visited_key: a function for deduplicating node visits. Defaults to the |
| 386 | identity function (lambda x: x) |
| 387 | |
| 388 | Returns: |
| 389 | A list of nodes which are reachable from any node in |to_visit| by calling |
| 390 | |children| any number of times. |
| 391 | """ |
| 392 | to_visit = list(to_visit) |
Mike Frysinger | 66ce413 | 2019-07-17 22:52:52 -0400 | [diff] [blame] | 393 | seen = set(visited_key(x) for x in to_visit) |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 394 | for node in to_visit: |
| 395 | for child in children(node): |
| 396 | key = visited_key(child) |
| 397 | if key not in seen: |
| 398 | seen.add(key) |
| 399 | to_visit.append(child) |
| 400 | return to_visit |
| 401 | |
| 402 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 403 | class ActionDeps(_ActionSearchQuery): |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 404 | """List CLs matching a query, and all transitive dependencies of those CLs""" |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 405 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 406 | COMMAND = 'deps' |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 407 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 408 | @staticmethod |
| 409 | def init_subparser(parser): |
| 410 | """Add arguments to this action's subparser.""" |
| 411 | _ActionSearchQuery.init_subparser(parser) |
| 412 | parser.add_argument('query', |
| 413 | help='The search query') |
| 414 | |
| 415 | def __call__(self, opts): |
| 416 | """Implement the action.""" |
| 417 | cls = _Query(opts, opts.query, raw=False) |
| 418 | |
| 419 | @memoize.Memoize |
| 420 | def _QueryChange(cl, helper=None): |
| 421 | return _Query(opts, cl, raw=False, helper=helper) |
| 422 | |
| 423 | transitives = _BreadthFirstSearch( |
Mike Nichols | a141416 | 2021-04-22 20:07:22 +0000 | [diff] [blame] | 424 | cls, functools.partial(self._Children, opts, _QueryChange), |
Mike Frysinger | dc407f5 | 2020-05-08 00:34:56 -0400 | [diff] [blame] | 425 | visited_key=lambda cl: cl.PatchLink()) |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 426 | |
Mike Frysinger | dc407f5 | 2020-05-08 00:34:56 -0400 | [diff] [blame] | 427 | # This is a hack to avoid losing GoB host for each CL. The PrintCls |
| 428 | # function assumes the GoB host specified by the user is the only one |
| 429 | # that is ever used, but the deps command walks across hosts. |
| 430 | if opts.raw: |
| 431 | print('\n'.join(x.PatchLink() for x in transitives)) |
| 432 | else: |
| 433 | transitives_raw = [cl.patch_dict for cl in transitives] |
| 434 | PrintCls(opts, transitives_raw) |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 435 | |
| 436 | @staticmethod |
| 437 | def _ProcessDeps(opts, querier, cl, deps, required): |
Mike Frysinger | 5726da9 | 2017-09-20 22:14:25 -0400 | [diff] [blame] | 438 | """Yields matching dependencies for a patch""" |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 439 | # We need to query the change to guarantee that we have a .gerrit_number |
Mike Frysinger | 5726da9 | 2017-09-20 22:14:25 -0400 | [diff] [blame] | 440 | for dep in deps: |
Mike Frysinger | b3300c4 | 2017-07-20 01:41:17 -0400 | [diff] [blame] | 441 | if not dep.remote in opts.gerrit: |
| 442 | opts.gerrit[dep.remote] = gerrit.GetGerritHelper( |
| 443 | remote=dep.remote, print_cmd=opts.debug) |
| 444 | helper = opts.gerrit[dep.remote] |
| 445 | |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 446 | # TODO(phobbs) this should maybe catch network errors. |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 447 | changes = querier(dep.ToGerritQueryText(), helper=helper) |
Mike Frysinger | 5726da9 | 2017-09-20 22:14:25 -0400 | [diff] [blame] | 448 | |
| 449 | # Handle empty results. If we found a commit that was pushed directly |
| 450 | # (e.g. a bot commit), then gerrit won't know about it. |
| 451 | if not changes: |
| 452 | if required: |
| 453 | logging.error('CL %s depends on %s which cannot be found', |
| 454 | cl, dep.ToGerritQueryText()) |
| 455 | continue |
| 456 | |
| 457 | # Our query might have matched more than one result. This can come up |
| 458 | # when CQ-DEPEND uses a Gerrit Change-Id, but that Change-Id shows up |
| 459 | # across multiple repos/branches. We blindly check all of them in the |
| 460 | # hopes that all open ones are what the user wants, but then again the |
| 461 | # CQ-DEPEND syntax itself is unable to differeniate. *shrug* |
| 462 | if len(changes) > 1: |
| 463 | logging.warning('CL %s has an ambiguous CQ dependency %s', |
| 464 | cl, dep.ToGerritQueryText()) |
| 465 | for change in changes: |
| 466 | if change.status == 'NEW': |
| 467 | yield change |
| 468 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 469 | @classmethod |
Mike Nichols | a141416 | 2021-04-22 20:07:22 +0000 | [diff] [blame] | 470 | def _Children(cls, opts, querier, cl): |
Mike Frysinger | 7cbd88c | 2021-02-12 03:52:25 -0500 | [diff] [blame] | 471 | """Yields the Gerrit dependencies of a patch""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 472 | for change in cls._ProcessDeps( |
| 473 | opts, querier, cl, cl.GerritDependencies(), False): |
Mike Frysinger | 5726da9 | 2017-09-20 22:14:25 -0400 | [diff] [blame] | 474 | yield change |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 475 | |
Paul Hobbs | 8976523 | 2015-06-24 14:07:49 -0700 | [diff] [blame] | 476 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 477 | class ActionInspect(_ActionSearchQuery): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 478 | """Show the details of one or more CLs""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 479 | |
| 480 | COMMAND = 'inspect' |
| 481 | |
| 482 | @staticmethod |
| 483 | def init_subparser(parser): |
| 484 | """Add arguments to this action's subparser.""" |
| 485 | _ActionSearchQuery.init_subparser(parser) |
| 486 | parser.add_argument('cls', nargs='+', metavar='CL', |
| 487 | help='The CL(s) to update') |
| 488 | |
| 489 | @staticmethod |
| 490 | def __call__(opts): |
| 491 | """Implement the action.""" |
| 492 | cls = [] |
| 493 | for arg in opts.cls: |
| 494 | helper, cl = GetGerrit(opts, arg) |
| 495 | change = FilteredQuery(opts, 'change:%s' % cl, helper=helper) |
| 496 | if change: |
| 497 | cls.extend(change) |
| 498 | else: |
| 499 | logging.warning('no results found for CL %s', arg) |
| 500 | PrintCls(opts, cls) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 501 | |
| 502 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 503 | class _ActionLabeler(UserAction): |
| 504 | """Base helper for setting labels.""" |
| 505 | |
| 506 | LABEL = None |
| 507 | VALUES = None |
| 508 | |
| 509 | @classmethod |
| 510 | def init_subparser(cls, parser): |
| 511 | """Add arguments to this action's subparser.""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 512 | parser.add_argument('-m', '--msg', '--message', metavar='MESSAGE', |
| 513 | help='Optional message to include') |
| 514 | parser.add_argument('cls', nargs='+', metavar='CL', |
| 515 | help='The CL(s) to update') |
| 516 | parser.add_argument('value', nargs=1, metavar='value', choices=cls.VALUES, |
| 517 | help='The label value; one of [%(choices)s]') |
| 518 | |
| 519 | @classmethod |
| 520 | def __call__(cls, opts): |
| 521 | """Implement the action.""" |
| 522 | # Convert user friendly command line option into a gerrit parameter. |
| 523 | def task(arg): |
| 524 | helper, cl = GetGerrit(opts, arg) |
| 525 | helper.SetReview(cl, labels={cls.LABEL: opts.value[0]}, msg=opts.msg, |
| 526 | dryrun=opts.dryrun, notify=opts.notify) |
| 527 | _run_parallel_tasks(task, *opts.cls) |
| 528 | |
| 529 | |
| 530 | class ActionLabelAutoSubmit(_ActionLabeler): |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 531 | """Change the Auto-Submit label""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 532 | |
| 533 | COMMAND = 'label-as' |
| 534 | LABEL = 'Auto-Submit' |
| 535 | VALUES = ('0', '1') |
Jack Rosenthal | 8a1fb54 | 2019-08-07 10:23:56 -0600 | [diff] [blame] | 536 | |
| 537 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 538 | class ActionLabelCodeReview(_ActionLabeler): |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 539 | """Change the Code-Review label (1=LGTM 2=LGTM+Approved)""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 540 | |
| 541 | COMMAND = 'label-cr' |
| 542 | LABEL = 'Code-Review' |
| 543 | VALUES = ('-2', '-1', '0', '1', '2') |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 544 | |
| 545 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 546 | class ActionLabelVerified(_ActionLabeler): |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 547 | """Change the Verified label""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 548 | |
| 549 | COMMAND = 'label-v' |
| 550 | LABEL = 'Verified' |
| 551 | VALUES = ('-1', '0', '1') |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 552 | |
| 553 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 554 | class ActionLabelCommitQueue(_ActionLabeler): |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 555 | """Change the Commit-Queue label (1=dry-run 2=commit)""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 556 | |
| 557 | COMMAND = 'label-cq' |
| 558 | LABEL = 'Commit-Queue' |
| 559 | VALUES = ('0', '1', '2') |
Mike Frysinger | 15b23e4 | 2014-12-05 17:00:05 -0500 | [diff] [blame] | 560 | |
| 561 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 562 | class _ActionSimpleParallelCLs(UserAction): |
| 563 | """Base helper for actions that only accept CLs.""" |
| 564 | |
| 565 | @staticmethod |
| 566 | def init_subparser(parser): |
| 567 | """Add arguments to this action's subparser.""" |
| 568 | parser.add_argument('cls', nargs='+', metavar='CL', |
| 569 | help='The CL(s) to update') |
| 570 | |
| 571 | def __call__(self, opts): |
| 572 | """Implement the action.""" |
| 573 | def task(arg): |
| 574 | helper, cl = GetGerrit(opts, arg) |
| 575 | self._process_one(helper, cl, opts) |
| 576 | _run_parallel_tasks(task, *opts.cls) |
| 577 | |
| 578 | |
| 579 | class ActionSubmit(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 580 | """Submit CLs""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 581 | |
| 582 | COMMAND = 'submit' |
| 583 | |
| 584 | @staticmethod |
| 585 | def _process_one(helper, cl, opts): |
| 586 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 8674a11 | 2021-02-09 14:44:17 -0500 | [diff] [blame] | 587 | helper.SubmitChange(cl, dryrun=opts.dryrun, notify=opts.notify) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 588 | |
| 589 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 590 | class ActionAbandon(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 591 | """Abandon CLs""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 592 | |
| 593 | COMMAND = 'abandon' |
| 594 | |
| 595 | @staticmethod |
Mike Frysinger | 3af378b | 2021-03-12 01:34:04 -0500 | [diff] [blame] | 596 | def init_subparser(parser): |
| 597 | """Add arguments to this action's subparser.""" |
| 598 | parser.add_argument('-m', '--msg', '--message', metavar='MESSAGE', |
| 599 | help='Include a message') |
| 600 | _ActionSimpleParallelCLs.init_subparser(parser) |
| 601 | |
| 602 | @staticmethod |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 603 | def _process_one(helper, cl, opts): |
| 604 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 3af378b | 2021-03-12 01:34:04 -0500 | [diff] [blame] | 605 | helper.AbandonChange(cl, msg=opts.msg, dryrun=opts.dryrun, |
| 606 | notify=opts.notify) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 607 | |
| 608 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 609 | class ActionRestore(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 610 | """Restore CLs that were abandoned""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 611 | |
| 612 | COMMAND = 'restore' |
| 613 | |
| 614 | @staticmethod |
| 615 | def _process_one(helper, cl, opts): |
| 616 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 617 | helper.RestoreChange(cl, dryrun=opts.dryrun) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 618 | |
| 619 | |
Tomasz Figa | 54d7099 | 2021-01-20 13:48:59 +0900 | [diff] [blame] | 620 | class ActionWorkInProgress(_ActionSimpleParallelCLs): |
| 621 | """Mark CLs as work in progress""" |
| 622 | |
| 623 | COMMAND = 'wip' |
| 624 | |
| 625 | @staticmethod |
| 626 | def _process_one(helper, cl, opts): |
| 627 | """Use |helper| to process the single |cl|.""" |
| 628 | helper.SetWorkInProgress(cl, True, dryrun=opts.dryrun) |
| 629 | |
| 630 | |
| 631 | class ActionReadyForReview(_ActionSimpleParallelCLs): |
| 632 | """Mark CLs as ready for review""" |
| 633 | |
| 634 | COMMAND = 'ready' |
| 635 | |
| 636 | @staticmethod |
| 637 | def _process_one(helper, cl, opts): |
| 638 | """Use |helper| to process the single |cl|.""" |
| 639 | helper.SetWorkInProgress(cl, False, dryrun=opts.dryrun) |
| 640 | |
| 641 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 642 | class ActionReviewers(UserAction): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 643 | """Add/remove reviewers' emails for a CL (prepend with '~' to remove)""" |
Vadim Bendebury | dcfe232 | 2013-05-23 10:54:49 -0700 | [diff] [blame] | 644 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 645 | COMMAND = 'reviewers' |
Vadim Bendebury | dcfe232 | 2013-05-23 10:54:49 -0700 | [diff] [blame] | 646 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 647 | @staticmethod |
| 648 | def init_subparser(parser): |
| 649 | """Add arguments to this action's subparser.""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 650 | parser.add_argument('cl', metavar='CL', |
| 651 | help='The CL to update') |
| 652 | parser.add_argument('reviewers', nargs='+', |
| 653 | help='The reviewers to add/remove') |
Vadim Bendebury | dcfe232 | 2013-05-23 10:54:49 -0700 | [diff] [blame] | 654 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 655 | @staticmethod |
| 656 | def __call__(opts): |
| 657 | """Implement the action.""" |
| 658 | # Allow for optional leading '~'. |
| 659 | email_validator = re.compile(r'^[~]?%s$' % constants.EMAIL_REGEX) |
| 660 | add_list, remove_list, invalid_list = [], [], [] |
| 661 | |
| 662 | for email in opts.reviewers: |
| 663 | if not email_validator.match(email): |
| 664 | invalid_list.append(email) |
| 665 | elif email[0] == '~': |
| 666 | remove_list.append(email[1:]) |
| 667 | else: |
| 668 | add_list.append(email) |
| 669 | |
| 670 | if invalid_list: |
| 671 | cros_build_lib.Die( |
| 672 | 'Invalid email address(es): %s' % ', '.join(invalid_list)) |
| 673 | |
| 674 | if add_list or remove_list: |
| 675 | helper, cl = GetGerrit(opts, opts.cl) |
| 676 | helper.SetReviewers(cl, add=add_list, remove=remove_list, |
| 677 | dryrun=opts.dryrun, notify=opts.notify) |
Vadim Bendebury | dcfe232 | 2013-05-23 10:54:49 -0700 | [diff] [blame] | 678 | |
| 679 | |
Mike Frysinger | 62178ae | 2020-03-20 01:37:43 -0400 | [diff] [blame] | 680 | class ActionMessage(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 681 | """Add a message to a CL""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 682 | |
| 683 | COMMAND = 'message' |
| 684 | |
| 685 | @staticmethod |
| 686 | def init_subparser(parser): |
| 687 | """Add arguments to this action's subparser.""" |
Mike Frysinger | 62178ae | 2020-03-20 01:37:43 -0400 | [diff] [blame] | 688 | _ActionSimpleParallelCLs.init_subparser(parser) |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 689 | parser.add_argument('message', |
| 690 | help='The message to post') |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 691 | |
| 692 | @staticmethod |
| 693 | def _process_one(helper, cl, opts): |
| 694 | """Use |helper| to process the single |cl|.""" |
| 695 | helper.SetReview(cl, msg=opts.message, dryrun=opts.dryrun) |
Doug Anderson | 8119df0 | 2013-07-20 21:00:24 +0530 | [diff] [blame] | 696 | |
| 697 | |
Mike Frysinger | 62178ae | 2020-03-20 01:37:43 -0400 | [diff] [blame] | 698 | class ActionTopic(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 699 | """Set a topic for one or more CLs""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 700 | |
| 701 | COMMAND = 'topic' |
| 702 | |
| 703 | @staticmethod |
| 704 | def init_subparser(parser): |
| 705 | """Add arguments to this action's subparser.""" |
Mike Frysinger | 62178ae | 2020-03-20 01:37:43 -0400 | [diff] [blame] | 706 | _ActionSimpleParallelCLs.init_subparser(parser) |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 707 | parser.add_argument('topic', |
| 708 | help='The topic to set') |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 709 | |
| 710 | @staticmethod |
| 711 | def _process_one(helper, cl, opts): |
| 712 | """Use |helper| to process the single |cl|.""" |
| 713 | helper.SetTopic(cl, opts.topic, dryrun=opts.dryrun) |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 714 | |
Mathieu Olivari | 02f89b3 | 2015-01-09 13:53:38 -0800 | [diff] [blame] | 715 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 716 | class ActionPrivate(_ActionSimpleParallelCLs): |
| 717 | """Mark CLs private""" |
Prathmesh Prabhu | 871e777 | 2018-03-28 17:11:29 -0700 | [diff] [blame] | 718 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 719 | COMMAND = 'private' |
| 720 | |
| 721 | @staticmethod |
| 722 | def _process_one(helper, cl, opts): |
| 723 | """Use |helper| to process the single |cl|.""" |
| 724 | helper.SetPrivate(cl, True, dryrun=opts.dryrun) |
Prathmesh Prabhu | 871e777 | 2018-03-28 17:11:29 -0700 | [diff] [blame] | 725 | |
Mathieu Olivari | 02f89b3 | 2015-01-09 13:53:38 -0800 | [diff] [blame] | 726 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 727 | class ActionPublic(_ActionSimpleParallelCLs): |
| 728 | """Mark CLs public""" |
| 729 | |
| 730 | COMMAND = 'public' |
| 731 | |
| 732 | @staticmethod |
| 733 | def _process_one(helper, cl, opts): |
| 734 | """Use |helper| to process the single |cl|.""" |
| 735 | helper.SetPrivate(cl, False, dryrun=opts.dryrun) |
| 736 | |
| 737 | |
| 738 | class ActionSethashtags(UserAction): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 739 | """Add/remove hashtags on a CL (prepend with '~' to remove)""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 740 | |
| 741 | COMMAND = 'hashtags' |
| 742 | |
| 743 | @staticmethod |
| 744 | def init_subparser(parser): |
| 745 | """Add arguments to this action's subparser.""" |
| 746 | parser.add_argument('cl', metavar='CL', |
| 747 | help='The CL to update') |
| 748 | parser.add_argument('hashtags', nargs='+', |
| 749 | help='The hashtags to add/remove') |
| 750 | |
| 751 | @staticmethod |
| 752 | def __call__(opts): |
| 753 | """Implement the action.""" |
| 754 | add = [] |
| 755 | remove = [] |
| 756 | for hashtag in opts.hashtags: |
| 757 | if hashtag.startswith('~'): |
| 758 | remove.append(hashtag[1:]) |
| 759 | else: |
| 760 | add.append(hashtag) |
| 761 | helper, cl = GetGerrit(opts, opts.cl) |
| 762 | helper.SetHashtags(cl, add, remove, dryrun=opts.dryrun) |
Wei-Han Chen | b4c9af5 | 2017-02-09 14:43:22 +0800 | [diff] [blame] | 763 | |
| 764 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 765 | class ActionDeletedraft(_ActionSimpleParallelCLs): |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 766 | """Delete draft CLs""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 767 | |
| 768 | COMMAND = 'deletedraft' |
| 769 | |
| 770 | @staticmethod |
| 771 | def _process_one(helper, cl, opts): |
| 772 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 773 | helper.DeleteDraft(cl, dryrun=opts.dryrun) |
Jon Salz | a427fb0 | 2014-03-07 18:13:17 +0800 | [diff] [blame] | 774 | |
| 775 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 776 | class ActionReviewed(_ActionSimpleParallelCLs): |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 777 | """Mark CLs as reviewed""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 778 | |
| 779 | COMMAND = 'reviewed' |
| 780 | |
| 781 | @staticmethod |
| 782 | def _process_one(helper, cl, opts): |
| 783 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 784 | helper.ReviewedChange(cl, dryrun=opts.dryrun) |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 785 | |
| 786 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 787 | class ActionUnreviewed(_ActionSimpleParallelCLs): |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 788 | """Mark CLs as unreviewed""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 789 | |
| 790 | COMMAND = 'unreviewed' |
| 791 | |
| 792 | @staticmethod |
| 793 | def _process_one(helper, cl, opts): |
| 794 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 795 | helper.UnreviewedChange(cl, dryrun=opts.dryrun) |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 796 | |
| 797 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 798 | class ActionIgnore(_ActionSimpleParallelCLs): |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 799 | """Ignore CLs (suppress notifications/dashboard/etc...)""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 800 | |
| 801 | COMMAND = 'ignore' |
| 802 | |
| 803 | @staticmethod |
| 804 | def _process_one(helper, cl, opts): |
| 805 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 806 | helper.IgnoreChange(cl, dryrun=opts.dryrun) |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 807 | |
| 808 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 809 | class ActionUnignore(_ActionSimpleParallelCLs): |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 810 | """Unignore CLs (enable notifications/dashboard/etc...)""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 811 | |
| 812 | COMMAND = 'unignore' |
| 813 | |
| 814 | @staticmethod |
| 815 | def _process_one(helper, cl, opts): |
| 816 | """Use |helper| to process the single |cl|.""" |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 817 | helper.UnignoreChange(cl, dryrun=opts.dryrun) |
Mike Frysinger | 6f04dd4 | 2020-02-06 16:48:18 -0500 | [diff] [blame] | 818 | |
| 819 | |
Mike Frysinger | 5dab15e | 2020-08-06 10:11:03 -0400 | [diff] [blame] | 820 | class ActionCherryPick(UserAction): |
| 821 | """Cherry pick CLs to branches.""" |
| 822 | |
| 823 | COMMAND = 'cherry-pick' |
| 824 | |
| 825 | @staticmethod |
| 826 | def init_subparser(parser): |
| 827 | """Add arguments to this action's subparser.""" |
| 828 | # Should we add an option to walk Cq-Depend and try to cherry-pick them? |
| 829 | parser.add_argument('--rev', '--revision', default='current', |
| 830 | help='A specific revision or patchset') |
| 831 | parser.add_argument('-m', '--msg', '--message', metavar='MESSAGE', |
| 832 | help='Include a message') |
| 833 | parser.add_argument('--branches', '--branch', '--br', action='split_extend', |
| 834 | default=[], required=True, |
| 835 | help='The destination branches') |
| 836 | parser.add_argument('cls', nargs='+', metavar='CL', |
| 837 | help='The CLs to cherry-pick') |
| 838 | |
| 839 | @staticmethod |
| 840 | def __call__(opts): |
| 841 | """Implement the action.""" |
| 842 | # Process branches in parallel, but CLs in serial in case of CL stacks. |
| 843 | def task(branch): |
| 844 | for arg in opts.cls: |
| 845 | helper, cl = GetGerrit(opts, arg) |
| 846 | ret = helper.CherryPick(cl, branch, rev=opts.rev, msg=opts.msg, |
Mike Frysinger | 8674a11 | 2021-02-09 14:44:17 -0500 | [diff] [blame] | 847 | dryrun=opts.dryrun, notify=opts.notify) |
Mike Frysinger | 5dab15e | 2020-08-06 10:11:03 -0400 | [diff] [blame] | 848 | logging.debug('Response: %s', ret) |
| 849 | if opts.raw: |
| 850 | print(ret['_number']) |
| 851 | else: |
| 852 | uri = f'https://{helper.host}/c/{ret["_number"]}' |
| 853 | print(uri_lib.ShortenUri(uri)) |
| 854 | |
| 855 | _run_parallel_tasks(task, *opts.branches) |
| 856 | |
| 857 | |
Mike Frysinger | 8037f75 | 2020-02-29 20:47:09 -0500 | [diff] [blame] | 858 | class ActionReview(_ActionSimpleParallelCLs): |
| 859 | """Review CLs with multiple settings |
| 860 | |
| 861 | The label option supports extended/multiple syntax for easy use. The --label |
| 862 | option may be specified multiple times (as settings are merges), and multiple |
| 863 | labels are allowed in a single argument. Each label has the form: |
| 864 | <long or short name><=+-><value> |
| 865 | |
| 866 | Common arguments: |
| 867 | Commit-Queue=0 Commit-Queue-1 Commit-Queue+2 CQ+2 |
| 868 | 'V+1 CQ+2' |
| 869 | 'AS=1 V=1' |
| 870 | """ |
| 871 | |
| 872 | COMMAND = 'review' |
| 873 | |
| 874 | class _SetLabel(argparse.Action): |
| 875 | """Argparse action for setting labels.""" |
| 876 | |
| 877 | LABEL_MAP = { |
| 878 | 'AS': 'Auto-Submit', |
| 879 | 'CQ': 'Commit-Queue', |
| 880 | 'CR': 'Code-Review', |
| 881 | 'V': 'Verified', |
| 882 | } |
| 883 | |
| 884 | def __call__(self, parser, namespace, values, option_string=None): |
| 885 | labels = getattr(namespace, self.dest) |
| 886 | for request in values.split(): |
| 887 | if '=' in request: |
| 888 | # Handle Verified=1 form. |
| 889 | short, value = request.split('=', 1) |
| 890 | elif '+' in request: |
| 891 | # Handle Verified+1 form. |
| 892 | short, value = request.split('+', 1) |
| 893 | elif '-' in request: |
| 894 | # Handle Verified-1 form. |
| 895 | short, value = request.split('-', 1) |
| 896 | value = '-%s' % (value,) |
| 897 | else: |
| 898 | parser.error('Invalid label setting "%s". Must be Commit-Queue=1 or ' |
| 899 | 'CQ+1 or CR-1.' % (request,)) |
| 900 | |
| 901 | # Convert possible short label names like "V" to "Verified". |
| 902 | label = self.LABEL_MAP.get(short) |
| 903 | if not label: |
| 904 | label = short |
| 905 | |
| 906 | # We allow existing label requests to be overridden. |
| 907 | labels[label] = value |
| 908 | |
| 909 | @classmethod |
| 910 | def init_subparser(cls, parser): |
| 911 | """Add arguments to this action's subparser.""" |
| 912 | parser.add_argument('-m', '--msg', '--message', metavar='MESSAGE', |
| 913 | help='Include a message') |
| 914 | parser.add_argument('-l', '--label', dest='labels', |
| 915 | action=cls._SetLabel, default={}, |
| 916 | help='Set a label with a value') |
| 917 | parser.add_argument('--ready', default=None, action='store_true', |
| 918 | help='Set CL status to ready-for-review') |
| 919 | parser.add_argument('--wip', default=None, action='store_true', |
| 920 | help='Set CL status to WIP') |
| 921 | parser.add_argument('--reviewers', '--re', action='append', default=[], |
| 922 | help='Add reviewers') |
| 923 | parser.add_argument('--cc', action='append', default=[], |
| 924 | help='Add people to CC') |
| 925 | _ActionSimpleParallelCLs.init_subparser(parser) |
| 926 | |
| 927 | @staticmethod |
| 928 | def _process_one(helper, cl, opts): |
| 929 | """Use |helper| to process the single |cl|.""" |
| 930 | helper.SetReview(cl, msg=opts.msg, labels=opts.labels, dryrun=opts.dryrun, |
| 931 | notify=opts.notify, reviewers=opts.reviewers, cc=opts.cc, |
| 932 | ready=opts.ready, wip=opts.wip) |
| 933 | |
| 934 | |
Mike Frysinger | 7f2018d | 2021-02-04 00:10:58 -0500 | [diff] [blame] | 935 | class ActionAccount(_ActionSimpleParallelCLs): |
| 936 | """Get user account information""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 937 | |
| 938 | COMMAND = 'account' |
| 939 | |
| 940 | @staticmethod |
Mike Frysinger | 7f2018d | 2021-02-04 00:10:58 -0500 | [diff] [blame] | 941 | def init_subparser(parser): |
| 942 | """Add arguments to this action's subparser.""" |
| 943 | parser.add_argument('accounts', nargs='*', default=['self'], |
| 944 | help='The accounts to query') |
| 945 | |
| 946 | @classmethod |
| 947 | def __call__(cls, opts): |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 948 | """Implement the action.""" |
| 949 | helper, _ = GetGerrit(opts) |
Mike Frysinger | 7f2018d | 2021-02-04 00:10:58 -0500 | [diff] [blame] | 950 | |
| 951 | def print_one(header, data): |
| 952 | print(f'### {header}') |
| 953 | print(pformat.json(data, compact=opts.json).rstrip()) |
| 954 | |
| 955 | def task(arg): |
| 956 | detail = gob_util.FetchUrlJson(helper.host, f'accounts/{arg}/detail') |
| 957 | if not detail: |
| 958 | print(f'{arg}: account not found') |
| 959 | else: |
| 960 | print_one('detail', detail) |
| 961 | for field in ('groups', 'capabilities', 'preferences', 'sshkeys', |
| 962 | 'gpgkeys'): |
| 963 | data = gob_util.FetchUrlJson(helper.host, f'accounts/{arg}/{field}') |
| 964 | print_one(field, data) |
| 965 | |
| 966 | _run_parallel_tasks(task, *opts.accounts) |
Yu-Ju Hong | c20d7b3 | 2014-11-18 07:51:11 -0800 | [diff] [blame] | 967 | |
| 968 | |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 969 | class ActionConfig(UserAction): |
| 970 | """Manage the gerrit tool's own config file |
| 971 | |
| 972 | Gerrit may be customized via ~/.config/chromite/gerrit.cfg. |
| 973 | It is an ini file like ~/.gitconfig. See `man git-config` for basic format. |
| 974 | |
| 975 | # Set up subcommand aliases. |
| 976 | [alias] |
| 977 | common-search = search 'is:open project:something/i/care/about' |
| 978 | """ |
| 979 | |
| 980 | COMMAND = 'config' |
| 981 | |
| 982 | @staticmethod |
| 983 | def __call__(opts): |
| 984 | """Implement the action.""" |
| 985 | # For now, this is a place holder for raising visibility for the config file |
| 986 | # and its associated help text documentation. |
| 987 | opts.parser.parse_args(['config', '--help']) |
| 988 | |
| 989 | |
Mike Frysinger | e545060 | 2021-03-08 15:34:17 -0500 | [diff] [blame] | 990 | class ActionHelp(UserAction): |
| 991 | """An alias to --help for CLI symmetry""" |
| 992 | |
| 993 | COMMAND = 'help' |
| 994 | |
| 995 | @staticmethod |
| 996 | def init_subparser(parser): |
| 997 | """Add arguments to this action's subparser.""" |
| 998 | parser.add_argument('command', nargs='?', |
| 999 | help='The command to display.') |
| 1000 | |
| 1001 | @staticmethod |
| 1002 | def __call__(opts): |
| 1003 | """Implement the action.""" |
| 1004 | # Show global help. |
| 1005 | if not opts.command: |
| 1006 | opts.parser.print_help() |
| 1007 | return |
| 1008 | |
| 1009 | opts.parser.parse_args([opts.command, '--help']) |
| 1010 | |
| 1011 | |
Mike Frysinger | 484e2f8 | 2020-03-20 01:41:10 -0400 | [diff] [blame] | 1012 | class ActionHelpAll(UserAction): |
| 1013 | """Show all actions help output at once.""" |
| 1014 | |
| 1015 | COMMAND = 'help-all' |
| 1016 | |
| 1017 | @staticmethod |
| 1018 | def __call__(opts): |
| 1019 | """Implement the action.""" |
| 1020 | first = True |
| 1021 | for action in _GetActions(): |
| 1022 | if first: |
| 1023 | first = False |
| 1024 | else: |
| 1025 | print('\n\n') |
| 1026 | |
| 1027 | try: |
| 1028 | opts.parser.parse_args([action, '--help']) |
| 1029 | except SystemExit: |
| 1030 | pass |
| 1031 | |
| 1032 | |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1033 | @memoize.Memoize |
| 1034 | def _GetActions(): |
| 1035 | """Get all the possible actions we support. |
| 1036 | |
| 1037 | Returns: |
| 1038 | An ordered dictionary mapping the user subcommand (e.g. "foo") to the |
| 1039 | function that implements that command (e.g. UserActFoo). |
| 1040 | """ |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1041 | VALID_NAME = re.compile(r'^[a-z][a-z-]*[a-z]$') |
| 1042 | |
| 1043 | actions = {} |
| 1044 | for cls in globals().values(): |
| 1045 | if (not inspect.isclass(cls) or |
| 1046 | not issubclass(cls, UserAction) or |
| 1047 | not getattr(cls, 'COMMAND', None)): |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1048 | continue |
| 1049 | |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1050 | # Sanity check names for devs adding new commands. Should be quick. |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1051 | cmd = cls.COMMAND |
| 1052 | assert VALID_NAME.match(cmd), '"%s" must match [a-z-]+' % (cmd,) |
| 1053 | assert cmd not in actions, 'multiple "%s" commands found' % (cmd,) |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1054 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1055 | actions[cmd] = cls |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1056 | |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1057 | return collections.OrderedDict(sorted(actions.items())) |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1058 | |
| 1059 | |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1060 | def _GetActionUsages(): |
| 1061 | """Formats a one-line usage and doc message for each action.""" |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1062 | actions = _GetActions() |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1063 | |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1064 | cmds = list(actions.keys()) |
| 1065 | functions = list(actions.values()) |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1066 | usages = [getattr(x, 'usage', '') for x in functions] |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1067 | docs = [x.__doc__.splitlines()[0] for x in functions] |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1068 | |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1069 | cmd_indent = len(max(cmds, key=len)) |
| 1070 | usage_indent = len(max(usages, key=len)) |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1071 | return '\n'.join( |
| 1072 | ' %-*s %-*s : %s' % (cmd_indent, cmd, usage_indent, usage, doc) |
| 1073 | for cmd, usage, doc in zip(cmds, usages, docs) |
| 1074 | ) |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1075 | |
| 1076 | |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 1077 | def _AddCommonOptions(parser, subparser): |
| 1078 | """Add options that should work before & after the subcommand. |
| 1079 | |
| 1080 | Make it easy to do `gerrit --dry-run foo` and `gerrit foo --dry-run`. |
| 1081 | """ |
| 1082 | parser.add_common_argument_to_group( |
| 1083 | subparser, '--ne', '--no-emails', dest='notify', |
| 1084 | default='ALL', action='store_const', const='NONE', |
| 1085 | help='Do not send e-mail notifications') |
| 1086 | parser.add_common_argument_to_group( |
| 1087 | subparser, '-n', '--dry-run', dest='dryrun', |
| 1088 | default=False, action='store_true', |
| 1089 | help='Show what would be done, but do not make changes') |
| 1090 | |
| 1091 | |
| 1092 | def GetBaseParser() -> commandline.ArgumentParser: |
| 1093 | """Returns the common parser (i.e. no subparsers added).""" |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1094 | description = """\ |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1095 | There is no support for doing line-by-line code review via the command line. |
| 1096 | This helps you manage various bits and CL status. |
| 1097 | |
Mike Frysinger | a1db2c4 | 2014-06-15 00:42:48 -0700 | [diff] [blame] | 1098 | For general Gerrit documentation, see: |
| 1099 | https://gerrit-review.googlesource.com/Documentation/ |
| 1100 | The Searching Changes page covers the search query syntax: |
| 1101 | https://gerrit-review.googlesource.com/Documentation/user-search.html |
| 1102 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1103 | Example: |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 1104 | $ gerrit todo # List all the CLs that await your review. |
| 1105 | $ gerrit mine # List all of your open CLs. |
| 1106 | $ gerrit inspect 28123 # Inspect CL 28123 on the public gerrit. |
| 1107 | $ gerrit inspect *28123 # Inspect CL 28123 on the internal gerrit. |
| 1108 | $ gerrit label-v 28123 1 # Mark CL 28123 as verified (+1). |
Harry Cutts | de9b32c | 2019-02-21 15:25:35 -0800 | [diff] [blame] | 1109 | $ gerrit reviewers 28123 foo@chromium.org # Add foo@ as a reviewer on CL \ |
| 1110 | 28123. |
| 1111 | $ gerrit reviewers 28123 ~foo@chromium.org # Remove foo@ as a reviewer on \ |
| 1112 | CL 28123. |
Mike Frysinger | d8f841c | 2014-06-15 00:48:26 -0700 | [diff] [blame] | 1113 | Scripting: |
Mike Frysinger | 48b5e01 | 2020-02-06 17:04:12 -0500 | [diff] [blame] | 1114 | $ gerrit label-cq `gerrit --raw mine` 1 # Mark *ALL* of your public CLs \ |
| 1115 | with Commit-Queue=1. |
| 1116 | $ gerrit label-cq `gerrit --raw -i mine` 1 # Mark *ALL* of your internal \ |
| 1117 | CLs with Commit-Queue=1. |
Mike Frysinger | d7f1079 | 2021-03-08 13:11:38 -0500 | [diff] [blame] | 1118 | $ gerrit --json search 'attention:self' # Dump all pending CLs in JSON. |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1119 | |
Harry Cutts | 26076b3 | 2019-02-26 15:01:29 -0800 | [diff] [blame] | 1120 | Actions: |
| 1121 | """ |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1122 | description += _GetActionUsages() |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1123 | |
Alex Klein | 2ab29cc | 2018-07-19 12:01:00 -0600 | [diff] [blame] | 1124 | site_params = config_lib.GetSiteParams() |
Mike Frysinger | 3f257c8 | 2020-06-16 01:42:29 -0400 | [diff] [blame] | 1125 | parser = commandline.ArgumentParser( |
| 1126 | description=description, default_log_level='notice') |
Mike Frysinger | 8674a11 | 2021-02-09 14:44:17 -0500 | [diff] [blame] | 1127 | |
| 1128 | group = parser.add_argument_group('Server options') |
| 1129 | group.add_argument('-i', '--internal', dest='gob', action='store_const', |
| 1130 | default=site_params.EXTERNAL_GOB_INSTANCE, |
| 1131 | const=site_params.INTERNAL_GOB_INSTANCE, |
| 1132 | help='Query internal Chrome Gerrit instance') |
| 1133 | group.add_argument('-g', '--gob', |
| 1134 | default=site_params.EXTERNAL_GOB_INSTANCE, |
| 1135 | help='Gerrit (on borg) instance to query (default: %s)' % |
| 1136 | (site_params.EXTERNAL_GOB_INSTANCE)) |
| 1137 | |
Mike Frysinger | 8674a11 | 2021-02-09 14:44:17 -0500 | [diff] [blame] | 1138 | group = parser.add_argument_group('CL options') |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 1139 | _AddCommonOptions(parser, group) |
Mike Frysinger | 8674a11 | 2021-02-09 14:44:17 -0500 | [diff] [blame] | 1140 | |
Mike Frysinger | f70bdc7 | 2014-06-15 00:44:06 -0700 | [diff] [blame] | 1141 | parser.add_argument('--raw', default=False, action='store_true', |
| 1142 | help='Return raw results (suitable for scripting)') |
Mike Frysinger | 87c74ce | 2017-04-04 16:12:31 -0400 | [diff] [blame] | 1143 | parser.add_argument('--json', default=False, action='store_true', |
| 1144 | help='Return results in JSON (suitable for scripting)') |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 1145 | return parser |
| 1146 | |
| 1147 | |
| 1148 | def GetParser(parser: commandline.ArgumentParser = None) -> ( |
| 1149 | commandline.ArgumentParser): |
| 1150 | """Returns the full parser to use for this module.""" |
| 1151 | if parser is None: |
| 1152 | parser = GetBaseParser() |
| 1153 | |
| 1154 | actions = _GetActions() |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1155 | |
| 1156 | # Subparsers are required by default under Python 2. Python 3 changed to |
| 1157 | # not required, but didn't include a required option until 3.7. Setting |
| 1158 | # the required member works in all versions (and setting dest name). |
| 1159 | subparsers = parser.add_subparsers(dest='action') |
| 1160 | subparsers.required = True |
| 1161 | for cmd, cls in actions.items(): |
| 1162 | # Format the full docstring by removing the file level indentation. |
| 1163 | description = re.sub(r'^ ', '', cls.__doc__, flags=re.M) |
| 1164 | subparser = subparsers.add_parser(cmd, description=description) |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 1165 | _AddCommonOptions(parser, subparser) |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1166 | cls.init_subparser(subparser) |
Mike Frysinger | 108eda2 | 2018-06-06 18:45:12 -0400 | [diff] [blame] | 1167 | |
| 1168 | return parser |
| 1169 | |
| 1170 | |
| 1171 | def main(argv): |
Mike Frysinger | 2295d79 | 2021-03-08 15:55:23 -0500 | [diff] [blame] | 1172 | base_parser = GetBaseParser() |
| 1173 | opts, subargs = base_parser.parse_known_args(argv) |
| 1174 | |
| 1175 | config = Config() |
| 1176 | if subargs: |
| 1177 | # If the action is an alias to an expanded value, we need to mutate the argv |
| 1178 | # and reparse things. |
| 1179 | action = config.expand_alias(subargs[0]) |
| 1180 | if action != subargs[0]: |
| 1181 | pos = argv.index(subargs[0]) |
| 1182 | argv = argv[:pos] + action + argv[pos + 1:] |
| 1183 | |
| 1184 | parser = GetParser(parser=base_parser) |
Mike Frysinger | ddf86eb | 2014-02-07 22:51:41 -0500 | [diff] [blame] | 1185 | opts = parser.parse_args(argv) |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1186 | |
Mike Frysinger | 484e2f8 | 2020-03-20 01:41:10 -0400 | [diff] [blame] | 1187 | # In case the action wants to throw a parser error. |
| 1188 | opts.parser = parser |
| 1189 | |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 1190 | # A cache of gerrit helpers we'll load on demand. |
| 1191 | opts.gerrit = {} |
Vadim Bendebury | 2e3f82d | 2019-02-11 17:53:03 -0800 | [diff] [blame] | 1192 | |
Mike Frysinger | 88f2729 | 2014-06-17 09:40:45 -0700 | [diff] [blame] | 1193 | opts.Freeze() |
| 1194 | |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 1195 | # pylint: disable=global-statement |
Mike Frysinger | 031ad0b | 2013-05-14 18:15:34 -0400 | [diff] [blame] | 1196 | global COLOR |
| 1197 | COLOR = terminal.Color(enabled=opts.color) |
| 1198 | |
Mike Frysinger | 13f23a4 | 2013-05-13 17:32:01 -0400 | [diff] [blame] | 1199 | # Now look up the requested user action and run it. |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1200 | actions = _GetActions() |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1201 | obj = actions[opts.action]() |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1202 | try: |
Mike Frysinger | c7796cf | 2020-02-06 23:55:15 -0500 | [diff] [blame] | 1203 | obj(opts) |
Mike Frysinger | 65fc863 | 2020-02-06 18:11:12 -0500 | [diff] [blame] | 1204 | except (cros_build_lib.RunCommandError, gerrit.GerritException, |
| 1205 | gob_util.GOBError) as e: |
| 1206 | cros_build_lib.Die(e) |