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 | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 8 | import threading |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 9 | |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 10 | import gerrit_util |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 11 | import git_common |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 12 | import owners as owners_db |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 13 | import scm |
| 14 | |
| 15 | |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 16 | def _owner_combinations(owners, num_owners): |
| 17 | """Iterate owners combinations by decrasing score. |
| 18 | |
| 19 | The score of an owner is its position on the owners list. |
| 20 | The score of a set of owners is the maximum score of all owners on the set. |
| 21 | |
| 22 | Returns all combinations of up to `num_owners` sorted by decreasing score: |
| 23 | _owner_combinations(['0', '1', '2', '3'], 2) == [ |
| 24 | # score 1 |
| 25 | ('1', '0'), |
| 26 | # score 2 |
| 27 | ('2', '0'), |
| 28 | ('2', '1'), |
| 29 | # score 3 |
| 30 | ('3', '0'), |
| 31 | ('3', '1'), |
| 32 | ('3', '2'), |
| 33 | ] |
| 34 | """ |
| 35 | return reversed(list(itertools.combinations(reversed(owners), num_owners))) |
| 36 | |
| 37 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 38 | class OwnersClient(object): |
| 39 | """Interact with OWNERS files in a repository. |
| 40 | |
| 41 | This class allows you to interact with OWNERS files in a repository both the |
| 42 | Gerrit Code-Owners plugin REST API, and the owners database implemented by |
| 43 | Depot Tools in owners.py: |
| 44 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 45 | - List all the owners for a group of files. |
| 46 | - Check if files have been approved. |
| 47 | - Suggest owners for a group of files. |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 48 | |
| 49 | All code should use this class to interact with OWNERS files instead of the |
| 50 | owners database in owners.py |
| 51 | """ |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 52 | # '*' means that everyone can approve. |
| 53 | EVERYONE = '*' |
| 54 | |
| 55 | # Possible status of a file. |
| 56 | # - INSUFFICIENT_REVIEWERS: The path needs owners approval, but none of its |
| 57 | # owners is currently a reviewer of the change. |
| 58 | # - PENDING: An owner of this path has been added as reviewer, but approval |
| 59 | # has not been given yet. |
| 60 | # - APPROVED: The path has been approved by an owner. |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 61 | APPROVED = 'APPROVED' |
| 62 | PENDING = 'PENDING' |
| 63 | INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS' |
| 64 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 65 | def ListOwners(self, path): |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 66 | """List all owners for a file. |
| 67 | |
| 68 | The returned list is sorted so that better owners appear first. |
| 69 | """ |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 70 | raise Exception('Not implemented') |
| 71 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 72 | def BatchListOwners(self, paths): |
| 73 | """List all owners for a group of files. |
| 74 | |
| 75 | Returns a dictionary {path: [owners]}. |
| 76 | """ |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 77 | with git_common.ScopedPool(kind='threads') as pool: |
| 78 | return dict(pool.imap_unordered( |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 79 | lambda p: (p, self.ListOwners(p)), paths)) |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 80 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 81 | def GetFilesApprovalStatus(self, paths, approvers, reviewers): |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 82 | """Check the approval status for the given paths. |
| 83 | |
| 84 | Utility method to check for approval status when a change has not yet been |
| 85 | created, given reviewers and approvers. |
| 86 | |
| 87 | See GetChangeApprovalStatus for description of the returned value. |
| 88 | """ |
| 89 | approvers = set(approvers) |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 90 | if approvers: |
| 91 | approvers.add(self.EVERYONE) |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 92 | reviewers = set(reviewers) |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 93 | if reviewers: |
| 94 | reviewers.add(self.EVERYONE) |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 95 | status = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 96 | owners_by_path = self.BatchListOwners(paths) |
| 97 | for path, owners in owners_by_path.items(): |
| 98 | owners = set(owners) |
| 99 | if owners.intersection(approvers): |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 100 | status[path] = self.APPROVED |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 101 | elif owners.intersection(reviewers): |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 102 | status[path] = self.PENDING |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 103 | else: |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 104 | status[path] = self.INSUFFICIENT_REVIEWERS |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 105 | return status |
| 106 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 107 | def ScoreOwners(self, paths, exclude=None): |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 108 | """Get sorted list of owners for the given paths.""" |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 109 | exclude = exclude or [] |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 110 | positions_by_owner = {} |
| 111 | owners_by_path = self.BatchListOwners(paths) |
| 112 | for owners in owners_by_path.values(): |
| 113 | for i, owner in enumerate(owners): |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 114 | if owner in exclude: |
| 115 | continue |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 116 | # Gerrit API lists owners of a path sorted by an internal score, so |
| 117 | # owners that appear first should be prefered. |
| 118 | # We define the score of an owner based on the pair |
| 119 | # (# of files owned, minimum position on all owned files) |
| 120 | positions_by_owner.setdefault(owner, []).append(i) |
| 121 | |
| 122 | # Sort owners by their score. Rank owners higher for more files owned and |
| 123 | # lower for a larger minimum position across all owned files. Randomize |
| 124 | # order for owners with same score to avoid bias. |
| 125 | return sorted( |
| 126 | positions_by_owner, |
| 127 | key=lambda o: (-len(positions_by_owner[o]), |
| 128 | min(positions_by_owner[o]) + random.random())) |
| 129 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 130 | def SuggestOwners(self, paths, exclude=None): |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 131 | """Suggest a set of owners for the given paths.""" |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 132 | exclude = exclude or [] |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 133 | paths_by_owner = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 134 | owners_by_path = self.BatchListOwners(paths) |
| 135 | for path, owners in owners_by_path.items(): |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 136 | for owner in owners: |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 137 | if owner not in exclude: |
| 138 | paths_by_owner.setdefault(owner, set()).add(path) |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 139 | |
| 140 | # Select the minimum number of owners that can approve all paths. |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 141 | # We start at 2 to avoid sending all changes that require multiple |
| 142 | # reviewers to top-level owners. |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 143 | owners = self.ScoreOwners(paths, exclude=exclude) |
Edward Lesmes | ca45aff | 2020-12-03 23:11:01 +0000 | [diff] [blame] | 144 | if len(owners) < 2: |
| 145 | return owners |
| 146 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 147 | # Note that we have to iterate up to len(owners) + 1. |
| 148 | # e.g. if there are only 2 owners, we should consider num_owners = 2. |
| 149 | for num_owners in range(2, len(owners) + 1): |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 150 | # Iterate all combinations of `num_owners` by decreasing score, and |
| 151 | # select the first one that covers all paths. |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 152 | for selected in _owner_combinations(owners, num_owners): |
| 153 | covered = set.union(*(paths_by_owner[o] for o in selected)) |
| 154 | if len(covered) == len(paths): |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 155 | return list(selected) |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 156 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 157 | return [] |
| 158 | |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 159 | |
| 160 | class DepotToolsClient(OwnersClient): |
| 161 | """Implement OwnersClient using owners.py Database.""" |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 162 | def __init__(self, root, branch, fopen=open, os_path=os.path): |
| 163 | super(DepotToolsClient, self).__init__() |
| 164 | |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 165 | self._root = root |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 166 | self._branch = branch |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 167 | self._fopen = fopen |
| 168 | self._os_path = os_path |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 169 | self._db = None |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 170 | self._db_lock = threading.Lock() |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 171 | |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 172 | def _ensure_db(self): |
| 173 | if self._db is not None: |
| 174 | return |
| 175 | self._db = owners_db.Database(self._root, self._fopen, self._os_path) |
| 176 | self._db.override_files = self._GetOriginalOwnersFiles() |
| 177 | |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 178 | def _GetOriginalOwnersFiles(self): |
| 179 | return { |
Edward Lesmes | 8a791e7 | 2020-12-02 18:33:18 +0000 | [diff] [blame] | 180 | f: scm.GIT.GetOldContents(self._root, f, self._branch).splitlines() |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 181 | for _, f in scm.GIT.CaptureStatus(self._root, self._branch) |
| 182 | if os.path.basename(f) == 'OWNERS' |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 183 | } |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 184 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 185 | def ListOwners(self, path): |
| 186 | # all_possible_owners is not thread safe. |
| 187 | with self._db_lock: |
Edward Lesmes | 82b992a | 2021-01-11 23:24:55 +0000 | [diff] [blame] | 188 | self._ensure_db() |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 189 | # all_possible_owners returns a dict {owner: [(path, distance)]}. We want |
| 190 | # to return a list of owners sorted by increasing distance. |
| 191 | distance_by_owner = self._db.all_possible_owners([path], None) |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 192 | # We add a small random number to the distance, so that owners at the |
| 193 | # same distance are returned in random order to avoid overloading those |
| 194 | # who would appear first. |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 195 | return sorted( |
| 196 | distance_by_owner, |
| 197 | key=lambda o: distance_by_owner[o][0][1] + random.random()) |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | class GerritClient(OwnersClient): |
| 201 | """Implement OwnersClient using OWNERS REST API.""" |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 202 | def __init__(self, host, project, branch): |
| 203 | super(GerritClient, self).__init__() |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 204 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 205 | self._host = host |
| 206 | self._project = project |
| 207 | self._branch = branch |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 208 | self._owners_cache = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 209 | |
| 210 | def ListOwners(self, path): |
Edward Lesmes | 5e37f6d | 2021-02-17 23:32:16 +0000 | [diff] [blame] | 211 | # Always use slashes as separators. |
| 212 | path = path.replace(os.sep, '/') |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 213 | if path not in self._owners_cache: |
| 214 | # GetOwnersForFile returns a list of account details sorted by order of |
| 215 | # best reviewer for path. If owners have the same score, the order is |
| 216 | # random. |
| 217 | data = gerrit_util.GetOwnersForFile( |
Gavin Mak | 7d69005 | 2021-02-25 19:14:22 +0000 | [diff] [blame] | 218 | self._host, self._project, self._branch, path, |
| 219 | resolve_all_users=False) |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 220 | self._owners_cache[path] = [ |
| 221 | d['account']['email'] |
| 222 | for d in data['code_owners'] |
Gavin Mak | 7d69005 | 2021-02-25 19:14:22 +0000 | [diff] [blame] | 223 | if 'account' in d and 'email' in d['account'] |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 224 | ] |
Gavin Mak | 7d69005 | 2021-02-25 19:14:22 +0000 | [diff] [blame] | 225 | # If owned_by_all_users is true, add everyone as an owner at the end of |
| 226 | # the owners list. |
| 227 | if data.get('owned_by_all_users', False): |
| 228 | self._owners_cache[path].append(self.EVERYONE) |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 229 | return self._owners_cache[path] |
Edward Lesmes | 110823b | 2021-02-05 21:42:27 +0000 | [diff] [blame] | 230 | |
| 231 | |
| 232 | def GetCodeOwnersClient(root, host, project, branch): |
| 233 | """Get a new OwnersClient. |
| 234 | |
| 235 | Defaults to GerritClient, and falls back to DepotToolsClient if code-owners |
| 236 | plugin is not available.""" |
Edward Lesmes | b87cca8 | 2021-03-01 20:45:47 +0000 | [diff] [blame] | 237 | # TODO(crbug.com/1183447): Use code-owners plugin if available on host once |
| 238 | # code-owners plugin issues have been fixed. |
Edward Lesmes | 110823b | 2021-02-05 21:42:27 +0000 | [diff] [blame] | 239 | return DepotToolsClient(root, branch) |