diff --git a/gerrit_util.py b/gerrit_util.py index 5bb62a9de..0be70e8e7 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -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.""" diff --git a/owners_client.py b/owners_client.py index 77d4d7d6e..d97c5a554 100644 --- a/owners_client.py +++ b/owners_client.py @@ -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) diff --git a/tests/owners_client_test.py b/tests/owners_client_test.py index cf9dfcce9..2a0d1289b 100644 --- a/tests/owners_client_test.py +++ b/tests/owners_client_test.py @@ -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()