-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathowners_finder.py
345 lines (281 loc) · 10.6 KB
/
owners_finder.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
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Interactive tool for finding reviewers/owners for a change."""
from __future__ import print_function
import os
import copy
import git_common
import gclient_utils
def first(iterable):
for element in iterable:
return element
class OwnersFinder(object):
COLOR_LINK = '\033[4m'
COLOR_BOLD = '\033[1;32m'
COLOR_GREY = '\033[0;37m'
COLOR_RESET = '\033[0m'
indentation = 0
def __init__(self, files, author, reviewers, owners_client,
email_postfix='@chromium.org',
disable_color=False,
ignore_author=False):
self.email_postfix = email_postfix
if os.name == 'nt' or disable_color:
self.COLOR_LINK = ''
self.COLOR_BOLD = ''
self.COLOR_GREY = ''
self.COLOR_RESET = ''
self.author = author
filtered_files = files
reviewers = list(reviewers)
if author and not ignore_author:
reviewers.append(author)
# Eliminate files that existing reviewers can review.
self.owners_client = owners_client
approval_status = self.owners_client.GetFilesApprovalStatus(
filtered_files, reviewers, [])
filtered_files = [
f for f in filtered_files
if approval_status[f] != self.owners_client.APPROVED]
# If some files are eliminated.
if len(filtered_files) != len(files):
files = filtered_files
self.files_to_owners = self.owners_client.BatchListOwners(files)
self.owners_to_files = {}
self._map_owners_to_files()
self.original_files_to_owners = copy.deepcopy(self.files_to_owners)
# This is the queue that will be shown in the interactive questions.
# It is initially sorted by the score in descending order. In the
# interactive questions a user can choose to "defer" its decision, then the
# owner will be put to the end of the queue and shown later.
self.owners_queue = []
self.unreviewed_files = set()
self.reviewed_by = {}
self.selected_owners = set()
self.deselected_owners = set()
self.reset()
def run(self):
self.reset()
while self.owners_queue and self.unreviewed_files:
owner = self.owners_queue[0]
if (owner in self.selected_owners) or (owner in self.deselected_owners):
continue
if not any((file_name in self.unreviewed_files)
for file_name in self.owners_to_files[owner]):
self.deselect_owner(owner)
continue
self.print_info(owner)
while True:
inp = self.input_command(owner)
if inp in ('y', 'yes'):
self.select_owner(owner)
break
if inp in ('n', 'no'):
self.deselect_owner(owner)
break
if inp in ('', 'd', 'defer'):
self.owners_queue.append(self.owners_queue.pop(0))
break
if inp in ('f', 'files'):
self.list_files()
break
if inp in ('o', 'owners'):
self.list_owners(self.owners_queue)
break
if inp in ('p', 'pick'):
self.pick_owner(gclient_utils.AskForData('Pick an owner: '))
break
if inp.startswith('p ') or inp.startswith('pick '):
self.pick_owner(inp.split(' ', 2)[1].strip())
break
if inp in ('r', 'restart'):
self.reset()
break
if inp in ('q', 'quit'):
# Exit with error
return 1
self.print_result()
return 0
def _map_owners_to_files(self):
for file_name in self.files_to_owners:
for owner in self.files_to_owners[file_name]:
self.owners_to_files.setdefault(owner, set())
self.owners_to_files[owner].add(file_name)
def reset(self):
self.files_to_owners = copy.deepcopy(self.original_files_to_owners)
self.unreviewed_files = set(self.files_to_owners.keys())
self.reviewed_by = {}
self.selected_owners = set()
self.deselected_owners = set()
# Randomize owners' names so that if many reviewers have identical scores
# they will be randomly ordered to avoid bias.
owners = list(self.owners_client.ScoreOwners(self.files_to_owners.keys()))
if self.author and self.author in owners:
owners.remove(self.author)
self.owners_queue = owners
self.find_mandatory_owners()
def select_owner(self, owner, findMandatoryOwners=True):
if owner in self.selected_owners or owner in self.deselected_owners\
or not (owner in self.owners_queue):
return
self.writeln('Selected: ' + owner)
self.owners_queue.remove(owner)
self.selected_owners.add(owner)
for file_name in filter(
lambda file_name: file_name in self.unreviewed_files,
self.owners_to_files[owner]):
self.unreviewed_files.remove(file_name)
self.reviewed_by[file_name] = owner
if findMandatoryOwners:
self.find_mandatory_owners()
def deselect_owner(self, owner, findMandatoryOwners=True):
if owner in self.selected_owners or owner in self.deselected_owners\
or not (owner in self.owners_queue):
return
self.writeln('Deselected: ' + owner)
self.owners_queue.remove(owner)
self.deselected_owners.add(owner)
for file_name in self.owners_to_files[owner] & self.unreviewed_files:
self.files_to_owners[file_name].remove(owner)
if findMandatoryOwners:
self.find_mandatory_owners()
def find_mandatory_owners(self):
continues = True
for owner in self.owners_queue:
if owner in self.selected_owners:
continue
if owner in self.deselected_owners:
continue
if len(self.owners_to_files[owner] & self.unreviewed_files) == 0:
self.deselect_owner(owner, False)
while continues:
continues = False
for file_name in filter(
lambda file_name: len(self.files_to_owners[file_name]) == 1,
self.unreviewed_files):
owner = first(self.files_to_owners[file_name])
self.select_owner(owner, False)
continues = True
break
def print_file_info(self, file_name, except_owner=''):
if file_name not in self.unreviewed_files:
self.writeln(self.greyed(file_name +
' (by ' +
self.bold_name(self.reviewed_by[file_name]) +
')'))
else:
if len(self.files_to_owners[file_name]) <= 3:
other_owners = []
for ow in self.files_to_owners[file_name]:
if ow != except_owner:
other_owners.append(self.bold_name(ow))
self.writeln(file_name +
' [' + (', '.join(other_owners)) + ']')
else:
self.writeln(file_name + ' [' +
self.bold(str(len(self.files_to_owners[file_name]))) +
']')
def print_file_info_detailed(self, file_name):
self.writeln(file_name)
self.indent()
for ow in sorted(self.files_to_owners[file_name]):
if ow in self.deselected_owners:
self.writeln(self.bold_name(self.greyed(ow)))
elif ow in self.selected_owners:
self.writeln(self.bold_name(self.greyed(ow)))
else:
self.writeln(self.bold_name(ow))
self.unindent()
def print_owned_files_for(self, owner):
# Print owned files
self.writeln(self.bold_name(owner))
self.writeln(self.bold_name(owner) + ' owns ' +
str(len(self.owners_to_files[owner])) + ' file(s):')
self.indent()
for file_name in sorted(self.owners_to_files[owner]):
self.print_file_info(file_name, owner)
self.unindent()
self.writeln()
def list_owners(self, owners_queue):
if (len(self.owners_to_files) - len(self.deselected_owners) -
len(self.selected_owners)) > 3:
for ow in owners_queue:
if ow not in self.deselected_owners and ow not in self.selected_owners:
self.writeln(self.bold_name(ow))
else:
for ow in owners_queue:
if ow not in self.deselected_owners and ow not in self.selected_owners:
self.writeln()
self.print_owned_files_for(ow)
def list_files(self):
self.indent()
if len(self.unreviewed_files) > 5:
for file_name in sorted(self.unreviewed_files):
self.print_file_info(file_name)
else:
for file_name in self.unreviewed_files:
self.print_file_info_detailed(file_name)
self.unindent()
def pick_owner(self, ow):
# Allowing to omit domain suffixes
if ow not in self.owners_to_files:
if ow + self.email_postfix in self.owners_to_files:
ow += self.email_postfix
if ow not in self.owners_to_files:
self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
'It\'s an invalid name or not related to the change list.')
return False
if ow in self.selected_owners:
self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
'It\'s already selected.')
return False
if ow in self.deselected_owners:
self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually.' +
'It\'s already unselected.')
return False
self.select_owner(ow)
return True
def print_result(self):
# Print results
self.writeln()
self.writeln()
if len(self.selected_owners) == 0:
self.writeln('This change list already has owner-reviewers for all '
'files.')
self.writeln('Use --ignore-current if you want to ignore them.')
else:
self.writeln('** You selected these owners **')
self.writeln()
for owner in self.selected_owners:
self.writeln(self.bold_name(owner) + ':')
self.indent()
for file_name in sorted(self.owners_to_files[owner]):
self.writeln(file_name)
self.unindent()
def bold(self, text):
return self.COLOR_BOLD + text + self.COLOR_RESET
def bold_name(self, name):
return (self.COLOR_BOLD +
name.replace(self.email_postfix, '') + self.COLOR_RESET)
def greyed(self, text):
return self.COLOR_GREY + text + self.COLOR_RESET
def indent(self):
self.indentation += 1
def unindent(self):
self.indentation -= 1
def print_indent(self):
return ' ' * self.indentation
def writeln(self, text=''):
print(self.print_indent() + text)
def hr(self):
self.writeln('=====================')
def print_info(self, owner):
self.hr()
self.writeln(
self.bold(str(len(self.unreviewed_files))) + ' file(s) left.')
self.print_owned_files_for(owner)
def input_command(self, owner):
self.writeln('Add ' + self.bold_name(owner) + ' as your reviewer? ')
return gclient_utils.AskForData(
'[yes/no/Defer/pick/files/owners/quit/restart]: ').lower()