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