-
Notifications
You must be signed in to change notification settings - Fork 9
/
gerrit.py
347 lines (294 loc) · 11.2 KB
/
gerrit.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from datetime import datetime
import json
import logging
from logging import handlers
import os
from pygerrit2 import GerritRestAPI, Anonymous, HTTPBasicAuthFromNetrc
import pprint
import requests
import urllib
logger = logging.getLogger('rom')
logger.setLevel(logging.DEBUG) # leave this to handlers
def parse_gerrit_timestamp(ts):
return datetime.strptime(ts[:-10], '%Y-%m-%d %H:%M:%S')
class AuthFromNetrc(HTTPBasicAuthFromNetrc):
def __init__(self, netrc, url, use_internal):
# This is a nasty little hack, that is probably going to be forgotten the
# next time my .netrc file goes away. If we want an "internal" login,
# append '.internal' to the host of the internal login in your .netrc file
if use_internal:
parsed = urllib.parse.urlsplit(url)
url = parsed.scheme + '://' + parsed.netloc + '.internal' + parsed.path
try:
old_netrc = os.environ.get('NETRC')
override_netrc = netrc and os.path.exists(netrc)
if override_netrc:
logger.debug('Using netrc={}'.format(netrc))
os.environ['NETRC'] = netrc
super().__init__(url)
finally:
if override_netrc:
os.environ.pop('NETRC')
if old_netrc:
os.environ['NETRC'] = old_netrc
class GerritMessage(object):
def __init__(self, rest):
# https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-message-info
self.id = rest['id']
self.revision_num = rest['_revision_number']
self.tag = rest.get('tag')
self.message = rest['message']
self.date = parse_gerrit_timestamp(rest['date'])
self.comments = []
class GerritComment(object):
def __init__(self, path, rest):
# https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-message-info
self.path = path
self.id = rest['id']
self.message_id = rest['change_message_id']
if rest.get('line'):
self.line = rest['line']
else:
self.line = 0
self.author = rest['author']['name']
self.message = rest['message']
class GerritRevision(object):
def __init__(self, id, rest):
# http://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#revision-info
self.id = id
self.ref = rest['ref']
self.number = rest['_number']
self.uploader_name = ''.join(rest['uploader']['name'])
self.uploader_email = ''.join(rest['uploader']['email'])
if rest.get('commit_with_footers'):
self.commit_message = ''.join(rest['commit_with_footers'])
def __eq__(self, other):
return self.id == other.id and self.number == other.number
def __hash__(self):
return hash((self.id, self.number))
def __str__(self):
return 'id={}, rev={}'.format(self.id, self.number)
class GerritChange(object):
def __init__(self, url, rest):
# https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#change-info
self.base_url = url
self.id = rest['id']
self.change_id = rest['change_id']
self.number = rest['_number']
# yyyy-mm-dd hh:mm:ss.fffffffff
self.last_updated = parse_gerrit_timestamp(rest['updated'])
self.status = rest['status']
self.subject = rest['subject']
self.topic = rest.get('topic')
self.project = rest['project']
self.branch = rest['branch']
self.current_revision = GerritRevision(
rest['current_revision'],
rest['revisions'][rest['current_revision']])
self.revisions = []
for r in rest['revisions']:
self.revisions.append(GerritRevision(r, rest['revisions'][r]))
self.messages = {}
for m in rest['messages']:
msg = GerritMessage(m)
self.messages[msg.id] = msg
self.vote_code_review = []
self.__parse_votes(rest, self.vote_code_review, 'Code-Review')
self.vote_commit_queue = []
self.__parse_votes(rest, self.vote_commit_queue, 'Commit-Queue')
self.vote_verified = []
self.__parse_votes(rest, self.vote_verified, 'Verified')
def __eq__(self, other):
return self.change_id == other.change_id and \
self.current_revision == other.current_revision
def __hash__(self):
return hash((self.change_id, self.current_revision))
def __str__(self):
return '"{}" ({})'.format(self.subject, self.url())
def __parse_votes(self, rest, array, label):
if not rest.get('labels') or not rest['labels'].get(label):
return False
values = rest['labels'][label].get('all')
if not values:
return True
for l in values:
value = l.get('value')
if value:
array.append(value)
return True
def url(self):
return '{}/c/{}/+/{}/{}'.format(self.base_url, self.project, self.number,
self.current_revision.number)
def get_messages(self):
return sorted(self.messages.values(), key=lambda msg: msg.date)
def is_merged(self):
return self.status == 'MERGED'
def is_reviewed(self):
return 2 in self.vote_code_review
def is_verified(self):
return 1 in self.vote_verified
def is_cq_ready(self):
return 1 in self.vote_commit_queue or 2 in self.vote_commit_queue
def add_comments(self, rest):
for (path, comments) in rest.items():
for c in comments:
cmt = GerritComment(path, c)
if self.messages.get(cmt.message_id):
self.messages[cmt.message_id].comments.append(cmt)
else:
logger.error('Could not find message id {} on change {}'.format(cmt.message_id, self.url()))
return
class Gerrit(object):
def __init__(self, url, netrc=None, use_internal=False):
if netrc:
auth = AuthFromNetrc(netrc, url, use_internal)
else:
logger.debug('No netrc specified. Using Gerrit anonymously.')
auth = Anonymous()
self.timeout = 90
self.rest = GerritRestAPI(url=url, auth=auth)
self.url = url
self.change_options = ['CURRENT_REVISION', 'MESSAGES', 'DETAILED_LABELS',
'DETAILED_ACCOUNTS', 'COMMIT_FOOTERS']
def get_change(self, change_id, rev_num=None):
options = self.change_options
if rev_num != None:
options += ['ALL_REVISIONS']
uri = '/changes/{}?o={}'.format(change_id, '&o='.join(options))
rest = self.rest.get(uri, timeout=self.timeout)
c = GerritChange(self.url, rest)
# The modifications to change here shouldn't be relied upon, but rolling
# back to a previous revision is useful for testing. So we'll do our best
# to act like the requested revision is the current_revision and hope
# nothing downstream of us gets too confused
if rev_num != None:
uri = '/changes/{}/revisions/{}/commit'.format(change_id, rev_num)
rest = self.rest.get(uri, timeout=self.timeout)
for r in c.revisions:
if int(r.number) != int(rev_num):
continue
r.commit_message = rest['message']
c.subject = rest['subject']
c.current_revision = r
uri = '/changes/{}/comments/'.format(change_id)
rest = self.rest.get(uri, timeout=self.timeout)
#pprint.PrettyPrinter(indent=4).pprint(rest)
c.add_comments(rest)
return c
def get_ancestor_changes(self, change):
uri = '/changes/{}/revisions/current/related'.format(change.id)
related_changes = self.rest.get(uri, timeout=self.timeout)['changes']
changes = []
parents = []
for c in related_changes:
if c['change_id'] == change.change_id:
parents = c['commit']['parents']
break
while True:
new_parents = []
for p in parents:
for c in related_changes:
if c['commit']['commit'] == p['commit']:
new_parents += c['commit']['parents']
changes.append(self.get_change(c['_change_number']))
break
if new_parents:
parents = new_parents
else:
break
return changes
def query_changes(self, status=None, message=None, after=None, age_days=None,
change_id=None, change_num=None, project=None, owner=None,
branches=None):
query = []
if message:
query.append('message:"{}"'.format(urllib.parse.quote(message)))
if status:
query.append('status:{}'.format(status))
if after:
query.append('after:"{}"'.format(after.isoformat()))
if age_days:
query.append('age:{}d'.format(age_days))
if change_id:
query.append('change:{}'.format(change_id))
if change_num:
query.append('change:{}'.format(change_num))
if project:
query.append('project:{}'.format(project))
if owner:
query.append('owner:{}'.format(owner))
if branches:
if len(branches) == 1:
q = 'branch:{}'.format(branches[0])
else:
q = '(branch:'
q += ' OR branch:'.join(branches)
q += ')'
query.append(q)
uri = '/changes/?q={}&o={}'.format('+'.join(query),
'&o='.join(self.change_options))
changes = []
for c in self.rest.get(uri, timeout=self.timeout):
changes.append(GerritChange(self.url, c))
return changes
def get_patch(self, change):
uri = '/changes/{}/revisions/{}/patch'.format(change.id,
change.current_revision.id)
return self.rest.get(uri, timeout=self.timeout)
def get_messages(self, change):
uri = '/changes/{}/messages'.format(change.id)
return self.rest.get(uri, timeout=self.timeout)
def set_topic(self, change):
# https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#set-topic
uri = '/changes/{}/topic'.format(change.id)
options = {
'topic': change.topic
}
try:
self.rest.put(uri, data=options, timeout=self.timeout)
return True
except requests.exceptions.HTTPError:
return False
def remove_reviewer(self, change):
uri = '/changes/{}/reviewers/self/delete'.format(change.id)
options = {
'notify': 'NONE',
}
try:
self.rest.post(uri, data=options, timeout=self.timeout)
return True
except requests.exceptions.HTTPError:
return False
def abandon(self, change):
uri = '/changes/{}/abandon'.format(change.id)
try:
self.rest.post(uri, timeout=self.timeout)
return True
except requests.exceptions.HTTPError:
return False
def review(self, change, tag, message, notify_owner, vote_code_review=None,
vote_verified=None, vote_cq_ready=None, inline_comments=None):
review = {
'tag': tag,
'message': message,
'notify': 'OWNER' if notify_owner else 'NONE',
'omit_duplicate_comments': True,
}
labels = {}
if vote_code_review != None:
labels['Code-Review'] = vote_code_review
if vote_verified != None:
labels['Verified'] = vote_verified
if vote_cq_ready != None:
labels['Commit-Queue'] = vote_cq_ready
if labels:
review['labels'] = labels
if inline_comments:
review['comments'] = inline_comments
#pprint.PrettyPrinter(indent=4).pprint(review)
#pprint.PrettyPrinter(indent=4).pprint(json.dumps(review))
uri = "changes/{}/revisions/{}/review".format(change.id,
change.current_revision.id)
return self.rest.post(uri, data=json.dumps(review),
headers={"Content-Type": "application/json"},
timeout=self.timeout)