blob: 7813bffb9b61dd8afa25f1abf3ddd45dd5d4bf8b [file] [log] [blame]
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00001# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Interactive tool for finding reviewers/owners for a change."""
6
Raul Tambre80ee78e2019-05-06 22:41:05 +00007from __future__ import print_function
8
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00009import os
10import copy
Gavin Makd36dbbd2021-01-25 19:34:58 +000011import owners_client
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000012
13
Gavin Makd36dbbd2021-01-25 19:34:58 +000014import git_common
Edward Lesmesae3586b2020-03-23 21:21:14 +000015import gclient_utils
16
17
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000018def first(iterable):
19 for element in iterable:
20 return element
21
22
23class OwnersFinder(object):
24 COLOR_LINK = '\033[4m'
25 COLOR_BOLD = '\033[1;32m'
26 COLOR_GREY = '\033[0;37m'
27 COLOR_RESET = '\033[0m'
28
29 indentation = 0
30
Edward Lemur707d70b2018-02-07 00:50:14 +010031 def __init__(self, files, local_root, author, reviewers,
dtu944b6052016-07-14 14:48:21 -070032 fopen, os_path,
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000033 email_postfix='@chromium.org',
Jochen Eisingerd0573ec2017-04-13 10:55:06 +020034 disable_color=False,
Sylvain Defresneb1f865d2019-02-12 12:38:22 +000035 override_files=None,
36 ignore_author=False):
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000037 self.email_postfix = email_postfix
38
39 if os.name == 'nt' or disable_color:
40 self.COLOR_LINK = ''
41 self.COLOR_BOLD = ''
42 self.COLOR_GREY = ''
43 self.COLOR_RESET = ''
44
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000045 self.os_path = os_path
46
47 self.author = author
48
49 filtered_files = files
50
Edward Lemur707d70b2018-02-07 00:50:14 +010051 reviewers = list(reviewers)
Sylvain Defresneb1f865d2019-02-12 12:38:22 +000052 if author and not ignore_author:
Edward Lemur707d70b2018-02-07 00:50:14 +010053 reviewers.append(author)
54
55 # Eliminate files that existing reviewers can review.
Gavin Makd36dbbd2021-01-25 19:34:58 +000056 self.client = owners_client.DepotToolsClient(
57 root=local_root,
58 branch=git_common.current_branch(),
59 fopen=fopen,
60 os_path=os_path)
61
62 approval_status = self.client.GetFilesApprovalStatus(
63 filtered_files, reviewers, [])
64 filtered_files = [
65 f for f in filtered_files
66 if approval_status[f] != owners_client.OwnersClient.APPROVED]
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000067
68 # If some files are eliminated.
69 if len(filtered_files) != len(files):
70 files = filtered_files
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000071
Gavin Makd36dbbd2021-01-25 19:34:58 +000072 self.files_to_owners = self.client.BatchListOwners(files)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000073
74 self.owners_to_files = {}
Gavin Makd36dbbd2021-01-25 19:34:58 +000075 self._map_owners_to_files()
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000076
77 self.original_files_to_owners = copy.deepcopy(self.files_to_owners)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000078
79 # This is the queue that will be shown in the interactive questions.
80 # It is initially sorted by the score in descending order. In the
81 # interactive questions a user can choose to "defer" its decision, then the
82 # owner will be put to the end of the queue and shown later.
83 self.owners_queue = []
84
85 self.unreviewed_files = set()
86 self.reviewed_by = {}
87 self.selected_owners = set()
88 self.deselected_owners = set()
89 self.reset()
90
91 def run(self):
92 self.reset()
93 while self.owners_queue and self.unreviewed_files:
94 owner = self.owners_queue[0]
95
96 if (owner in self.selected_owners) or (owner in self.deselected_owners):
97 continue
98
99 if not any((file_name in self.unreviewed_files)
100 for file_name in self.owners_to_files[owner]):
101 self.deselect_owner(owner)
102 continue
103
104 self.print_info(owner)
105
106 while True:
107 inp = self.input_command(owner)
108 if inp == 'y' or inp == 'yes':
109 self.select_owner(owner)
110 break
111 elif inp == 'n' or inp == 'no':
112 self.deselect_owner(owner)
113 break
114 elif inp == '' or inp == 'd' or inp == 'defer':
115 self.owners_queue.append(self.owners_queue.pop(0))
116 break
117 elif inp == 'f' or inp == 'files':
118 self.list_files()
119 break
120 elif inp == 'o' or inp == 'owners':
121 self.list_owners(self.owners_queue)
122 break
123 elif inp == 'p' or inp == 'pick':
Edward Lesmesae3586b2020-03-23 21:21:14 +0000124 self.pick_owner(gclient_utils.AskForData('Pick an owner: '))
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000125 break
126 elif inp.startswith('p ') or inp.startswith('pick '):
127 self.pick_owner(inp.split(' ', 2)[1].strip())
128 break
129 elif inp == 'r' or inp == 'restart':
130 self.reset()
131 break
132 elif inp == 'q' or inp == 'quit':
133 # Exit with error
134 return 1
135
136 self.print_result()
137 return 0
138
Gavin Makd36dbbd2021-01-25 19:34:58 +0000139 def _map_owners_to_files(self):
140 for file_name in self.files_to_owners:
141 for owner in self.files_to_owners[file_name]:
142 self.owners_to_files.setdefault(owner, set())
143 self.owners_to_files[owner].add(file_name)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000144
145 def reset(self):
146 self.files_to_owners = copy.deepcopy(self.original_files_to_owners)
147 self.unreviewed_files = set(self.files_to_owners.keys())
148 self.reviewed_by = {}
149 self.selected_owners = set()
150 self.deselected_owners = set()
151
Bruce Dawson37740e22019-11-14 00:27:44 +0000152 # Randomize owners' names so that if many reviewers have identical scores
153 # they will be randomly ordered to avoid bias.
Gavin Makd36dbbd2021-01-25 19:34:58 +0000154 owners = self.client.ScoreOwners(self.files_to_owners.keys())
155 if self.author and self.author in owners:
156 owners.remove(self.author)
157 self.owners_queue = owners
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000158 self.find_mandatory_owners()
159
160 def select_owner(self, owner, findMandatoryOwners=True):
161 if owner in self.selected_owners or owner in self.deselected_owners\
162 or not (owner in self.owners_queue):
163 return
164 self.writeln('Selected: ' + owner)
165 self.owners_queue.remove(owner)
166 self.selected_owners.add(owner)
167 for file_name in filter(
168 lambda file_name: file_name in self.unreviewed_files,
169 self.owners_to_files[owner]):
170 self.unreviewed_files.remove(file_name)
171 self.reviewed_by[file_name] = owner
172 if findMandatoryOwners:
173 self.find_mandatory_owners()
174
175 def deselect_owner(self, owner, findMandatoryOwners=True):
176 if owner in self.selected_owners or owner in self.deselected_owners\
177 or not (owner in self.owners_queue):
178 return
179 self.writeln('Deselected: ' + owner)
180 self.owners_queue.remove(owner)
181 self.deselected_owners.add(owner)
182 for file_name in self.owners_to_files[owner] & self.unreviewed_files:
183 self.files_to_owners[file_name].remove(owner)
184 if findMandatoryOwners:
185 self.find_mandatory_owners()
186
187 def find_mandatory_owners(self):
188 continues = True
189 for owner in self.owners_queue:
190 if owner in self.selected_owners:
191 continue
192 if owner in self.deselected_owners:
193 continue
194 if len(self.owners_to_files[owner] & self.unreviewed_files) == 0:
195 self.deselect_owner(owner, False)
196
197 while continues:
198 continues = False
199 for file_name in filter(
200 lambda file_name: len(self.files_to_owners[file_name]) == 1,
201 self.unreviewed_files):
202 owner = first(self.files_to_owners[file_name])
203 self.select_owner(owner, False)
204 continues = True
205 break
206
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000207 def print_file_info(self, file_name, except_owner=''):
208 if file_name not in self.unreviewed_files:
209 self.writeln(self.greyed(file_name +
210 ' (by ' +
211 self.bold_name(self.reviewed_by[file_name]) +
212 ')'))
213 else:
214 if len(self.files_to_owners[file_name]) <= 3:
215 other_owners = []
216 for ow in self.files_to_owners[file_name]:
217 if ow != except_owner:
218 other_owners.append(self.bold_name(ow))
219 self.writeln(file_name +
220 ' [' + (', '.join(other_owners)) + ']')
221 else:
222 self.writeln(file_name + ' [' +
223 self.bold(str(len(self.files_to_owners[file_name]))) +
224 ']')
225
226 def print_file_info_detailed(self, file_name):
227 self.writeln(file_name)
228 self.indent()
229 for ow in sorted(self.files_to_owners[file_name]):
230 if ow in self.deselected_owners:
231 self.writeln(self.bold_name(self.greyed(ow)))
232 elif ow in self.selected_owners:
233 self.writeln(self.bold_name(self.greyed(ow)))
234 else:
235 self.writeln(self.bold_name(ow))
236 self.unindent()
237
238 def print_owned_files_for(self, owner):
239 # Print owned files
Edward Lesmesc3c15a12021-01-19 20:19:14 +0000240 self.writeln(self.bold_name(owner))
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000241 self.writeln(self.bold_name(owner) + ' owns ' +
242 str(len(self.owners_to_files[owner])) + ' file(s):')
243 self.indent()
244 for file_name in sorted(self.owners_to_files[owner]):
245 self.print_file_info(file_name, owner)
246 self.unindent()
247 self.writeln()
248
249 def list_owners(self, owners_queue):
250 if (len(self.owners_to_files) - len(self.deselected_owners) -
251 len(self.selected_owners)) > 3:
252 for ow in owners_queue:
253 if ow not in self.deselected_owners and ow not in self.selected_owners:
Edward Lesmesc3c15a12021-01-19 20:19:14 +0000254 self.writeln(self.bold_name(ow))
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000255 else:
256 for ow in owners_queue:
257 if ow not in self.deselected_owners and ow not in self.selected_owners:
258 self.writeln()
259 self.print_owned_files_for(ow)
260
261 def list_files(self):
262 self.indent()
263 if len(self.unreviewed_files) > 5:
264 for file_name in sorted(self.unreviewed_files):
265 self.print_file_info(file_name)
266 else:
267 for file_name in self.unreviewed_files:
268 self.print_file_info_detailed(file_name)
269 self.unindent()
270
271 def pick_owner(self, ow):
272 # Allowing to omit domain suffixes
273 if ow not in self.owners_to_files:
274 if ow + self.email_postfix in self.owners_to_files:
275 ow += self.email_postfix
276
277 if ow not in self.owners_to_files:
278 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
279 'It\'s an invalid name or not related to the change list.')
280 return False
281 elif ow in self.selected_owners:
282 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually. ' +
283 'It\'s already selected.')
284 return False
285 elif ow in self.deselected_owners:
286 self.writeln('You cannot pick ' + self.bold_name(ow) + ' manually.' +
287 'It\'s already unselected.')
288 return False
289
290 self.select_owner(ow)
291 return True
292
293 def print_result(self):
294 # Print results
295 self.writeln()
296 self.writeln()
Bruce Dawson9b4a0572020-05-06 17:05:01 +0000297 if len(self.selected_owners) == 0:
298 self.writeln('This change list already has owner-reviewers for all '
299 'files.')
300 self.writeln('Use --ignore-current if you want to ignore them.')
301 else:
302 self.writeln('** You selected these owners **')
303 self.writeln()
304 for owner in self.selected_owners:
305 self.writeln(self.bold_name(owner) + ':')
306 self.indent()
307 for file_name in sorted(self.owners_to_files[owner]):
308 self.writeln(file_name)
309 self.unindent()
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000310
311 def bold(self, text):
312 return self.COLOR_BOLD + text + self.COLOR_RESET
313
314 def bold_name(self, name):
315 return (self.COLOR_BOLD +
316 name.replace(self.email_postfix, '') + self.COLOR_RESET)
317
318 def greyed(self, text):
319 return self.COLOR_GREY + text + self.COLOR_RESET
320
321 def indent(self):
322 self.indentation += 1
323
324 def unindent(self):
325 self.indentation -= 1
326
327 def print_indent(self):
328 return ' ' * self.indentation
329
330 def writeln(self, text=''):
Raul Tambre80ee78e2019-05-06 22:41:05 +0000331 print(self.print_indent() + text)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000332
333 def hr(self):
334 self.writeln('=====================')
335
336 def print_info(self, owner):
337 self.hr()
338 self.writeln(
339 self.bold(str(len(self.unreviewed_files))) + ' file(s) left.')
340 self.print_owned_files_for(owner)
341
342 def input_command(self, owner):
343 self.writeln('Add ' + self.bold_name(owner) + ' as your reviewer? ')
Edward Lesmesae3586b2020-03-23 21:21:14 +0000344 return gclient_utils.AskForData(
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +0000345 '[yes/no/Defer/pick/files/owners/quit/restart]: ').lower()