blob: 6a174b38430b69079382e0905ad5c4253511cd2e [file] [log] [blame]
dimu833c94c2017-01-18 17:36:15 -08001#!/usr/bin/python
2# 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
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000156@subcommand.usage('')
157def CMDabandon(parser, args):
158 parser.add_option('-c', '--change', type=int, help='change number')
159 parser.add_option('-m', '--message', default='', help='reason for abandoning')
160
161 (opt, args) = parser.parse_args(args)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000162 assert opt.change, "-c not defined"
Sergiy Belozorovfe347232019-02-27 15:07:33 +0000163 result = gerrit_util.AbandonChange(
164 urlparse.urlparse(opt.host).netloc,
165 opt.change, opt.message)
166 logging.info(result)
167 write_result(result, opt)
168
169
dimu833c94c2017-01-18 17:36:15 -0800170class OptionParser(optparse.OptionParser):
171 """Creates the option parse and add --verbose support."""
172 def __init__(self, *args, **kwargs):
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000173 optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs)
dimu833c94c2017-01-18 17:36:15 -0800174 self.add_option(
175 '--verbose', action='count', default=0,
176 help='Use 2 times for more debugging info')
177 self.add_option('--host', dest='host', help='Url of host.')
178 self.add_option('--project', dest='project', help='project name')
179 self.add_option(
180 '--json_file', dest='json_file', help='output json filepath')
181
182 def parse_args(self, args=None, values=None):
183 options, args = optparse.OptionParser.parse_args(self, args, values)
Josip Sokcevicc99efb22020-03-17 00:35:34 +0000184 # Host is always required
185 assert options.host, "--host not defined."
dimu833c94c2017-01-18 17:36:15 -0800186 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
187 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
188 return options, args
189
190
191def main(argv):
192 if sys.hexversion < 0x02060000:
193 print('\nYour python version %s is unsupported, please upgrade.\n'
Quinten Yearsleyd9cbe7a2019-09-03 16:49:11 +0000194 % (sys.version.split(' ', 1)[0],),
dimu833c94c2017-01-18 17:36:15 -0800195 file=sys.stderr)
196 return 2
197 dispatcher = subcommand.CommandDispatcher(__name__)
198 return dispatcher.execute(OptionParser(), argv)
199
200
201if __name__ == '__main__':
202 # These affect sys.stdout so do it outside of main() to simplify mocks in
203 # unit testing.
204 fix_encoding.fix_encoding()
205 setup_color.init()
206 try:
207 sys.exit(main(sys.argv[1:]))
208 except KeyboardInterrupt:
209 sys.stderr.write('interrupted\n')
Michael Achenbach6fbf12f2017-07-06 10:54:11 +0200210 sys.exit(1)