blob: 1a35f740fb4f93b469d67a417a7339c09cfc72b5 [file] [log] [blame]
Mike Frysinger13f23a42013-05-13 17:32:01 -04001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Mike Frysinger08737512014-02-07 22:58:26 -05005"""A command line interface to Gerrit-on-borg instances.
Mike Frysinger13f23a42013-05-13 17:32:01 -04006
7Internal Note:
8To expose a function directly to the command line interface, name your function
9with the prefix "UserAct".
10"""
11
Mike Frysinger31ff6f92014-02-08 04:33:03 -050012from __future__ import print_function
13
Mike Frysinger13f23a42013-05-13 17:32:01 -040014import inspect
15import os
Yu-Ju Hongc20d7b32014-11-18 07:51:11 -080016import pprint
Vadim Bendeburydcfe2322013-05-23 10:54:49 -070017import re
Mike Frysinger13f23a42013-05-13 17:32:01 -040018
Don Garrett88b8d782014-05-13 17:30:55 -070019from chromite.cbuildbot import constants
Mike Frysinger13f23a42013-05-13 17:32:01 -040020from chromite.lib import commandline
21from chromite.lib import cros_build_lib
22from chromite.lib import gerrit
Mike Frysingerc85d8162014-02-08 00:45:21 -050023from chromite.lib import gob_util
Mike Frysinger13f23a42013-05-13 17:32:01 -040024from chromite.lib import terminal
25
26
Mike Frysinger031ad0b2013-05-14 18:15:34 -040027COLOR = None
Mike Frysinger13f23a42013-05-13 17:32:01 -040028
29# Map the internal names to the ones we normally show on the web ui.
30GERRIT_APPROVAL_MAP = {
Vadim Bendebury50571832013-11-12 10:43:19 -080031 'COMR': ['CQ', 'Commit Queue ',],
32 'CRVW': ['CR', 'Code Review ',],
33 'SUBM': ['S ', 'Submitted ',],
David James2b2e2c52014-12-02 19:32:07 -080034 'TRY': ['T ', 'Trybot Ready ',],
Vadim Bendebury50571832013-11-12 10:43:19 -080035 'VRIF': ['V ', 'Verified ',],
Mike Frysinger13f23a42013-05-13 17:32:01 -040036}
37
38# Order is important -- matches the web ui. This also controls the short
39# entries that we summarize in non-verbose mode.
40GERRIT_SUMMARY_CATS = ('CR', 'CQ', 'V',)
41
42
43def red(s):
44 return COLOR.Color(terminal.Color.RED, s)
45
46
47def green(s):
48 return COLOR.Color(terminal.Color.GREEN, s)
49
50
51def blue(s):
52 return COLOR.Color(terminal.Color.BLUE, s)
53
54
55def limits(cls):
56 """Given a dict of fields, calculate the longest string lengths
57
58 This allows you to easily format the output of many results so that the
59 various cols all line up correctly.
60 """
61 lims = {}
62 for cl in cls:
63 for k in cl.keys():
Mike Frysingerf16b8f02013-10-21 22:24:46 -040064 # Use %s rather than str() to avoid codec issues.
65 # We also do this so we can format integers.
66 lims[k] = max(lims.get(k, 0), len('%s' % cl[k]))
Mike Frysinger13f23a42013-05-13 17:32:01 -040067 return lims
68
69
Mike Frysinger88f27292014-06-17 09:40:45 -070070# TODO: This func really needs to be merged into the core gerrit logic.
71def GetGerrit(opts, cl=None):
72 """Auto pick the right gerrit instance based on the |cl|
73
74 Args:
75 opts: The general options object.
76 cl: A CL taking one of the forms: 1234 *1234 chromium:1234
77
78 Returns:
79 A tuple of a gerrit object and a sanitized CL #.
80 """
81 gob = opts.gob
82 if not cl is None:
83 if cl.startswith('*'):
84 gob = constants.INTERNAL_GOB_INSTANCE
85 cl = cl[1:]
86 elif ':' in cl:
87 gob, cl = cl.split(':', 1)
88
89 if not gob in opts.gerrit:
90 opts.gerrit[gob] = gerrit.GetGerritHelper(gob=gob, print_cmd=opts.debug)
91
92 return (opts.gerrit[gob], cl)
93
94
Mike Frysinger13f23a42013-05-13 17:32:01 -040095def GetApprovalSummary(_opts, cls):
96 """Return a dict of the most important approvals"""
97 approvs = dict([(x, '') for x in GERRIT_SUMMARY_CATS])
98 if 'approvals' in cls['currentPatchSet']:
99 for approver in cls['currentPatchSet']['approvals']:
100 cats = GERRIT_APPROVAL_MAP.get(approver['type'])
101 if not cats:
102 cros_build_lib.Warning('unknown gerrit approval type: %s',
103 approver['type'])
104 continue
105 cat = cats[0].strip()
106 val = int(approver['value'])
107 if not cat in approvs:
108 # Ignore the extended categories in the summary view.
109 continue
110 elif approvs[cat] is '':
111 approvs[cat] = val
112 elif val < 0:
113 approvs[cat] = min(approvs[cat], val)
114 else:
115 approvs[cat] = max(approvs[cat], val)
116 return approvs
117
118
119def PrintCl(opts, cls, lims, show_approvals=True):
120 """Pretty print a single result"""
Mike Frysingerf70bdc72014-06-15 00:44:06 -0700121 if opts.raw:
122 # Special case internal Chrome GoB as that is what most devs use.
123 # They can always redirect the list elsewhere via the -g option.
124 if opts.gob == constants.INTERNAL_GOB_INSTANCE:
125 print(constants.INTERNAL_CHANGE_PREFIX, end='')
126 print(cls['number'])
127 return
128
Mike Frysinger13f23a42013-05-13 17:32:01 -0400129 if not lims:
130 lims = {'url': 0, 'project': 0}
131
132 status = ''
133 if show_approvals and not opts.verbose:
134 approvs = GetApprovalSummary(opts, cls)
135 for cat in GERRIT_SUMMARY_CATS:
136 if approvs[cat] is '':
137 functor = lambda x: x
138 elif approvs[cat] < 0:
139 functor = red
140 else:
141 functor = green
142 status += functor('%s:%2s ' % (cat, approvs[cat]))
143
Mike Frysinger31ff6f92014-02-08 04:33:03 -0500144 print('%s %s%-*s %s' % (blue('%-*s' % (lims['url'], cls['url'])), status,
145 lims['project'], cls['project'], cls['subject']))
Mike Frysinger13f23a42013-05-13 17:32:01 -0400146
147 if show_approvals and opts.verbose:
148 for approver in cls['currentPatchSet'].get('approvals', []):
149 functor = red if int(approver['value']) < 0 else green
150 n = functor('%2s' % approver['value'])
151 t = GERRIT_APPROVAL_MAP.get(approver['type'], [approver['type'],
152 approver['type']])[1]
Mike Frysinger31ff6f92014-02-08 04:33:03 -0500153 print(' %s %s %s' % (n, t, approver['by']['email']))
Mike Frysinger13f23a42013-05-13 17:32:01 -0400154
155
156def _MyUserInfo():
157 username = os.environ['USER']
158 emails = ['%s@%s' % (username, domain)
159 for domain in ('google.com', 'chromium.org')]
160 reviewers = ['reviewer:%s' % x for x in emails]
161 owners = ['owner:%s' % x for x in emails]
162 return emails, reviewers, owners
163
164
165def FilteredQuery(opts, query):
166 """Query gerrit and filter/clean up the results"""
167 ret = []
168
Mike Frysinger88f27292014-06-17 09:40:45 -0700169 helper, _ = GetGerrit(opts)
170 for cl in helper.Query(query, raw=True, bypass_cache=False):
Mike Frysinger13f23a42013-05-13 17:32:01 -0400171 # Gerrit likes to return a stats record too.
172 if not 'project' in cl:
173 continue
174
175 # Strip off common leading names since the result is still
176 # unique over the whole tree.
177 if not opts.verbose:
Mike Frysingere5e78272014-06-15 00:41:30 -0700178 for pfx in ('chromeos', 'chromiumos', 'overlays', 'platform',
179 'third_party'):
Mike Frysinger13f23a42013-05-13 17:32:01 -0400180 if cl['project'].startswith('%s/' % pfx):
181 cl['project'] = cl['project'][len(pfx) + 1:]
182
183 ret.append(cl)
184
185 if opts.sort in ('number',):
186 key = lambda x: int(x[opts.sort])
187 else:
188 key = lambda x: x[opts.sort]
189 return sorted(ret, key=key)
190
191
Mike Frysinger13f23a42013-05-13 17:32:01 -0400192def IsApprover(cl, users):
193 """See if the approvers in |cl| is listed in |users|"""
194 # See if we are listed in the approvals list. We have to parse
195 # this by hand as the gerrit query system doesn't support it :(
196 # http://code.google.com/p/gerrit/issues/detail?id=1235
197 if 'approvals' not in cl['currentPatchSet']:
198 return False
199
200 if isinstance(users, basestring):
201 users = (users,)
202
203 for approver in cl['currentPatchSet']['approvals']:
Stefan Zager29560302013-09-06 14:30:54 -0700204 if (approver['by']['email'] in users and
205 approver['type'] == 'CRVW' and
206 int(approver['value']) != 0):
Mike Frysinger13f23a42013-05-13 17:32:01 -0400207 return True
208
209 return False
210
211
212def UserActTodo(opts):
213 """List CLs needing your review"""
214 emails, reviewers, owners = _MyUserInfo()
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500215 cls = FilteredQuery(opts, ('( %s ) status:open NOT ( %s )' %
216 (' OR '.join(reviewers), ' OR '.join(owners))))
Mike Frysinger13f23a42013-05-13 17:32:01 -0400217 cls = [x for x in cls if not IsApprover(x, emails)]
218 lims = limits(cls)
219 for cl in cls:
220 PrintCl(opts, cl, lims)
221
222
Mike Frysingera1db2c42014-06-15 00:42:48 -0700223def UserActSearch(opts, query):
224 """List CLs matching the Gerrit <search query>"""
225 cls = FilteredQuery(opts, query)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400226 lims = limits(cls)
227 for cl in cls:
228 PrintCl(opts, cl, lims)
229
230
Mike Frysingera1db2c42014-06-15 00:42:48 -0700231def UserActMine(opts):
232 """List your CLs with review statuses"""
233 _, _, owners = _MyUserInfo()
234 UserActSearch(opts, '( %s ) status:new' % (' OR '.join(owners),))
235
236
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700237def UserActInspect(opts, *args):
238 """Inspect CL number <n> [n ...]"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700239 for arg in args:
240 cl = FilteredQuery(opts, arg)
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700241 if cl:
242 PrintCl(opts, cl[0], None)
243 else:
Mike Frysinger88f27292014-06-17 09:40:45 -0700244 print('no results found for CL %s' % arg)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400245
246
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700247def UserActReview(opts, *args):
248 """Mark CL <n> [n ...] with code review status <-2,-1,0,1,2>"""
249 num = args[-1]
Mike Frysinger88f27292014-06-17 09:40:45 -0700250 for arg in args[:-1]:
251 helper, cl = GetGerrit(opts, arg)
252 helper.SetReview(cl, labels={'Code-Review': num}, dryrun=opts.dryrun)
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700253UserActReview.arg_min = 2
Mike Frysinger13f23a42013-05-13 17:32:01 -0400254
255
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700256def UserActVerify(opts, *args):
257 """Mark CL <n> [n ...] with verify status <-1,0,1>"""
258 num = args[-1]
Mike Frysinger88f27292014-06-17 09:40:45 -0700259 for arg in args[:-1]:
260 helper, cl = GetGerrit(opts, arg)
261 helper.SetReview(cl, labels={'Verified': num}, dryrun=opts.dryrun)
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700262UserActVerify.arg_min = 2
Mike Frysinger13f23a42013-05-13 17:32:01 -0400263
264
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700265def UserActReady(opts, *args):
266 """Mark CL <n> [n ...] with ready status <0,1,2>"""
267 num = args[-1]
Mike Frysinger88f27292014-06-17 09:40:45 -0700268 for arg in args[:-1]:
269 helper, cl = GetGerrit(opts, arg)
270 helper.SetReview(cl, labels={'Commit-Queue': num}, dryrun=opts.dryrun)
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700271UserActReady.arg_min = 2
Mike Frysinger13f23a42013-05-13 17:32:01 -0400272
273
Mike Frysinger15b23e42014-12-05 17:00:05 -0500274def UserActTrybotready(opts, *args):
275 """Mark CL <n> [n ...] with trybot-ready status <0,1>"""
276 num = args[-1]
277 for arg in args[:-1]:
278 helper, cl = GetGerrit(opts, arg)
279 helper.SetReview(cl, labels={'Trybot-Ready': num}, dryrun=opts.dryrun)
280UserActTrybotready.arg_min = 2
281
282
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700283def UserActSubmit(opts, *args):
284 """Submit CL <n> [n ...]"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700285 for arg in args:
286 helper, cl = GetGerrit(opts, arg)
287 helper.SubmitChange(cl, dryrun=opts.dryrun)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400288
289
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700290def UserActAbandon(opts, *args):
291 """Abandon CL <n> [n ...]"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700292 for arg in args:
293 helper, cl = GetGerrit(opts, arg)
294 helper.AbandonChange(cl, dryrun=opts.dryrun)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400295
296
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700297def UserActRestore(opts, *args):
298 """Restore CL <n> [n ...] that was abandoned"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700299 for arg in args:
300 helper, cl = GetGerrit(opts, arg)
301 helper.RestoreChange(cl, dryrun=opts.dryrun)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400302
303
Mike Frysinger88f27292014-06-17 09:40:45 -0700304def UserActReviewers(opts, cl, *args):
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700305 """Add/remove reviewers' emails for CL <n> (prepend with '~' to remove)"""
Mike Frysingerc15efa52013-12-12 01:13:56 -0500306 emails = args
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700307 # Allow for optional leading '~'.
308 email_validator = re.compile(r'^[~]?%s$' % constants.EMAIL_REGEX)
309 add_list, remove_list, invalid_list = [], [], []
310
311 for x in emails:
312 if not email_validator.match(x):
313 invalid_list.append(x)
314 elif x[0] == '~':
315 remove_list.append(x[1:])
316 else:
317 add_list.append(x)
318
319 if invalid_list:
320 cros_build_lib.Die(
321 'Invalid email address(es): %s' % ', '.join(invalid_list))
322
323 if add_list or remove_list:
Mike Frysinger88f27292014-06-17 09:40:45 -0700324 helper, cl = GetGerrit(opts, cl)
325 helper.SetReviewers(cl, add=add_list, remove=remove_list,
326 dryrun=opts.dryrun)
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700327
328
Mike Frysinger88f27292014-06-17 09:40:45 -0700329def UserActMessage(opts, cl, message):
Doug Anderson8119df02013-07-20 21:00:24 +0530330 """Add a message to CL <n>"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700331 helper, cl = GetGerrit(opts, cl)
332 helper.SetReview(cl, msg=message, dryrun=opts.dryrun)
Doug Anderson8119df02013-07-20 21:00:24 +0530333
334
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700335def UserActDeletedraft(opts, *args):
336 """Delete draft patch set <n> [n ...]"""
Mike Frysinger88f27292014-06-17 09:40:45 -0700337 for arg in args:
338 helper, cl = GetGerrit(opts, arg)
339 helper.DeleteDraft(cl, dryrun=opts.dryrun)
Jon Salza427fb02014-03-07 18:13:17 +0800340
341
Yu-Ju Hongc20d7b32014-11-18 07:51:11 -0800342def UserActAccount(opts):
343 """Get user account information."""
344 helper, _ = GetGerrit(opts)
345 pprint.PrettyPrinter().pprint(helper.GetAccount())
346
347
Mike Frysinger13f23a42013-05-13 17:32:01 -0400348def main(argv):
349 # Locate actions that are exposed to the user. All functions that start
350 # with "UserAct" are fair game.
351 act_pfx = 'UserAct'
352 actions = [x for x in globals() if x.startswith(act_pfx)]
353
Mike Frysingerddf86eb2014-02-07 22:51:41 -0500354 usage = """%(prog)s [options] <action> [action args]
Mike Frysinger13f23a42013-05-13 17:32:01 -0400355
356There is no support for doing line-by-line code review via the command line.
357This helps you manage various bits and CL status.
358
Mike Frysingera1db2c42014-06-15 00:42:48 -0700359For general Gerrit documentation, see:
360 https://gerrit-review.googlesource.com/Documentation/
361The Searching Changes page covers the search query syntax:
362 https://gerrit-review.googlesource.com/Documentation/user-search.html
363
Mike Frysinger13f23a42013-05-13 17:32:01 -0400364Example:
365 $ gerrit todo # List all the CLs that await your review.
366 $ gerrit mine # List all of your open CLs.
367 $ gerrit inspect 28123 # Inspect CL 28123 on the public gerrit.
368 $ gerrit inspect *28123 # Inspect CL 28123 on the internal gerrit.
369 $ gerrit verify 28123 1 # Mark CL 28123 as verified (+1).
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700370Scripting:
Mike Frysinger88f27292014-06-17 09:40:45 -0700371 $ gerrit ready `gerrit --raw mine` 1 # Mark *ALL* of your public CLs \
372ready.
373 $ gerrit ready `gerrit --raw -i mine` 1 # Mark *ALL* of your internal CLs \
374ready.
Mike Frysinger13f23a42013-05-13 17:32:01 -0400375
376Actions:"""
377 indent = max([len(x) - len(act_pfx) for x in actions])
378 for a in sorted(actions):
Mike Frysinger15b23e42014-12-05 17:00:05 -0500379 cmd = a[len(act_pfx):]
380 # Sanity check for devs adding new commands. Should be quick.
381 if cmd != cmd.lower().capitalize():
382 raise RuntimeError('callback "%s" is misnamed; should be "%s"' %
383 (cmd, cmd.lower().capitalize()))
384 usage += '\n %-*s: %s' % (indent, cmd.lower(), globals()[a].__doc__)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400385
Mike Frysingerddf86eb2014-02-07 22:51:41 -0500386 parser = commandline.ArgumentParser(usage=usage)
Mike Frysinger08737512014-02-07 22:58:26 -0500387 parser.add_argument('-i', '--internal', dest='gob', action='store_const',
Mike Frysinger88f27292014-06-17 09:40:45 -0700388 default=constants.EXTERNAL_GOB_INSTANCE,
Mike Frysinger40541c62014-02-08 04:38:37 -0500389 const=constants.INTERNAL_GOB_INSTANCE,
Mike Frysinger08737512014-02-07 22:58:26 -0500390 help='Query internal Chromium Gerrit instance')
391 parser.add_argument('-g', '--gob',
Mike Frysinger88f27292014-06-17 09:40:45 -0700392 default=constants.EXTERNAL_GOB_INSTANCE,
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500393 help=('Gerrit (on borg) instance to query (default: %s)' %
394 (constants.EXTERNAL_GOB_INSTANCE)))
Mike Frysingerddf86eb2014-02-07 22:51:41 -0500395 parser.add_argument('--sort', default='number',
396 help='Key to sort on (number, project)')
Mike Frysingerf70bdc72014-06-15 00:44:06 -0700397 parser.add_argument('--raw', default=False, action='store_true',
398 help='Return raw results (suitable for scripting)')
Mike Frysinger550d9aa2014-06-15 00:55:31 -0700399 parser.add_argument('-n', '--dry-run', default=False, action='store_true',
400 dest='dryrun',
401 help='Show what would be done, but do not make changes')
Mike Frysingerddf86eb2014-02-07 22:51:41 -0500402 parser.add_argument('-v', '--verbose', default=False, action='store_true',
403 help='Be more verbose in output')
404 parser.add_argument('args', nargs='+')
405 opts = parser.parse_args(argv)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400406
Mike Frysinger88f27292014-06-17 09:40:45 -0700407 # A cache of gerrit helpers we'll load on demand.
408 opts.gerrit = {}
409 opts.Freeze()
410
Mike Frysinger031ad0b2013-05-14 18:15:34 -0400411 # pylint: disable=W0603
412 global COLOR
413 COLOR = terminal.Color(enabled=opts.color)
414
Mike Frysinger13f23a42013-05-13 17:32:01 -0400415 # Now look up the requested user action and run it.
Mike Frysinger88f27292014-06-17 09:40:45 -0700416 cmd = opts.args[0].lower()
417 args = opts.args[1:]
Mike Frysinger13f23a42013-05-13 17:32:01 -0400418 functor = globals().get(act_pfx + cmd.capitalize())
419 if functor:
420 argspec = inspect.getargspec(functor)
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700421 if argspec.varargs:
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700422 arg_min = getattr(functor, 'arg_min', len(argspec.args))
423 if len(args) < arg_min:
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700424 parser.error('incorrect number of args: %s expects at least %s' %
Mike Frysingerd8f841c2014-06-15 00:48:26 -0700425 (cmd, arg_min))
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700426 elif len(argspec.args) - 1 != len(args):
Mike Frysinger13f23a42013-05-13 17:32:01 -0400427 parser.error('incorrect number of args: %s expects %s' %
428 (cmd, len(argspec.args) - 1))
Vadim Bendebury614f8682013-05-23 10:33:35 -0700429 try:
430 functor(opts, *args)
Mike Frysingerc85d8162014-02-08 00:45:21 -0500431 except (cros_build_lib.RunCommandError, gerrit.GerritException,
432 gob_util.GOBError) as e:
Vadim Bendeburydcfe2322013-05-23 10:54:49 -0700433 cros_build_lib.Die(e.message)
Mike Frysinger13f23a42013-05-13 17:32:01 -0400434 else:
435 parser.error('unknown action: %s' % (cmd,))