Commit 0d1bdb23 authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

owners: Cache code-owners result.

Cache result of ListOwners to avoid making duplicate REST API calls.

Change-Id: Ie97fd7b605b73e1baed74302615f9df44fb622d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2693914
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarGavin Mak <gavinmak@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent c1afadf8
...@@ -205,14 +205,20 @@ class GerritClient(OwnersClient): ...@@ -205,14 +205,20 @@ class GerritClient(OwnersClient):
self._host = host self._host = host
self._project = project self._project = project
self._branch = branch self._branch = branch
self._owners_cache = {}
def ListOwners(self, path): def ListOwners(self, path):
# GetOwnersForFile returns a list of account details sorted by order of if path not in self._owners_cache:
# best reviewer for path. If owners have the same score, the order is # GetOwnersForFile returns a list of account details sorted by order of
# random. # best reviewer for path. If owners have the same score, the order is
data = gerrit_util.GetOwnersForFile( # random.
self._host, self._project, self._branch, path) data = gerrit_util.GetOwnersForFile(
return [d['account']['email'] for d in data['code_owners']] self._host, self._project, self._branch, path)
self._owners_cache[path] = [
d['account']['email']
for d in data['code_owners']
]
return self._owners_cache[path]
def GetCodeOwnersClient(root, host, project, branch): def GetCodeOwnersClient(root, host, project, branch):
......
...@@ -87,6 +87,13 @@ class GerritClientTest(unittest.TestCase): ...@@ -87,6 +87,13 @@ class GerritClientTest(unittest.TestCase):
['approver@example.com', 'reviewer@example.com', 'missing@example.com'], ['approver@example.com', 'reviewer@example.com', 'missing@example.com'],
self.client.ListOwners('bar/everyone/foo.txt')) self.client.ListOwners('bar/everyone/foo.txt'))
# Result should be cached.
self.assertEquals(
['approver@example.com', 'reviewer@example.com', 'missing@example.com'],
self.client.ListOwners('bar/everyone/foo.txt'))
gerrit_util.GetOwnersForFile.assert_called_once_with(
'host', 'project', 'branch', 'bar/everyone/foo.txt')
class TestClient(owners_client.OwnersClient): class TestClient(owners_client.OwnersClient):
def __init__(self, owners_by_path): def __init__(self, owners_by_path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment