blob: 07b6c0e513222741260dd5274e3d6d6b14747852 [file] [log] [blame]
Josip Sokcevic84434e82021-06-09 22:48:43 +00001#!/usr/bin/env vpython3
dimu833c94c2017-01-18 17:36:15 -08002# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Simple client for the Gerrit REST API.
7
8Example usage:
Michael Moss5eebf6f2021-07-16 13:18:34 +00009 ./gerrit_client.py [command] [args]
dimu833c94c2017-01-18 17:36:15 -080010"""
11
12from __future__ import print_function
13
14import json
15import logging
16import optparse
17import subcommand
18import sys
Josip Sokcevicc99efb22020-03-17 00:35:34 +000019
20if sys.version_info.major == 2:
21 import urlparse
22 from urllib import quote_plus
23else:
24 from urllib.parse import quote_plus
25 import urllib.parse as urlparse
dimu833c94c2017-01-18 17:36:15 -080026
dimu833c94c2017-01-18 17:36:15 -080027import fix_encoding
28import gerrit_util
29import setup_color
30
31__version__ = '0.1'
dimu833c94c2017-01-18 17:36:15 -080032
33
34def write_result(result, opt):
35 if opt.json_file:
36 with open(opt.json_file, 'w') as json_file:
37 json_file.write(json.dumps(result))
38
39
40@subcommand.usage('[args ...]')
Josip Sokcevicc39ab992020-09-24 20:09:15 +000041def CMDmovechanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +000042 """Move changes to a different destination branch."""
Josip Sokcevicc39ab992020-09-24 20:09:15 +000043 parser.add_option('-p', '--param', dest='params', action='append',
44 help='repeatable query parameter, format: -p key=value')
45 parser.add_option('--destination_branch', dest='destination_branch',
46 help='where to move changes to')
47
48 (opt, args) = parser.parse_args(args)
49 assert opt.destination_branch, "--destination_branch not defined"
Mike Frysinger8820ab82020-11-25 00:52:31 +000050 for p in opt.params:
51 assert '=' in p, '--param is key=value, not "%s"' % p
Josip Sokcevicc39ab992020-09-24 20:09:15 +000052 host = urlparse.urlparse(opt.host).netloc
53
54 limit = 100
55 while True:
56 result = gerrit_util.QueryChanges(
57 host,
58 list(tuple(p.split('=', 1)) for p in opt.params),
59 limit=limit,
60 )
61 for change in result:
62 gerrit_util.MoveChange(host, change['id'], opt.destination_branch)
63
64 if len(result) < limit:
65 break
66 logging.info("Done")
67
68
69@subcommand.usage('[args ...]')
dimu833c94c2017-01-18 17:36:15 -080070def CMDbranchinfo(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +000071 """Get information on a gerrit branch."""
dimu833c94c2017-01-18 17:36:15 -080072 parser.add_option('--branch', dest='branch', help='branch name')
73
74 (opt, args) = parser.parse_args(args)
75 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicc99efb22020-03-17 00:35:34 +000076 project = quote_plus(opt.project)
77 branch = quote_plus(opt.branch)
dimu833c94c2017-01-18 17:36:15 -080078 result = gerrit_util.GetGerritBranch(host, project, branch)
79 logging.info(result)
80 write_result(result, opt)
81
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +000082
dimu833c94c2017-01-18 17:36:15 -080083@subcommand.usage('[args ...]')
84def CMDbranch(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +000085 """Create a branch in a gerrit project."""
dimu833c94c2017-01-18 17:36:15 -080086 parser.add_option('--branch', dest='branch', help='branch name')
87 parser.add_option('--commit', dest='commit', help='commit hash')
88
89 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +000090 assert opt.project, "--project not defined"
91 assert opt.branch, "--branch not defined"
92 assert opt.commit, "--commit not defined"
dimu833c94c2017-01-18 17:36:15 -080093
Josip Sokcevicc99efb22020-03-17 00:35:34 +000094 project = quote_plus(opt.project)
dimu833c94c2017-01-18 17:36:15 -080095 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicc99efb22020-03-17 00:35:34 +000096 branch = quote_plus(opt.branch)
97 commit = quote_plus(opt.commit)
dimu833c94c2017-01-18 17:36:15 -080098 result = gerrit_util.CreateGerritBranch(host, project, branch, commit)
99 logging.info(result)
100 write_result(result, opt)
101
102
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200103@subcommand.usage('[args ...]')
Michael Mossb6ce2442021-10-20 04:36:24 +0000104def CMDtag(parser, args):
105 """Create a tag in a gerrit project."""
106 parser.add_option('--tag', dest='tag', help='tag name')
107 parser.add_option('--commit', dest='commit', help='commit hash')
108
109 (opt, args) = parser.parse_args(args)
110 assert opt.project, "--project not defined"
111 assert opt.tag, "--tag not defined"
112 assert opt.commit, "--commit not defined"
113
114 project = quote_plus(opt.project)
115 host = urlparse.urlparse(opt.host).netloc
116 tag = quote_plus(opt.tag)
117 commit = quote_plus(opt.commit)
118 result = gerrit_util.CreateGerritTag(host, project, tag, commit)
119 logging.info(result)
120 write_result(result, opt)
121
122
123@subcommand.usage('[args ...]')
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000124def CMDhead(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000125 """Update which branch the project HEAD points to."""
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000126 parser.add_option('--branch', dest='branch', help='branch name')
127
128 (opt, args) = parser.parse_args(args)
129 assert opt.project, "--project not defined"
130 assert opt.branch, "--branch not defined"
131
132 project = quote_plus(opt.project)
133 host = urlparse.urlparse(opt.host).netloc
134 branch = quote_plus(opt.branch)
135 result = gerrit_util.UpdateHead(host, project, branch)
136 logging.info(result)
137 write_result(result, opt)
138
139
140@subcommand.usage('[args ...]')
141def CMDheadinfo(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000142 """Retrieves the current HEAD of the project."""
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000143
144 (opt, args) = parser.parse_args(args)
145 assert opt.project, "--project not defined"
146
147 project = quote_plus(opt.project)
148 host = urlparse.urlparse(opt.host).netloc
149 result = gerrit_util.GetHead(host, project)
150 logging.info(result)
151 write_result(result, opt)
152
153
154@subcommand.usage('[args ...]')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200155def CMDchanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000156 """Queries gerrit for matching changes."""
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200157 parser.add_option('-p', '--param', dest='params', action='append',
158 help='repeatable query parameter, format: -p key=value')
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200159 parser.add_option('-o', '--o-param', dest='o_params', action='append',
160 help='gerrit output parameters, e.g. ALL_REVISIONS')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200161 parser.add_option('--limit', dest='limit', type=int,
162 help='maximum number of results to return')
163 parser.add_option('--start', dest='start', type=int,
164 help='how many changes to skip '
165 '(starting with the most recent)')
166
167 (opt, args) = parser.parse_args(args)
Mike Frysinger8820ab82020-11-25 00:52:31 +0000168 for p in opt.params:
169 assert '=' in p, '--param is key=value, not "%s"' % p
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200170
171 result = gerrit_util.QueryChanges(
172 urlparse.urlparse(opt.host).netloc,
173 list(tuple(p.split('=', 1)) for p in opt.params),
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200174 start=opt.start, # Default: None
175 limit=opt.limit, # Default: None
176 o_params=opt.o_params, # Default: None
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200177 )
178 logging.info('Change query returned %d changes.', len(result))
179 write_result(result, opt)
180
181
LaMont Jones9eed4232021-04-02 16:29:49 +0000182@subcommand.usage('[args ...]')
Marco Georgaklis85557a02021-06-03 15:56:54 +0000183def CMDrelatedchanges(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000184 """Gets related changes for a given change and revision."""
Marco Georgaklis85557a02021-06-03 15:56:54 +0000185 parser.add_option('-c', '--change', type=str, help='change id')
186 parser.add_option('-r', '--revision', type=str, help='revision id')
187
188 (opt, args) = parser.parse_args(args)
189
190 result = gerrit_util.GetRelatedChanges(
191 urlparse.urlparse(opt.host).netloc,
192 change=opt.change,
193 revision=opt.revision,
194 )
195 logging.info(result)
196 write_result(result, opt)
197
198
199@subcommand.usage('[args ...]')
LaMont Jones9eed4232021-04-02 16:29:49 +0000200def CMDcreatechange(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000201 """Create a new change in gerrit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000202 parser.add_option('-s', '--subject', help='subject for change')
203 parser.add_option('-b',
204 '--branch',
205 default='main',
206 help='target branch for change')
207 parser.add_option(
208 '-p',
209 '--param',
210 dest='params',
211 action='append',
212 help='repeatable field value parameter, format: -p key=value')
213
214 (opt, args) = parser.parse_args(args)
215 for p in opt.params:
216 assert '=' in p, '--param is key=value, not "%s"' % p
217
218 result = gerrit_util.CreateChange(
219 urlparse.urlparse(opt.host).netloc,
220 opt.project,
221 branch=opt.branch,
222 subject=opt.subject,
223 params=list(tuple(p.split('=', 1)) for p in opt.params),
224 )
225 logging.info(result)
226 write_result(result, opt)
227
228
229@subcommand.usage('[args ...]')
230def CMDchangeedit(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000231 """Puts content of a file into a change edit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000232 parser.add_option('-c', '--change', type=int, help='change number')
233 parser.add_option('--path', help='path for file')
234 parser.add_option('--file', help='file to place at |path|')
235
236 (opt, args) = parser.parse_args(args)
237
238 with open(opt.file) as f:
239 data = f.read()
240 result = gerrit_util.ChangeEdit(
241 urlparse.urlparse(opt.host).netloc, opt.change, opt.path, data)
242 logging.info(result)
243 write_result(result, opt)
244
245
246@subcommand.usage('[args ...]')
247def CMDpublishchangeedit(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000248 """Publish a Gerrit change edit."""
LaMont Jones9eed4232021-04-02 16:29:49 +0000249 parser.add_option('-c', '--change', type=int, help='change number')
250 parser.add_option('--notify', help='whether to notify')
251
252 (opt, args) = parser.parse_args(args)
253
254 result = gerrit_util.PublishChangeEdit(
255 urlparse.urlparse(opt.host).netloc, opt.change, opt.notify)
256 logging.info(result)
257 write_result(result, opt)
258
259
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000260@subcommand.usage('[args ...]')
261def CMDsubmitchange(parser, args):
262 """Submit a Gerrit change."""
263 parser.add_option('-c', '--change', type=int, help='change number')
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000264 (opt, args) = parser.parse_args(args)
Xinan Lin1bd4ffa2021-07-28 00:54:22 +0000265 result = gerrit_util.SubmitChange(
266 urlparse.urlparse(opt.host).netloc, opt.change)
Xinan Lin6ec7cd82021-07-21 00:53:42 +0000267 logging.info(result)
268 write_result(result, opt)
269
270
Xinan Linc2fb26a2021-07-27 18:01:55 +0000271@subcommand.usage('[args ...]')
Xinan Lin2b4ec952021-08-20 17:35:29 +0000272def CMDchangesubmittedtogether(parser, args):
273 """Get all changes submitted with the given one."""
274 parser.add_option('-c', '--change', type=int, help='change number')
275 (opt, args) = parser.parse_args(args)
276 result = gerrit_util.GetChangesSubmittedTogether(
277 urlparse.urlparse(opt.host).netloc, opt.change)
278 logging.info(result)
279 write_result(result, opt)
280
281
282@subcommand.usage('[args ...]')
Xinan Linc2fb26a2021-07-27 18:01:55 +0000283def CMDgetcommitincludedin(parser, args):
284 """Retrieves the branches and tags for a given commit."""
285 parser.add_option('--commit', dest='commit', help='commit hash')
286 (opt, args) = parser.parse_args(args)
287 result = gerrit_util.GetCommitIncludedIn(
288 urlparse.urlparse(opt.host).netloc, opt.project, opt.commit)
289 logging.info(result)
290 write_result(result, opt)
291
292
Xinan Lin0b0738d2021-07-27 19:13:49 +0000293@subcommand.usage('[args ...]')
294def CMDsetbotcommit(parser, args):
295 """Sets bot-commit+1 to a bot generated change."""
296 parser.add_option('-c', '--change', type=int, help='change number')
297 (opt, args) = parser.parse_args(args)
298 result = gerrit_util.SetReview(
299 urlparse.urlparse(opt.host).netloc,
300 opt.change,
301 labels={'Bot-Commit': 1},
302 ready=True)
303 logging.info(result)
304 write_result(result, opt)
305
306
Ben Pastene281edf72021-10-06 01:23:24 +0000307@subcommand.usage('[args ...]')
308def CMDsetlabel(parser, args):
309 """Sets a label to a specific value on a given change."""
310 parser.add_option('-c', '--change', type=int, help='change number')
311 parser.add_option('-l',
312 '--label',
313 nargs=2,
314 metavar=('label_name', 'label_value'))
315 (opt, args) = parser.parse_args(args)
316 result = gerrit_util.SetReview(urlparse.urlparse(opt.host).netloc,
317 opt.change,
318 labels={opt.label[0]: opt.label[1]})
319 logging.info(result)
320 write_result(result, opt)
321
322
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000323@subcommand.usage('')
324def CMDabandon(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000325 """Abandons a Gerrit change."""
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000326 parser.add_option('-c', '--change', type=int, help='change number')
327 parser.add_option('-m', '--message', default='', help='reason for abandoning')
328
329 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000330 assert opt.change, "-c not defined"
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000331 result = gerrit_util.AbandonChange(
332 urlparse.urlparse(opt.host).netloc,
333 opt.change, opt.message)
334 logging.info(result)
335 write_result(result, opt)
336
337
Michael Moss5eebf6f2021-07-16 13:18:34 +0000338@subcommand.usage('')
Josip Sokcevice1a98942021-04-07 21:35:29 +0000339def CMDmass_abandon(parser, args):
Michael Moss5eebf6f2021-07-16 13:18:34 +0000340 """Mass abandon changes
341
342 Abandons CLs that match search criteria provided by user. Before any change is
343 actually abandoned, user is presented with a list of CLs that will be affected
344 if user confirms. User can skip confirmation by passing --force parameter.
345
346 The script can abandon up to 100 CLs per invocation.
347
348 Examples:
349 gerrit_client.py mass-abandon --host https://HOST -p 'project=repo2'
350 gerrit_client.py mass-abandon --host https://HOST -p 'message=testing'
351 gerrit_client.py mass-abandon --host https://HOST -p 'is=wip' -p 'age=1y'
352 """
Josip Sokcevice1a98942021-04-07 21:35:29 +0000353 parser.add_option('-p',
354 '--param',
355 dest='params',
356 action='append',
357 default=[],
358 help='repeatable query parameter, format: -p key=value')
359 parser.add_option('-m', '--message', default='', help='reason for abandoning')
360 parser.add_option('-f',
361 '--force',
362 action='store_true',
363 help='Don\'t prompt for confirmation')
364
365 opt, args = parser.parse_args(args)
366
367 for p in opt.params:
368 assert '=' in p, '--param is key=value, not "%s"' % p
369 search_query = list(tuple(p.split('=', 1)) for p in opt.params)
370 if not any(t for t in search_query if t[0] == 'owner'):
371 # owner should always be present when abandoning changes
372 search_query.append(('owner', 'me'))
373 search_query.append(('status', 'open'))
374 logging.info("Searching for: %s" % search_query)
375
376 host = urlparse.urlparse(opt.host).netloc
377
378 result = gerrit_util.QueryChanges(
379 host,
380 search_query,
381 # abandon at most 100 changes as not all Gerrit instances support
382 # unlimited results.
383 limit=100,
384 )
385 if len(result) == 0:
386 logging.warn("Nothing to abandon")
387 return
388
389 logging.warn("%s CLs match search query: " % len(result))
390 for change in result:
391 logging.warn("[ID: %d] %s" % (change['_number'], change['subject']))
392
393 if not opt.force:
Josip Sokcevic284fbdd2021-10-08 18:26:30 +0000394 q = input(
Josip Sokcevice1a98942021-04-07 21:35:29 +0000395 'Do you want to move forward with abandoning? [y to confirm] ').strip()
396 if q not in ['y', 'Y']:
397 logging.warn("Aborting...")
398 return
399
400 for change in result:
401 logging.warning("Abandoning: %s" % change['subject'])
402 gerrit_util.AbandonChange(host, change['id'], opt.message)
403
404 logging.warning("Done")
405
406
dimu833c94c2017-01-18 17:36:15 -0800407class OptionParser(optparse.OptionParser):
408 """Creates the option parse and add --verbose support."""
409 def __init__(self, *args, **kwargs):
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000410 optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs)
dimu833c94c2017-01-18 17:36:15 -0800411 self.add_option(
412 '--verbose', action='count', default=0,
413 help='Use 2 times for more debugging info')
414 self.add_option('--host', dest='host', help='Url of host.')
415 self.add_option('--project', dest='project', help='project name')
416 self.add_option(
417 '--json_file', dest='json_file', help='output json filepath')
418
419 def parse_args(self, args=None, values=None):
420 options, args = optparse.OptionParser.parse_args(self, args, values)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000421 # Host is always required
422 assert options.host, "--host not defined."
dimu833c94c2017-01-18 17:36:15 -0800423 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
424 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
425 return options, args
426
427
428def main(argv):
429 if sys.hexversion < 0x02060000:
430 print('\nYour python version %s is unsupported, please upgrade.\n'
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +0000431 % (sys.version.split(' ', 1)[0],),
dimu833c94c2017-01-18 17:36:15 -0800432 file=sys.stderr)
433 return 2
434 dispatcher = subcommand.CommandDispatcher(__name__)
435 return dispatcher.execute(OptionParser(), argv)
436
437
438if __name__ == '__main__':
439 # These affect sys.stdout so do it outside of main() to simplify mocks in
440 # unit testing.
441 fix_encoding.fix_encoding()
442 setup_color.init()
443 try:
444 sys.exit(main(sys.argv[1:]))
445 except KeyboardInterrupt:
446 sys.stderr.write('interrupted\n')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200447 sys.exit(1)