Commit 110823ba authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

Add a GetCodeOwnersClient method.

GetCodeOwnersClient will return a GerritClient instance
if code-owners is enabled for the host, and will fall
back to DepotToolsClient otherwise.

Change-Id: I303147f36cb28ae3a40649fee020e6ea459b2c73
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2676989Reviewed-by: 's avatarGavin Mak <gavinmak@google.com>
Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent 75a92611
......@@ -771,6 +771,13 @@ def SetCommitMessage(host, change, description, notify='ALL'):
'in change %s' % change)
def IsCodeOwnersEnabled(host):
"""Check if the code-owners plugin is enabled for the host."""
path = 'config/server/capabilities'
capabilities = ReadHttpJsonResponse(CreateHttpConn(host, path))
return 'code-owners-checkCodeOwner' in capabilities
def GetOwnersForFile(host, project, branch, path, limit=100,
o_params=('DETAILS',)):
"""Gets information about owners attached to a file."""
......
......@@ -213,3 +213,13 @@ class GerritClient(OwnersClient):
data = gerrit_util.GetOwnersForFile(
self._host, self._project, self._branch, path)
return [d['account']['email'] for d in data['code_owners']]
def GetCodeOwnersClient(root, host, project, branch):
"""Get a new OwnersClient.
Defaults to GerritClient, and falls back to DepotToolsClient if code-owners
plugin is not available."""
if gerrit_util.IsCodeOwnersEnabled(host):
return GerritClient(host, project, branch)
return DepotToolsClient(root, branch)
......@@ -48,7 +48,6 @@ def _get_owners():
}
class DepotToolsClientTest(unittest.TestCase):
def setUp(self):
self.repo = filesystem_mock.MockFileSystem(files={
......@@ -264,5 +263,23 @@ class OwnersClientTest(unittest.TestCase):
['bar/everyone/foo.txt', 'bar/everyone/bar.txt', 'bar/foo/']))
class GetCodeOwnersClientTest(unittest.TestCase):
def setUp(self):
mock.patch('gerrit_util.IsCodeOwnersEnabled').start()
self.addCleanup(mock.patch.stopall)
def testGetCodeOwnersClient_GerritClient(self):
gerrit_util.IsCodeOwnersEnabled.return_value = True
self.assertIsInstance(
owners_client.GetCodeOwnersClient('root', 'host', 'project', 'branch'),
owners_client.GerritClient)
def testGetCodeOwnersClient_DepotToolsClient(self):
gerrit_util.IsCodeOwnersEnabled.return_value = False
self.assertIsInstance(
owners_client.GetCodeOwnersClient('root', 'host', 'project', 'branch'),
owners_client.DepotToolsClient)
if __name__ == '__main__':
unittest.main()
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