@@ -167,33 +167,54 @@ def __init__(self, host, project, branch):
167
167
self ._project = project
168
168
self ._branch = branch
169
169
self ._owners_cache = {}
170
+ self ._best_owners_cache = {}
170
171
171
172
# Seed used by Gerrit to shuffle code owners that have the same score. Can
172
173
# be used to make the sort order stable across several requests, e.g. to get
173
174
# the same set of random code owners for different file paths that have the
174
175
# same code owners.
175
176
self ._seed = random .getrandbits (30 )
176
177
177
- def ListOwners (self , path ):
178
+ def _FetchOwners (self , path , cache , highest_score_only = False ):
178
179
# Always use slashes as separators.
179
180
path = path .replace (os .sep , '/' )
180
- if path not in self . _owners_cache :
181
+ if path not in cache :
181
182
# GetOwnersForFile returns a list of account details sorted by order of
182
183
# best reviewer for path. If owners have the same score, the order is
183
184
# 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' ]
191
195
]
192
196
# If owned_by_all_users is true, add everyone as an owner at the end of
193
197
# the owners list.
194
198
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 ))
197
218
198
219
199
220
def GetCodeOwnersClient (root , upstream , host , project , branch ):
0 commit comments