[owners] Use new API to list owners for file

This change implements ListOwnersForFile using gerrit's REST API in owners_client.py
Change-Id: Ic454dcd89a8f00f6b9e913e6b8ee9e1169581b38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2583327
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
changes/27/2583327/11
Gavin Mak 4 years ago committed by LUCI CQ
parent d79114a2ed
commit c94b21d60c

@ -770,6 +770,23 @@ def SetCommitMessage(host, change, description, notify='ALL'):
'in change %s' % change)
def GetOwnersForFile(host, project, branch, path, limit=100,
o_params=('DETAILS',)):
"""Gets information about owners attached to a file."""
path = 'projects/%s/branches/%s/code_owners/%s' % (
urllib.parse.quote(project, ''),
urllib.parse.quote(branch, ''),
urllib.parse.quote(path, ''))
q = []
if limit:
q.append('n=%d' % limit)
if o_params:
q.extend(['o=%s' % p for p in o_params])
if q:
path = '%s?%s' % (path, '&'.join(q))
return ReadHttpJsonResponse(CreateHttpConn(host, path))
def GetReviewers(host, change):
"""Gets information about all reviewers attached to a change."""
path = 'changes/%s/reviewers' % change

@ -194,3 +194,16 @@ class DepotToolsClient(OwnersClient):
[f for f in files if os.path.basename(f) == 'OWNERS'])
except Exception as e:
raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e)
class GerritClient(OwnersClient):
"""Implement OwnersClient using OWNERS REST API."""
def __init__(self, host):
super(GerritClient, self).__init__(host)
def ListOwnersForFile(self, project, branch, path):
# GetOwnersForFile returns a list of account details sorted by order of
# best reviewer for path. If code owners have the same score, the order is
# random.
data = gerrit_util.GetOwnersForFile(self._host, project, branch, path)
return [d['account']['email'] for d in data]

@ -63,6 +63,27 @@ def _get_change():
def _get_owners():
return [
{
"account": {
"email": 'approver@example.com'
}
},
{
"account": {
"email": 'reviewer@example.com'
},
},
{
"account": {
"email": 'missing@example.com'
},
}
]
class DepotToolsClientTest(unittest.TestCase):
def setUp(self):
self.repo = filesystem_mock.MockFileSystem(files={
@ -118,6 +139,18 @@ class DepotToolsClientTest(unittest.TestCase):
self.client.ValidateOwnersConfig('changeid')
class GerritClientTest(unittest.TestCase):
def setUp(self):
self.client = owners_client.GerritClient('host')
@mock.patch('gerrit_util.GetOwnersForFile', return_value=_get_owners())
def testListOwners(self, get_owners_mock):
self.assertEquals(
['approver@example.com', 'reviewer@example.com', 'missing@example.com'],
self.client.ListOwnersForFile('project', 'branch',
'bar/everyone/foo.txt'))
class TestClient(owners_client.OwnersClient):
def __init__(self, host, owners_by_path):
super(TestClient, self).__init__(host)

Loading…
Cancel
Save