-
Notifications
You must be signed in to change notification settings - Fork 2
/
gerrit
executable file
·225 lines (187 loc) · 7.23 KB
/
gerrit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.
import argparse
import bugzilla
import configparser
import os
import pprint
import re
import subprocess
import sys
from typing import TypeVar, List
ListOrStr = TypeVar('ListOrStr', List[str], str)
class GerritError(Exception):
pass
def bug_id_from_branch() -> int:
branch_name = current_git_branch()
match = re.match('bug_(\d+)|.+\/bug\/(\d+)', branch_name)
if match is None:
raise GerritError("current branch %s not of format bug_NUMBER" % branch_name)
bug_id = match.group(match.lastindex)
return int(bug_id)
def bz_connection() -> bugzilla.RHBugzilla:
config = read_config()
bzapi = bugzilla.RHBugzilla(config['bugzilla']['url'])
if not bzapi.logged_in:
bzapi.interactive_login()
return bzapi
def config_file() -> str:
default_config_home = os.path.join(os.path.expanduser('~'), '.config')
config_home = os.environ.get('XDG_CONFIG_HOME', default_config_home)
return os.path.join(config_home, 'gerrit_workflow', 'config.ini')
def current_git_branch() -> str:
branch_name = execute_git_command('git rev-parse --abbrev-ref HEAD')
return branch_name
def execute_git_command(cmd: ListOrStr) -> str:
if isinstance(cmd, str):
cmd = cmd.split()
result = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False)
if 0 == result.returncode:
return result.stdout.decode('utf-8').strip()
else:
raise GerritError(result.stderr.decode('utf-8').strip())
def gerrit_server_host() -> str:
config = read_config()
return config['gerrit']['server']
def get_git_origin_url() -> str:
remote = execute_git_command('git remote')
if '' == remote:
raise GerritError("No git remote is set")
return execute_git_command('git remote get-url %s' % remote)
def get_head_commit_id() -> str:
return execute_git_command('git log -1 --pretty=format:"%h"')
def read_config():
config = configparser.ConfigParser()
config.read(config_file())
return config
def reviewers() -> List[str]:
config = read_config()
reviewers = config['gerrit']['reviewers']
return reviewers.split()
def submit_git_review() -> str:
command = ['git-review', '--y', '--reviewer'] + reviewers()
result = execute_git_command(command)
urlmatch = re.compile('https://%s/gerrit/(\d+)' % gerrit_server_host())
matching = [s for s in result.split() if urlmatch.match(s)]
gerrit_id = urlmatch.match(matching[0]).group(1)
return gerrit_id
def verify_git_project():
config = read_config()
expected_origin_url = config['gerrit']['origin_url']
origin_url = get_git_origin_url()
if origin_url != expected_origin_url:
raise GerritError("Origin of this git tree is not %s, is instead: %s" % (expected_origin_url, origin_url))
def verify_bug(bug_id: int):
bzapi = bz_connection()
bug = bzapi.getbug(bug_id)
config = read_config()
product = config['bugzilla']['product']
if product != bug.product:
raise GerritError("Bug %s must be for %s, not %s" % (bug_id, product, bug.product))
def take_bug(args: argparse.Namespace):
"""
Attemps to assign self to a bug.
First check that current branch is develop
"""
verify_git_project()
verify_bug(args.bug_id)
if 'develop' != current_git_branch():
latest_devel(args)
bzapi = bz_connection()
config = read_config()
update = bzapi.build_update(status='ASSIGNED',
assigned_to=config['bugzilla']['login_name'])
bzapi.update_bugs([args.bug_id], update)
msg = execute_git_command('git checkout -b bug_%s' % args.bug_id)
print(msg)
def latest_devel(args: argparse.Namespace):
"""
Reverts all current changed files, checks
out develop branch, pulls latest from gerrit
"""
execute_git_command('git reset --hard HEAD')
execute_git_command('git checkout develop')
execute_git_command('git pull')
def merge(args: argparse.Namespace):
"""
Merges the current review, and moves bug to MODIFIED
"""
verify_git_project()
bug_id = bug_id_from_branch()
verify_bug(bug_id)
commit_id = get_head_commit_id()
execute_git_command(['ssh',
'-x',
gerrit_server_host(),
'gerrit',
'review',
'--submit',
commit_id])
bzapi = bz_connection()
update = bzapi.build_update(status="MODIFIED")
bzapi.update_bugs([bug_id], update)
def only_submit(args: argparse.Namespace):
"""
Submits a git review, but does not affect bug
"""
verify_git_project()
submit_git_review()
def submit(args: argparse.Namespace):
"""
Submits a git review and moves bug into POST
"""
verify_git_project()
bug_id = bug_id_from_branch()
verify_bug(bug_id)
gerrit_id = submit_git_review()
bzapi = bz_connection()
update = bzapi.build_update(status="POST")
bzapi.update_bugs([bug_id], update)
config = read_config()
bzapi.add_external_tracker(bug_id,
gerrit_id,
ext_type_description=config['bugzilla']['external_tracker_name'])
def main():
parser = argparse.ArgumentParser(description='Gerrit Workflow Helper')
subparsers = parser.add_subparsers(title='commands')
parser_take = subparsers.add_parser('take-bug',
help='Add self to bug',
description=take_bug.__doc__)
parser_take.add_argument('bug_id',
type=int)
parser_take.set_defaults(func=take_bug)
parser_submit = subparsers.add_parser('submit',
help='Submits code for gerrit review',
description=submit.__doc__)
parser_submit.set_defaults(func=submit)
parser_only = subparsers.add_parser('only-submit',
help='Submits code for gerrit review only',
description=only_submit.__doc__)
parser_only.set_defaults(func=only_submit)
parser_merge = subparsers.add_parser('merge',
help='Merges current commit',
description=submit.__doc__)
parser_merge.set_defaults(func=merge)
parser_devel = subparsers.add_parser('latest-devel',
help='Switch to develop branch, pull latest changes',
description=latest_devel.__doc__)
parser_devel.set_defaults(func=latest_devel)
if len(sys.argv) == 1:
parser.print_help()
return 1
args = parser.parse_args()
return args.func(args)
if __name__ == '__main__':
try:
sys.exit(main())
except GerritError as e:
print(e, file=sys.stderr)
sys.exit(1)