Commit 5cd75478 authored by Edward Lesmes's avatar Edward Lesmes Committed by LUCI CQ

owners_finder: Use code-owners plugin if available.

Change-Id: I94d7b3cdc17651a6fe5dafa261c58f46f37b8439
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2693919
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarJosip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent f362f6f9
...@@ -4831,21 +4831,12 @@ def CMDowners(parser, args): ...@@ -4831,21 +4831,12 @@ def CMDowners(parser, args):
print('\n'.join(owners)) print('\n'.join(owners))
return 0 return 0
root = settings.GetRoot()
owner_files = [f for f in affected_files if 'OWNERS' in os.path.basename(f)]
original_owner_files = {
f: scm.GIT.GetOldContents(root, f, base_branch).splitlines()
for f in owner_files}
return owners_finder.OwnersFinder( return owners_finder.OwnersFinder(
affected_files, affected_files,
root,
author, author,
[] if options.ignore_current else cl.GetReviewers(), [] if options.ignore_current else cl.GetReviewers(),
fopen=open, cl.owners_client,
os_path=os.path,
disable_color=options.no_color, disable_color=options.no_color,
override_files=original_owner_files,
ignore_author=options.ignore_self).run() ignore_author=options.ignore_self).run()
......
...@@ -8,7 +8,6 @@ from __future__ import print_function ...@@ -8,7 +8,6 @@ from __future__ import print_function
import os import os
import copy import copy
import owners_client
import git_common import git_common
...@@ -28,11 +27,9 @@ class OwnersFinder(object): ...@@ -28,11 +27,9 @@ class OwnersFinder(object):
indentation = 0 indentation = 0
def __init__(self, files, local_root, author, reviewers, def __init__(self, files, author, reviewers, owners_client,
fopen, os_path,
email_postfix='@chromium.org', email_postfix='@chromium.org',
disable_color=False, disable_color=False,
override_files=None,
ignore_author=False): ignore_author=False):
self.email_postfix = email_postfix self.email_postfix = email_postfix
...@@ -42,8 +39,6 @@ class OwnersFinder(object): ...@@ -42,8 +39,6 @@ class OwnersFinder(object):
self.COLOR_GREY = '' self.COLOR_GREY = ''
self.COLOR_RESET = '' self.COLOR_RESET = ''
self.os_path = os_path
self.author = author self.author = author
filtered_files = files filtered_files = files
...@@ -53,23 +48,18 @@ class OwnersFinder(object): ...@@ -53,23 +48,18 @@ class OwnersFinder(object):
reviewers.append(author) reviewers.append(author)
# Eliminate files that existing reviewers can review. # Eliminate files that existing reviewers can review.
self.client = owners_client.DepotToolsClient( self.owners_client = owners_client
root=local_root, approval_status = self.owners_client.GetFilesApprovalStatus(
branch=git_common.current_branch(),
fopen=fopen,
os_path=os_path)
approval_status = self.client.GetFilesApprovalStatus(
filtered_files, reviewers, []) filtered_files, reviewers, [])
filtered_files = [ filtered_files = [
f for f in filtered_files f for f in filtered_files
if approval_status[f] != owners_client.OwnersClient.APPROVED] if approval_status[f] != self.owners_client.APPROVED]
# If some files are eliminated. # If some files are eliminated.
if len(filtered_files) != len(files): if len(filtered_files) != len(files):
files = filtered_files files = filtered_files
self.files_to_owners = self.client.BatchListOwners(files) self.files_to_owners = self.owners_client.BatchListOwners(files)
self.owners_to_files = {} self.owners_to_files = {}
self._map_owners_to_files() self._map_owners_to_files()
...@@ -151,7 +141,7 @@ class OwnersFinder(object): ...@@ -151,7 +141,7 @@ class OwnersFinder(object):
# Randomize owners' names so that if many reviewers have identical scores # Randomize owners' names so that if many reviewers have identical scores
# they will be randomly ordered to avoid bias. # they will be randomly ordered to avoid bias.
owners = self.client.ScoreOwners(self.files_to_owners.keys()) owners = list(self.owners_client.ScoreOwners(self.files_to_owners.keys()))
if self.author and self.author in owners: if self.author and self.author in owners:
owners.remove(self.author) owners.remove(self.author)
self.owners_queue = owners self.owners_queue = owners
......
...@@ -33,12 +33,6 @@ tom = 'tom@example.com' ...@@ -33,12 +33,6 @@ tom = 'tom@example.com'
nonowner = 'nonowner@example.com' nonowner = 'nonowner@example.com'
def _get_score_owners_darin_variant():
return [brett, darin, john, peter, ken, ben, tom]
def _get_score_owners_john_variant():
return [brett, john, darin, peter, ken, ben, tom]
def owners_file(*email_addresses, **kwargs): def owners_file(*email_addresses, **kwargs):
...@@ -50,45 +44,36 @@ def owners_file(*email_addresses, **kwargs): ...@@ -50,45 +44,36 @@ def owners_file(*email_addresses, **kwargs):
return s + '\n'.join(email_addresses) + '\n' return s + '\n'.join(email_addresses) + '\n'
def test_repo(): class TestClient(owners_client.OwnersClient):
return filesystem_mock.MockFileSystem(files={ def __init__(self):
'/DEPS': '', super(TestClient, self).__init__()
'/OWNERS': owners_file(ken, peter, tom, self.owners_by_path = {
comment='OWNERS_STATUS = build/OWNERS.status'), 'DEPS': [ken, peter, tom],
'/build/OWNERS.status': '%s: bar' % jochen, 'base/vlog.h': [ken, peter, tom],
'/base/vlog.h': '', 'chrome/browser/defaults.h': [brett, ben, ken, peter, tom],
'/chrome/OWNERS': owners_file(ben, brett), 'chrome/gpu/gpu_channel.h': [ken, ben, brett, ken, peter, tom],
'/chrome/browser/OWNERS': owners_file(brett), 'chrome/renderer/gpu/gpu_channel_host.h': [peter, ben, brett, ken, tom],
'/chrome/browser/defaults.h': '', 'chrome/renderer/safe_browsing/scorer.h': [peter, ben, brett, ken, tom],
'/chrome/gpu/OWNERS': owners_file(ken), 'content/content.gyp': [john, darin],
'/chrome/gpu/gpu_channel.h': '', 'content/bar/foo.cc': [john, darin],
'/chrome/renderer/OWNERS': owners_file(peter), 'content/baz/froboz.h': [brett, john, darin],
'/chrome/renderer/gpu/gpu_channel_host.h': '', 'content/baz/ugly.cc': [brett, john, darin],
'/chrome/renderer/safe_browsing/scorer.h': '', 'content/baz/ugly.h': [brett, john, darin],
'/content/OWNERS': owners_file(john, darin, comment='foo', noparent=True), 'content/common/common.cc': [jochen, john, darin],
'/content/content.gyp': '', 'content/foo/foo.cc': [jochen, john, darin],
'/content/bar/foo.cc': '', 'content/views/pie.h': [ben, john, self.EVERYONE],
'/content/baz/OWNERS': owners_file(brett), }
'/content/baz/froboz.h': '',
'/content/baz/ugly.cc': '', def ListOwners(self, path):
'/content/baz/ugly.h': '', path = path.replace(os.sep, '/')
'/content/common/OWNERS': owners_file(jochen), return self.owners_by_path[path]
'/content/common/common.cc': '',
'/content/foo/OWNERS': owners_file(jochen, comment='foo'),
'/content/foo/foo.cc': '',
'/content/views/OWNERS': owners_file(ben, john,
owners_client.OwnersClient.EVERYONE,
noparent=True),
'/content/views/pie.h': '',
})
class OutputInterceptedOwnersFinder(owners_finder.OwnersFinder): class OutputInterceptedOwnersFinder(owners_finder.OwnersFinder):
def __init__(self, files, local_root, author, reviewers, def __init__(
fopen, os_path, disable_color=False): self, files, author, reviewers, client, disable_color=False):
super(OutputInterceptedOwnersFinder, self).__init__( super(OutputInterceptedOwnersFinder, self).__init__(
files, local_root, author, reviewers, fopen, os_path, files, author, reviewers, client, disable_color=disable_color)
disable_color=disable_color)
self.output = [] self.output = []
self.indentation_stack = [] self.indentation_stack = []
...@@ -123,24 +108,10 @@ class _BaseTestCase(unittest.TestCase): ...@@ -123,24 +108,10 @@ class _BaseTestCase(unittest.TestCase):
'content/views/pie.h' 'content/views/pie.h'
] ]
def setUp(self):
self.repo = test_repo()
self.root = '/'
self.fopen = self.repo.open_for_reading
mock.patch('owners_client.DepotToolsClient._GetOriginalOwnersFiles',
return_value={}).start()
self.addCleanup(mock.patch.stopall)
def ownersFinder(self, files, author=nonowner, reviewers=None): def ownersFinder(self, files, author=nonowner, reviewers=None):
reviewers = reviewers or [] reviewers = reviewers or []
finder = OutputInterceptedOwnersFinder(files, return OutputInterceptedOwnersFinder(
self.root, files, author, reviewers, TestClient(), disable_color=True)
author,
reviewers,
fopen=self.fopen,
os_path=self.repo,
disable_color=True)
return finder
def defaultFinder(self): def defaultFinder(self):
return self.ownersFinder(self.default_files) return self.ownersFinder(self.default_files)
...@@ -177,14 +148,9 @@ class OwnersFinderTests(_BaseTestCase): ...@@ -177,14 +148,9 @@ class OwnersFinderTests(_BaseTestCase):
finder = self.ownersFinder(files, reviewers=[brett]) finder = self.ownersFinder(files, reviewers=[brett])
self.assertEqual(finder.unreviewed_files, {'content/bar/foo.cc'}) self.assertEqual(finder.unreviewed_files, {'content/bar/foo.cc'})
def test_reset(self): @mock.patch('owners_client.OwnersClient.ScoreOwners')
mock.patch('owners_client.DepotToolsClient.ScoreOwners', def test_reset(self, mockScoreOwners):
side_effect=[ mockScoreOwners.return_value = [brett, darin, john, peter, ken, ben, tom]
_get_score_owners_darin_variant(),
_get_score_owners_darin_variant(),
_get_score_owners_darin_variant()
]).start()
finder = self.defaultFinder() finder = self.defaultFinder()
for _ in range(2): for _ in range(2):
expected = [brett, darin, john, peter, ken, ben, tom] expected = [brett, darin, john, peter, ken, ben, tom]
...@@ -209,14 +175,9 @@ class OwnersFinderTests(_BaseTestCase): ...@@ -209,14 +175,9 @@ class OwnersFinderTests(_BaseTestCase):
finder.reset() finder.reset()
finder.resetText() finder.resetText()
def test_select(self): @mock.patch('owners_client.OwnersClient.ScoreOwners')
mock.patch('owners_client.DepotToolsClient.ScoreOwners', def test_select(self, mockScoreOwners):
side_effect=[ mockScoreOwners.return_value = [brett, darin, john, peter, ken, ben, tom]
_get_score_owners_darin_variant(),
_get_score_owners_john_variant(),
_get_score_owners_darin_variant()
]).start()
finder = self.defaultFinder() finder = self.defaultFinder()
finder.select_owner(john) finder.select_owner(john)
self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom]) self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom])
...@@ -257,10 +218,9 @@ class OwnersFinderTests(_BaseTestCase): ...@@ -257,10 +218,9 @@ class OwnersFinderTests(_BaseTestCase):
self.assertEqual(finder.output, self.assertEqual(finder.output,
['Selected: ' + brett, 'Deselected: ' + ben]) ['Selected: ' + brett, 'Deselected: ' + ben])
def test_deselect(self): @mock.patch('owners_client.OwnersClient.ScoreOwners')
mock.patch('owners_client.DepotToolsClient.ScoreOwners', def test_deselect(self, mockScoreOwners):
return_value=_get_score_owners_darin_variant()).start() mockScoreOwners.return_value = [brett, darin, john, peter, ken, ben, tom]
finder = self.defaultFinder() finder = self.defaultFinder()
finder.deselect_owner(john) finder.deselect_owner(john)
self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom]) self.assertEqual(finder.owners_queue, [brett, peter, ken, ben, tom])
......
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