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