Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 1 | # 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 Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 5 | import itertools |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 6 | import os |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 7 | import random |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 8 | |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 9 | import gerrit_util |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 10 | import git_common |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 11 | import owners as owners_db |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 12 | import scm |
| 13 | |
| 14 | |
| 15 | APPROVED = 'APPROVED' |
| 16 | PENDING = 'PENDING' |
| 17 | INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS' |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 18 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 19 | |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 20 | def _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 Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 42 | class InvalidOwnersConfig(Exception): |
| 43 | pass |
| 44 | |
| 45 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 46 | class 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 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 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. |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 56 | |
| 57 | All code should use this class to interact with OWNERS files instead of the |
| 58 | owners database in owners.py |
| 59 | """ |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 60 | def __init__(self, host): |
| 61 | self._host = host |
| 62 | |
| 63 | def ListOwnersForFile(self, project, branch, path): |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 64 | """List all owners for a file. |
| 65 | |
| 66 | The returned list is sorted so that better owners appear first. |
| 67 | """ |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 68 | raise Exception('Not implemented') |
| 69 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 70 | def BatchListOwners(self, project, branch, paths): |
| 71 | """Returns a dictionary {path: [owners]}.""" |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 72 | with git_common.ScopedPool(kind='threads') as pool: |
| 73 | return dict(pool.imap_unordered( |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 74 | lambda p: (p, self.ListOwnersForFile(project, branch, p)), paths)) |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 75 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 76 | def GetChangeApprovalStatus(self, change_id): |
| 77 | """Check the approval status for the latest revision_id in a change. |
| 78 | |
| 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 | """ |
| 85 | raise Exception('Not implemented') |
| 86 | |
| 87 | def ValidateOwnersConfig(self, change_id): |
| 88 | """Check if the owners configuration in a change is valid.""" |
| 89 | raise Exception('Not implemented') |
| 90 | |
| 91 | def GetFilesApprovalStatus( |
| 92 | self, project, branch, paths, approvers, reviewers): |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 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 = {} |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 103 | for path in paths: |
| 104 | path_owners = set(self.ListOwnersForFile(project, branch, path)) |
| 105 | if path_owners.intersection(approvers): |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 106 | status[path] = APPROVED |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 107 | elif path_owners.intersection(reviewers): |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 108 | status[path] = PENDING |
| 109 | else: |
| 110 | status[path] = INSUFFICIENT_REVIEWERS |
| 111 | return status |
| 112 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 113 | def SuggestOwners(self, project, branch, paths): |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 114 | """Suggest a set of owners for the given paths.""" |
| 115 | paths_by_owner = {} |
| 116 | score_by_owner = {} |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 117 | for path in paths: |
| 118 | owners = self.ListOwnersForFile(project, branch, path) |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 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 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 127 | # Sort owners by their score. |
| 128 | owners = sorted(score_by_owner, key=lambda o: score_by_owner[o]) |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 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 Lesmes | ca45aff | 2020-12-03 23:11:01 +0000 | [diff] [blame] | 133 | if len(owners) < 2: |
| 134 | return owners |
| 135 | |
| 136 | for num_owners in range(2, len(owners)): |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 137 | # 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): |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 142 | return selected |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 143 | |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 144 | |
| 145 | class DepotToolsClient(OwnersClient): |
| 146 | """Implement OwnersClient using owners.py Database.""" |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 147 | def __init__(self, host, root, branch, fopen=open, os_path=os.path): |
| 148 | super(DepotToolsClient, self).__init__(host) |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 149 | self._root = root |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 150 | self._fopen = fopen |
| 151 | self._os_path = os_path |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 152 | self._branch = branch |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 153 | self._db = owners_db.Database(root, fopen, os_path) |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 154 | self._db.override_files = self._GetOriginalOwnersFiles() |
| 155 | |
| 156 | def _GetOriginalOwnersFiles(self): |
| 157 | return { |
Edward Lesmes | 8a791e7 | 2020-12-02 18:33:18 +0000 | [diff] [blame] | 158 | f: scm.GIT.GetOldContents(self._root, f, self._branch).splitlines() |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 159 | for _, f in scm.GIT.CaptureStatus(self._root, self._branch) |
| 160 | if os.path.basename(f) == 'OWNERS' |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 161 | } |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 162 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 163 | def ListOwnersForFile(self, _project, _branch, path): |
| 164 | # 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()) |
| 173 | |
| 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 |
| 183 | label = data['labels']['Code-Review'] |
| 184 | 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'] |
| 188 | return self.GetFilesApprovalStatus(None, None, files, approvers, reviewers) |
| 189 | |
| 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 | |
| 198 | db = owners_db.Database(self._root, self._fopen, self._os_path) |
| 199 | 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 Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | class GerritClient(OwnersClient): |
| 207 | """Implement OwnersClient using OWNERS REST API.""" |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 208 | def __init__(self, host): |
| 209 | super(GerritClient, self).__init__(host) |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 210 | |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 211 | def ListOwnersForFile(self, project, branch, path): |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 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. |
Edward Lesmes | b34cd6d | 2021-01-07 01:05:41 +0000 | [diff] [blame^] | 215 | data = gerrit_util.GetOwnersForFile(self._host, project, branch, path) |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 216 | return [d['account']['email'] for d in data] |