Skip to content

Commit e0fee9f

Browse files
gavinmakLUCI CQ
authored andcommitted
Add BatchListBestOwners and ListBestOwners to GerritClient
ListOwners returns all OWNERS for a file, which is undesirable in cases when only direct OWNERS are needed. Bug: 1351212, 1351519 Change-Id: I693b6645c780aa589e8ab24d0b58691f4aeb30f6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3823299 Reviewed-by: Aravind Vasudevan <[email protected]> Commit-Queue: Gavin Mak <[email protected]>
1 parent f53bb83 commit e0fee9f

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

gerrit_util.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,23 @@ def IsCodeOwnersEnabledOnRepo(host, repo):
874874
return not config['status'].get('disabled', False)
875875

876876

877-
def GetOwnersForFile(host, project, branch, path, limit=100,
878-
resolve_all_users=True, seed=None, o_params=('DETAILS',)):
877+
def GetOwnersForFile(host,
878+
project,
879+
branch,
880+
path,
881+
limit=100,
882+
resolve_all_users=True,
883+
highest_score_only=False,
884+
seed=None,
885+
o_params=('DETAILS',)):
879886
"""Gets information about owners attached to a file."""
880887
path = 'projects/%s/branches/%s/code_owners/%s' % (
881888
urllib.parse.quote(project, ''),
882889
urllib.parse.quote(branch, ''),
883890
urllib.parse.quote(path, ''))
884891
q = ['resolve-all-users=%s' % json.dumps(resolve_all_users)]
892+
if highest_score_only:
893+
q.append('highest-score-only=%s' % json.dumps(highest_score_only))
885894
if seed:
886895
q.append('seed=%d' % seed)
887896
if limit:

owners_client.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,33 +167,54 @@ def __init__(self, host, project, branch):
167167
self._project = project
168168
self._branch = branch
169169
self._owners_cache = {}
170+
self._best_owners_cache = {}
170171

171172
# Seed used by Gerrit to shuffle code owners that have the same score. Can
172173
# be used to make the sort order stable across several requests, e.g. to get
173174
# the same set of random code owners for different file paths that have the
174175
# same code owners.
175176
self._seed = random.getrandbits(30)
176177

177-
def ListOwners(self, path):
178+
def _FetchOwners(self, path, cache, highest_score_only=False):
178179
# Always use slashes as separators.
179180
path = path.replace(os.sep, '/')
180-
if path not in self._owners_cache:
181+
if path not in cache:
181182
# GetOwnersForFile returns a list of account details sorted by order of
182183
# best reviewer for path. If owners have the same score, the order is
183184
# random, seeded by `self._seed`.
184-
data = gerrit_util.GetOwnersForFile(
185-
self._host, self._project, self._branch, path,
186-
resolve_all_users=False, seed=self._seed)
187-
self._owners_cache[path] = [
188-
d['account']['email']
189-
for d in data['code_owners']
190-
if 'account' in d and 'email' in d['account']
185+
data = gerrit_util.GetOwnersForFile(self._host,
186+
self._project,
187+
self._branch,
188+
path,
189+
resolve_all_users=False,
190+
highest_score_only=highest_score_only,
191+
seed=self._seed)
192+
cache[path] = [
193+
d['account']['email'] for d in data['code_owners']
194+
if 'account' in d and 'email' in d['account']
191195
]
192196
# If owned_by_all_users is true, add everyone as an owner at the end of
193197
# the owners list.
194198
if data.get('owned_by_all_users', False):
195-
self._owners_cache[path].append(self.EVERYONE)
196-
return self._owners_cache[path]
199+
cache[path].append(self.EVERYONE)
200+
return cache[path]
201+
202+
def ListOwners(self, path):
203+
return self._FetchOwners(path, self._owners_cache)
204+
205+
def ListBestOwners(self, path):
206+
return self._FetchOwners(path,
207+
self._best_owners_cache,
208+
highest_score_only=True)
209+
210+
def BatchListBestOwners(self, paths):
211+
"""List only the higest-scoring owners for a group of files.
212+
213+
Returns a dictionary {path: [owners]}.
214+
"""
215+
with git_common.ScopedPool(kind='threads') as pool:
216+
return dict(
217+
pool.imap_unordered(lambda p: (p, self.ListBestOwners(p)), paths))
197218

198219

199220
def GetCodeOwnersClient(root, upstream, host, project, branch):

tests/owners_client_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def testListOwners(self):
102102
# Always use slashes as separators.
103103
gerrit_util.GetOwnersForFile.assert_called_once_with(
104104
'host', 'project', 'branch', 'bar/everyone/foo.txt',
105-
resolve_all_users=False, seed=mock.ANY)
105+
resolve_all_users=False, highest_score_only=False, seed=mock.ANY)
106106

107107
def testListOwnersOwnedByAll(self):
108108
mock.patch(

0 commit comments

Comments
 (0)