blob: 5dabe5dc028d6792b4e30efe455284e536067d03 [file] [log] [blame]
Edward Lesmes91bb7502020-11-06 00:50:24 +00001# Copyright (c) 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Edward Lesmes295dd182020-11-24 23:07:26 +00005import itertools
Edward Lesmesd4e6fb62020-11-17 00:17:58 +00006import os
Edward Lesmes64e80762020-11-24 19:46:45 +00007import random
Edward Lesmesd4e6fb62020-11-17 00:17:58 +00008
Edward Lesmes829ce022020-11-18 18:30:31 +00009import gerrit_util
Gavin Mak99399ca2020-12-11 20:56:03 +000010import git_common
Edward Lesmes64e80762020-11-24 19:46:45 +000011import owners as owners_db
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000012import scm
13
14
15APPROVED = 'APPROVED'
16PENDING = 'PENDING'
17INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS'
Edward Lesmesb4f42262020-11-10 23:41:35 +000018
Edward Lesmes91bb7502020-11-06 00:50:24 +000019
Edward Lesmes295dd182020-11-24 23:07:26 +000020def _owner_combinations(owners, num_owners):
21 """Iterate owners combinations by decrasing score.
22
23 The score of an owner is its position on the owners list.
24 The score of a set of owners is the maximum score of all owners on the set.
25
26 Returns all combinations of up to `num_owners` sorted by decreasing score:
27 _owner_combinations(['0', '1', '2', '3'], 2) == [
28 # score 1
29 ('1', '0'),
30 # score 2
31 ('2', '0'),
32 ('2', '1'),
33 # score 3
34 ('3', '0'),
35 ('3', '1'),
36 ('3', '2'),
37 ]
38 """
39 return reversed(list(itertools.combinations(reversed(owners), num_owners)))
40
41
Edward Lesmesb4721682020-11-19 22:59:57 +000042class InvalidOwnersConfig(Exception):
43 pass
44
45
Edward Lesmes91bb7502020-11-06 00:50:24 +000046class OwnersClient(object):
47 """Interact with OWNERS files in a repository.
48
49 This class allows you to interact with OWNERS files in a repository both the
50 Gerrit Code-Owners plugin REST API, and the owners database implemented by
51 Depot Tools in owners.py:
52
53 - List all the owners for a change.
54 - Check if a change has been approved.
55 - Check if the OWNERS configuration in a change is valid.
56
57 All code should use this class to interact with OWNERS files instead of the
58 owners database in owners.py
59 """
60 def __init__(self, host):
61 self._host = host
62
63 def ListOwnersForFile(self, project, branch, path):
Edward Lesmes64e80762020-11-24 19:46:45 +000064 """List all owners for a file.
65
66 The returned list is sorted so that better owners appear first.
67 """
Edward Lesmes91bb7502020-11-06 00:50:24 +000068 raise Exception('Not implemented')
69
Gavin Mak99399ca2020-12-11 20:56:03 +000070 def BatchListOwners(self, project, branch, paths):
71 """Returns a dictionary {path: [owners]}."""
72 with git_common.ScopedPool(kind='threads') as pool:
73 return dict(pool.imap_unordered(
74 lambda p: (p, self.ListOwnersForFile(project, branch, p)), paths))
75
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000076 def GetChangeApprovalStatus(self, change_id):
Edward Lesmesb4721682020-11-19 22:59:57 +000077 """Check the approval status for the latest revision_id in a change.
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000078
79 Returns a map of path to approval status, where the status can be one of:
80 - APPROVED: An owner of the file has reviewed the change.
81 - PENDING: An owner of the file has been added as a reviewer, but no owner
82 has approved.
83 - INSUFFICIENT_REVIEWERS: No owner of the file has been added as a reviewer.
84 """
Edward Lesmes91bb7502020-11-06 00:50:24 +000085 raise Exception('Not implemented')
86
Edward Lesmesb4721682020-11-19 22:59:57 +000087 def ValidateOwnersConfig(self, change_id):
Edward Lesmes91bb7502020-11-06 00:50:24 +000088 """Check if the owners configuration in a change is valid."""
89 raise Exception('Not implemented')
Edward Lesmesb4f42262020-11-10 23:41:35 +000090
Edward Lesmese7d18622020-11-19 23:46:17 +000091 def GetFilesApprovalStatus(
92 self, project, branch, paths, approvers, reviewers):
93 """Check the approval status for the given paths.
94
95 Utility method to check for approval status when a change has not yet been
96 created, given reviewers and approvers.
97
98 See GetChangeApprovalStatus for description of the returned value.
99 """
100 approvers = set(approvers)
101 reviewers = set(reviewers)
102 status = {}
103 for path in paths:
104 path_owners = set(self.ListOwnersForFile(project, branch, path))
105 if path_owners.intersection(approvers):
106 status[path] = APPROVED
107 elif path_owners.intersection(reviewers):
108 status[path] = PENDING
109 else:
110 status[path] = INSUFFICIENT_REVIEWERS
111 return status
112
Edward Lesmes295dd182020-11-24 23:07:26 +0000113 def SuggestOwners(self, project, branch, paths):
114 """Suggest a set of owners for the given paths."""
115 paths_by_owner = {}
116 score_by_owner = {}
117 for path in paths:
118 owners = self.ListOwnersForFile(project, branch, path)
119 for i, owner in enumerate(owners):
120 paths_by_owner.setdefault(owner, set()).add(path)
121 # Gerrit API lists owners of a path sorted by an internal score, so
122 # owners that appear first should be prefered.
123 # We define the score of an owner to be their minimum position in all
124 # paths.
125 score_by_owner[owner] = min(i, score_by_owner.get(owner, i))
126
127 # Sort owners by their score.
128 owners = sorted(score_by_owner, key=lambda o: score_by_owner[o])
129
130 # Select the minimum number of owners that can approve all paths.
131 # We start at 2 to avoid sending all changes that require multiple reviewers
132 # to top-level owners.
Edward Lesmesca45aff2020-12-03 23:11:01 +0000133 if len(owners) < 2:
134 return owners
135
136 for num_owners in range(2, len(owners)):
Edward Lesmes295dd182020-11-24 23:07:26 +0000137 # Iterate all combinations of `num_owners` by decreasing score, and select
138 # the first one that covers all paths.
139 for selected in _owner_combinations(owners, num_owners):
140 covered = set.union(*(paths_by_owner[o] for o in selected))
141 if len(covered) == len(paths):
142 return selected
Edward Lesmes295dd182020-11-24 23:07:26 +0000143
Edward Lesmesb4f42262020-11-10 23:41:35 +0000144
145class DepotToolsClient(OwnersClient):
146 """Implement OwnersClient using owners.py Database."""
Edward Lesmeseeca9c62020-11-20 00:00:17 +0000147 def __init__(self, host, root, branch, fopen=open, os_path=os.path):
Edward Lesmesb4f42262020-11-10 23:41:35 +0000148 super(DepotToolsClient, self).__init__(host)
149 self._root = root
Edward Lesmesb4721682020-11-19 22:59:57 +0000150 self._fopen = fopen
151 self._os_path = os_path
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000152 self._branch = branch
Edward Lesmes64e80762020-11-24 19:46:45 +0000153 self._db = owners_db.Database(root, fopen, os_path)
Edward Lesmes829ce022020-11-18 18:30:31 +0000154 self._db.override_files = self._GetOriginalOwnersFiles()
155
156 def _GetOriginalOwnersFiles(self):
157 return {
Edward Lesmes8a791e72020-12-02 18:33:18 +0000158 f: scm.GIT.GetOldContents(self._root, f, self._branch).splitlines()
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000159 for _, f in scm.GIT.CaptureStatus(self._root, self._branch)
160 if os.path.basename(f) == 'OWNERS'
Edward Lesmes829ce022020-11-18 18:30:31 +0000161 }
Edward Lesmesb4f42262020-11-10 23:41:35 +0000162
163 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmes64e80762020-11-24 19:46:45 +0000164 # all_possible_owners returns a dict {owner: [(path, distance)]}. We want to
165 # return a list of owners sorted by increasing distance.
166 distance_by_owner = self._db.all_possible_owners([path], None)
167 # We add a small random number to the distance, so that owners at the same
168 # distance are returned in random order to avoid overloading those who would
169 # appear first.
170 return sorted(
171 distance_by_owner,
172 key=lambda o: distance_by_owner[o][0][1] + random.random())
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000173
174 def GetChangeApprovalStatus(self, change_id):
175 data = gerrit_util.GetChange(
176 self._host, change_id,
177 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
178 'CURRENT_REVISION'])
179
180 reviewers = [r['email'] for r in data['reviewers']['REVIEWER']]
181
182 # Get reviewers that have approved this change
Edward Lesmes829ce022020-11-18 18:30:31 +0000183 label = data['labels']['Code-Review']
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000184 max_value = max(int(v) for v in label['values'])
185 approvers = [v['email'] for v in label['all'] if v['value'] == max_value]
186
187 files = data['revisions'][data['current_revision']]['files']
Edward Lesmese7d18622020-11-19 23:46:17 +0000188 return self.GetFilesApprovalStatus(None, None, files, approvers, reviewers)
Edward Lesmesb4721682020-11-19 22:59:57 +0000189
190 def ValidateOwnersConfig(self, change_id):
191 data = gerrit_util.GetChange(
192 self._host, change_id,
193 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
194 'CURRENT_REVISION'])
195
196 files = data['revisions'][data['current_revision']]['files']
197
Edward Lesmes64e80762020-11-24 19:46:45 +0000198 db = owners_db.Database(self._root, self._fopen, self._os_path)
Edward Lesmesb4721682020-11-19 22:59:57 +0000199 try:
200 db.load_data_needed_for(
201 [f for f in files if os.path.basename(f) == 'OWNERS'])
202 except Exception as e:
203 raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e)
Gavin Makc94b21d2020-12-10 20:27:32 +0000204
205
206class GerritClient(OwnersClient):
207 """Implement OwnersClient using OWNERS REST API."""
208 def __init__(self, host):
209 super(GerritClient, self).__init__(host)
210
211 def ListOwnersForFile(self, project, branch, path):
212 # GetOwnersForFile returns a list of account details sorted by order of
213 # best reviewer for path. If code owners have the same score, the order is
214 # random.
215 data = gerrit_util.GetOwnersForFile(self._host, project, branch, path)
216 return [d['account']['email'] for d in data]