blob: 0228d5ac068e351ddc82512e46c89ca696475ff7 [file] [log] [blame]
Josip Sokcevic848a5362021-03-22 22:58:00 +00001#!/usr/bin/env python
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:
9 ./gerrit_client.py [command] [args]""
10"""
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):
42 parser.add_option('-p', '--param', dest='params', action='append',
43 help='repeatable query parameter, format: -p key=value')
44 parser.add_option('--destination_branch', dest='destination_branch',
45 help='where to move changes to')
46
47 (opt, args) = parser.parse_args(args)
48 assert opt.destination_branch, "--destination_branch not defined"
Mike Frysinger8820ab82020-11-25 00:52:31 +000049 for p in opt.params:
50 assert '=' in p, '--param is key=value, not "%s"' % p
Josip Sokcevicc39ab992020-09-24 20:09:15 +000051 host = urlparse.urlparse(opt.host).netloc
52
53 limit = 100
54 while True:
55 result = gerrit_util.QueryChanges(
56 host,
57 list(tuple(p.split('=', 1)) for p in opt.params),
58 limit=limit,
59 )
60 for change in result:
61 gerrit_util.MoveChange(host, change['id'], opt.destination_branch)
62
63 if len(result) < limit:
64 break
65 logging.info("Done")
66
67
68@subcommand.usage('[args ...]')
dimu833c94c2017-01-18 17:36:15 -080069def CMDbranchinfo(parser, args):
70 parser.add_option('--branch', dest='branch', help='branch name')
71
72 (opt, args) = parser.parse_args(args)
73 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicc99efb22020-03-17 00:35:34 +000074 project = quote_plus(opt.project)
75 branch = quote_plus(opt.branch)
dimu833c94c2017-01-18 17:36:15 -080076 result = gerrit_util.GetGerritBranch(host, project, branch)
77 logging.info(result)
78 write_result(result, opt)
79
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +000080
dimu833c94c2017-01-18 17:36:15 -080081@subcommand.usage('[args ...]')
82def CMDbranch(parser, args):
83 parser.add_option('--branch', dest='branch', help='branch name')
84 parser.add_option('--commit', dest='commit', help='commit hash')
85
86 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +000087 assert opt.project, "--project not defined"
88 assert opt.branch, "--branch not defined"
89 assert opt.commit, "--commit not defined"
dimu833c94c2017-01-18 17:36:15 -080090
Josip Sokcevicc99efb22020-03-17 00:35:34 +000091 project = quote_plus(opt.project)
dimu833c94c2017-01-18 17:36:15 -080092 host = urlparse.urlparse(opt.host).netloc
Josip Sokcevicc99efb22020-03-17 00:35:34 +000093 branch = quote_plus(opt.branch)
94 commit = quote_plus(opt.commit)
dimu833c94c2017-01-18 17:36:15 -080095 result = gerrit_util.CreateGerritBranch(host, project, branch, commit)
96 logging.info(result)
97 write_result(result, opt)
98
99
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200100@subcommand.usage('[args ...]')
Josip Sokcevicdf9a8022020-12-08 00:10:19 +0000101def CMDhead(parser, args):
102 parser.add_option('--branch', dest='branch', help='branch name')
103
104 (opt, args) = parser.parse_args(args)
105 assert opt.project, "--project not defined"
106 assert opt.branch, "--branch not defined"
107
108 project = quote_plus(opt.project)
109 host = urlparse.urlparse(opt.host).netloc
110 branch = quote_plus(opt.branch)
111 result = gerrit_util.UpdateHead(host, project, branch)
112 logging.info(result)
113 write_result(result, opt)
114
115
116@subcommand.usage('[args ...]')
117def CMDheadinfo(parser, args):
118
119 (opt, args) = parser.parse_args(args)
120 assert opt.project, "--project not defined"
121
122 project = quote_plus(opt.project)
123 host = urlparse.urlparse(opt.host).netloc
124 result = gerrit_util.GetHead(host, project)
125 logging.info(result)
126 write_result(result, opt)
127
128
129@subcommand.usage('[args ...]')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200130def CMDchanges(parser, args):
131 parser.add_option('-p', '--param', dest='params', action='append',
132 help='repeatable query parameter, format: -p key=value')
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200133 parser.add_option('-o', '--o-param', dest='o_params', action='append',
134 help='gerrit output parameters, e.g. ALL_REVISIONS')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200135 parser.add_option('--limit', dest='limit', type=int,
136 help='maximum number of results to return')
137 parser.add_option('--start', dest='start', type=int,
138 help='how many changes to skip '
139 '(starting with the most recent)')
140
141 (opt, args) = parser.parse_args(args)
Mike Frysinger8820ab82020-11-25 00:52:31 +0000142 for p in opt.params:
143 assert '=' in p, '--param is key=value, not "%s"' % p
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200144
145 result = gerrit_util.QueryChanges(
146 urlparse.urlparse(opt.host).netloc,
147 list(tuple(p.split('=', 1)) for p in opt.params),
Paweł Hajdan, Jr24025d32017-07-11 16:38:21 +0200148 start=opt.start, # Default: None
149 limit=opt.limit, # Default: None
150 o_params=opt.o_params, # Default: None
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200151 )
152 logging.info('Change query returned %d changes.', len(result))
153 write_result(result, opt)
154
155
LaMont Jones9eed4232021-04-02 16:29:49 +0000156@subcommand.usage('[args ...]')
Marco Georgaklis85557a02021-06-03 15:56:54 +0000157def CMDrelatedchanges(parser, args):
158 parser.add_option('-c', '--change', type=str, help='change id')
159 parser.add_option('-r', '--revision', type=str, help='revision id')
160
161 (opt, args) = parser.parse_args(args)
162
163 result = gerrit_util.GetRelatedChanges(
164 urlparse.urlparse(opt.host).netloc,
165 change=opt.change,
166 revision=opt.revision,
167 )
168 logging.info(result)
169 write_result(result, opt)
170
171
172@subcommand.usage('[args ...]')
LaMont Jones9eed4232021-04-02 16:29:49 +0000173def CMDcreatechange(parser, args):
174 parser.add_option('-s', '--subject', help='subject for change')
175 parser.add_option('-b',
176 '--branch',
177 default='main',
178 help='target branch for change')
179 parser.add_option(
180 '-p',
181 '--param',
182 dest='params',
183 action='append',
184 help='repeatable field value parameter, format: -p key=value')
185
186 (opt, args) = parser.parse_args(args)
187 for p in opt.params:
188 assert '=' in p, '--param is key=value, not "%s"' % p
189
190 result = gerrit_util.CreateChange(
191 urlparse.urlparse(opt.host).netloc,
192 opt.project,
193 branch=opt.branch,
194 subject=opt.subject,
195 params=list(tuple(p.split('=', 1)) for p in opt.params),
196 )
197 logging.info(result)
198 write_result(result, opt)
199
200
201@subcommand.usage('[args ...]')
202def CMDchangeedit(parser, args):
203 parser.add_option('-c', '--change', type=int, help='change number')
204 parser.add_option('--path', help='path for file')
205 parser.add_option('--file', help='file to place at |path|')
206
207 (opt, args) = parser.parse_args(args)
208
209 with open(opt.file) as f:
210 data = f.read()
211 result = gerrit_util.ChangeEdit(
212 urlparse.urlparse(opt.host).netloc, opt.change, opt.path, data)
213 logging.info(result)
214 write_result(result, opt)
215
216
217@subcommand.usage('[args ...]')
218def CMDpublishchangeedit(parser, args):
219 parser.add_option('-c', '--change', type=int, help='change number')
220 parser.add_option('--notify', help='whether to notify')
221
222 (opt, args) = parser.parse_args(args)
223
224 result = gerrit_util.PublishChangeEdit(
225 urlparse.urlparse(opt.host).netloc, opt.change, opt.notify)
226 logging.info(result)
227 write_result(result, opt)
228
229
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000230@subcommand.usage('')
231def CMDabandon(parser, args):
232 parser.add_option('-c', '--change', type=int, help='change number')
233 parser.add_option('-m', '--message', default='', help='reason for abandoning')
234
235 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000236 assert opt.change, "-c not defined"
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000237 result = gerrit_util.AbandonChange(
238 urlparse.urlparse(opt.host).netloc,
239 opt.change, opt.message)
240 logging.info(result)
241 write_result(result, opt)
242
243
Josip Sokcevice1a98942021-04-07 21:35:29 +0000244@subcommand.usage('''Mass abandon changes
245
246Mass abandon abandons CLs that match search criteria provided by user. Before
247any change is actually abandoned, user is presented with a list of CLs that
248will be affected if user confirms. User can skip confirmation by passing --force
249parameter.
250
251The script can abandon up to 100 CLs per invocation.
252
253Examples:
254gerrit_client.py mass-abandon --host https://HOST -p 'project=repo2'
255gerrit_client.py mass-abandon --host https://HOST -p 'message=testing'
Josip Sokcevicf5c6d8a2021-05-12 18:23:24 +0000256gerrit_client.py mass-abandon --host https://HOST -p 'is=wip' -p 'age=1y'
Josip Sokcevice1a98942021-04-07 21:35:29 +0000257''')
258def CMDmass_abandon(parser, args):
259 parser.add_option('-p',
260 '--param',
261 dest='params',
262 action='append',
263 default=[],
264 help='repeatable query parameter, format: -p key=value')
265 parser.add_option('-m', '--message', default='', help='reason for abandoning')
266 parser.add_option('-f',
267 '--force',
268 action='store_true',
269 help='Don\'t prompt for confirmation')
270
271 opt, args = parser.parse_args(args)
272
273 for p in opt.params:
274 assert '=' in p, '--param is key=value, not "%s"' % p
275 search_query = list(tuple(p.split('=', 1)) for p in opt.params)
276 if not any(t for t in search_query if t[0] == 'owner'):
277 # owner should always be present when abandoning changes
278 search_query.append(('owner', 'me'))
279 search_query.append(('status', 'open'))
280 logging.info("Searching for: %s" % search_query)
281
282 host = urlparse.urlparse(opt.host).netloc
283
284 result = gerrit_util.QueryChanges(
285 host,
286 search_query,
287 # abandon at most 100 changes as not all Gerrit instances support
288 # unlimited results.
289 limit=100,
290 )
291 if len(result) == 0:
292 logging.warn("Nothing to abandon")
293 return
294
295 logging.warn("%s CLs match search query: " % len(result))
296 for change in result:
297 logging.warn("[ID: %d] %s" % (change['_number'], change['subject']))
298
299 if not opt.force:
300 q = raw_input(
301 'Do you want to move forward with abandoning? [y to confirm] ').strip()
302 if q not in ['y', 'Y']:
303 logging.warn("Aborting...")
304 return
305
306 for change in result:
307 logging.warning("Abandoning: %s" % change['subject'])
308 gerrit_util.AbandonChange(host, change['id'], opt.message)
309
310 logging.warning("Done")
311
312
dimu833c94c2017-01-18 17:36:15 -0800313class OptionParser(optparse.OptionParser):
314 """Creates the option parse and add --verbose support."""
315 def __init__(self, *args, **kwargs):
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000316 optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs)
dimu833c94c2017-01-18 17:36:15 -0800317 self.add_option(
318 '--verbose', action='count', default=0,
319 help='Use 2 times for more debugging info')
320 self.add_option('--host', dest='host', help='Url of host.')
321 self.add_option('--project', dest='project', help='project name')
322 self.add_option(
323 '--json_file', dest='json_file', help='output json filepath')
324
325 def parse_args(self, args=None, values=None):
326 options, args = optparse.OptionParser.parse_args(self, args, values)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000327 # Host is always required
328 assert options.host, "--host not defined."
dimu833c94c2017-01-18 17:36:15 -0800329 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
330 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
331 return options, args
332
333
334def main(argv):
335 if sys.hexversion < 0x02060000:
336 print('\nYour python version %s is unsupported, please upgrade.\n'
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +0000337 % (sys.version.split(' ', 1)[0],),
dimu833c94c2017-01-18 17:36:15 -0800338 file=sys.stderr)
339 return 2
340 dispatcher = subcommand.CommandDispatcher(__name__)
341 return dispatcher.execute(OptionParser(), argv)
342
343
344if __name__ == '__main__':
345 # These affect sys.stdout so do it outside of main() to simplify mocks in
346 # unit testing.
347 fix_encoding.fix_encoding()
348 setup_color.init()
349 try:
350 sys.exit(main(sys.argv[1:]))
351 except KeyboardInterrupt:
352 sys.stderr.write('interrupted\n')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200353 sys.exit(1)