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