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