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 | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 5 | import os |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 6 | import random |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 7 | |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 8 | import gerrit_util |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 9 | import git_common |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 10 | |
| 11 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 12 | class OwnersClient(object): |
| 13 | """Interact with OWNERS files in a repository. |
| 14 | |
| 15 | This class allows you to interact with OWNERS files in a repository both the |
| 16 | Gerrit Code-Owners plugin REST API, and the owners database implemented by |
| 17 | Depot Tools in owners.py: |
| 18 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 19 | - List all the owners for a group of files. |
| 20 | - Check if files have been approved. |
| 21 | - Suggest owners for a group of files. |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 22 | |
| 23 | All code should use this class to interact with OWNERS files instead of the |
| 24 | owners database in owners.py |
| 25 | """ |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 26 | # '*' means that everyone can approve. |
| 27 | EVERYONE = '*' |
| 28 | |
| 29 | # Possible status of a file. |
| 30 | # - INSUFFICIENT_REVIEWERS: The path needs owners approval, but none of its |
| 31 | # owners is currently a reviewer of the change. |
| 32 | # - PENDING: An owner of this path has been added as reviewer, but approval |
| 33 | # has not been given yet. |
| 34 | # - APPROVED: The path has been approved by an owner. |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 35 | APPROVED = 'APPROVED' |
| 36 | PENDING = 'PENDING' |
| 37 | INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS' |
| 38 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 39 | def ListOwners(self, path): |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame] | 40 | """List all owners for a file. |
| 41 | |
| 42 | The returned list is sorted so that better owners appear first. |
| 43 | """ |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 44 | raise Exception('Not implemented') |
| 45 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 46 | def BatchListOwners(self, paths): |
| 47 | """List all owners for a group of files. |
| 48 | |
| 49 | Returns a dictionary {path: [owners]}. |
| 50 | """ |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 51 | with git_common.ScopedPool(kind='threads') as pool: |
| 52 | return dict(pool.imap_unordered( |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 53 | lambda p: (p, self.ListOwners(p)), paths)) |
Gavin Mak | 99399ca | 2020-12-11 20:56:03 +0000 | [diff] [blame] | 54 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 55 | def GetFilesApprovalStatus(self, paths, approvers, reviewers): |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 56 | """Check the approval status for the given paths. |
| 57 | |
| 58 | Utility method to check for approval status when a change has not yet been |
| 59 | created, given reviewers and approvers. |
| 60 | |
| 61 | See GetChangeApprovalStatus for description of the returned value. |
| 62 | """ |
| 63 | approvers = set(approvers) |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 64 | if approvers: |
| 65 | approvers.add(self.EVERYONE) |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 66 | reviewers = set(reviewers) |
Edward Lesmes | 071c3b1 | 2021-01-15 19:02:59 +0000 | [diff] [blame] | 67 | if reviewers: |
| 68 | reviewers.add(self.EVERYONE) |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 69 | status = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 70 | owners_by_path = self.BatchListOwners(paths) |
| 71 | for path, owners in owners_by_path.items(): |
| 72 | owners = set(owners) |
| 73 | if owners.intersection(approvers): |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 74 | status[path] = self.APPROVED |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 75 | elif owners.intersection(reviewers): |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 76 | status[path] = self.PENDING |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 77 | else: |
Edward Lesmes | c40b240 | 2021-01-12 20:03:11 +0000 | [diff] [blame] | 78 | status[path] = self.INSUFFICIENT_REVIEWERS |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 79 | return status |
| 80 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 81 | def ScoreOwners(self, paths, exclude=None): |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 82 | """Get sorted list of owners for the given paths.""" |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 83 | if not paths: |
| 84 | return [] |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 85 | exclude = exclude or [] |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 86 | owners = [] |
| 87 | queues = self.BatchListOwners(paths).values() |
| 88 | for i in range(max(len(q) for q in queues)): |
| 89 | for q in queues: |
| 90 | if i < len(q) and q[i] not in owners and q[i] not in exclude: |
| 91 | owners.append(q[i]) |
| 92 | return owners |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 93 | |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 94 | def SuggestOwners(self, paths, exclude=None): |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 95 | """Suggest a set of owners for the given paths.""" |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 96 | exclude = exclude or [] |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 97 | |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 98 | paths_by_owner = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 99 | owners_by_path = self.BatchListOwners(paths) |
| 100 | for path, owners in owners_by_path.items(): |
Gavin Mak | d36dbbd | 2021-01-25 19:34:58 +0000 | [diff] [blame] | 101 | for owner in owners: |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 102 | paths_by_owner.setdefault(owner, set()).add(path) |
Edward Lesmes | 295dd18 | 2020-11-24 23:07:26 +0000 | [diff] [blame] | 103 | |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 104 | selected = [] |
| 105 | missing = set(paths) |
| 106 | for owner in self.ScoreOwners(paths, exclude=exclude): |
| 107 | missing_len = len(missing) |
| 108 | missing.difference_update(paths_by_owner[owner]) |
| 109 | if missing_len > len(missing): |
| 110 | selected.append(owner) |
| 111 | if not missing: |
| 112 | break |
Edward Lesmes | ca45aff | 2020-12-03 23:11:01 +0000 | [diff] [blame] | 113 | |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 114 | return selected |
Edward Lesmes | 0e2aee7 | 2021-02-03 20:12:46 +0000 | [diff] [blame] | 115 | |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 116 | class GerritClient(OwnersClient): |
| 117 | """Implement OwnersClient using OWNERS REST API.""" |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 118 | def __init__(self, host, project, branch): |
| 119 | super(GerritClient, self).__init__() |
Gavin Mak | c94b21d | 2020-12-10 20:27:32 +0000 | [diff] [blame] | 120 | |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 121 | self._host = host |
| 122 | self._project = project |
| 123 | self._branch = branch |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 124 | self._owners_cache = {} |
Gavin Mak | e0fee9f | 2022-08-10 23:41:55 +0000 | [diff] [blame] | 125 | self._best_owners_cache = {} |
Edward Lesmes | 0e4e5ae | 2021-01-08 18:28:46 +0000 | [diff] [blame] | 126 | |
Edward Lesmes | 23c3bdc | 2021-03-11 20:37:32 +0000 | [diff] [blame] | 127 | # Seed used by Gerrit to shuffle code owners that have the same score. Can |
| 128 | # be used to make the sort order stable across several requests, e.g. to get |
| 129 | # the same set of random code owners for different file paths that have the |
| 130 | # same code owners. |
| 131 | self._seed = random.getrandbits(30) |
| 132 | |
Gavin Mak | e0fee9f | 2022-08-10 23:41:55 +0000 | [diff] [blame] | 133 | def _FetchOwners(self, path, cache, highest_score_only=False): |
Edward Lesmes | 5e37f6d | 2021-02-17 23:32:16 +0000 | [diff] [blame] | 134 | # Always use slashes as separators. |
| 135 | path = path.replace(os.sep, '/') |
Gavin Mak | e0fee9f | 2022-08-10 23:41:55 +0000 | [diff] [blame] | 136 | if path not in cache: |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 137 | # GetOwnersForFile returns a list of account details sorted by order of |
| 138 | # 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] | 139 | # random, seeded by `self._seed`. |
Gavin Mak | e0fee9f | 2022-08-10 23:41:55 +0000 | [diff] [blame] | 140 | data = gerrit_util.GetOwnersForFile(self._host, |
| 141 | self._project, |
| 142 | self._branch, |
| 143 | path, |
| 144 | resolve_all_users=False, |
| 145 | highest_score_only=highest_score_only, |
| 146 | seed=self._seed) |
| 147 | cache[path] = [ |
| 148 | d['account']['email'] for d in data['code_owners'] |
| 149 | if 'account' in d and 'email' in d['account'] |
Edward Lesmes | 0d1bdb2 | 2021-02-16 21:27:04 +0000 | [diff] [blame] | 150 | ] |
Gavin Mak | 7d69005 | 2021-02-25 19:14:22 +0000 | [diff] [blame] | 151 | # If owned_by_all_users is true, add everyone as an owner at the end of |
| 152 | # the owners list. |
| 153 | if data.get('owned_by_all_users', False): |
Gavin Mak | e0fee9f | 2022-08-10 23:41:55 +0000 | [diff] [blame] | 154 | cache[path].append(self.EVERYONE) |
| 155 | return cache[path] |
| 156 | |
| 157 | def ListOwners(self, path): |
| 158 | return self._FetchOwners(path, self._owners_cache) |
| 159 | |
| 160 | def ListBestOwners(self, path): |
| 161 | return self._FetchOwners(path, |
| 162 | self._best_owners_cache, |
| 163 | highest_score_only=True) |
| 164 | |
| 165 | def BatchListBestOwners(self, paths): |
| 166 | """List only the higest-scoring owners for a group of files. |
| 167 | |
| 168 | Returns a dictionary {path: [owners]}. |
| 169 | """ |
| 170 | with git_common.ScopedPool(kind='threads') as pool: |
| 171 | return dict( |
| 172 | pool.imap_unordered(lambda p: (p, self.ListBestOwners(p)), paths)) |
Edward Lesmes | 110823b | 2021-02-05 21:42:27 +0000 | [diff] [blame] | 173 | |
| 174 | |
Gavin Mak | 0f1addc | 2022-09-08 15:26:06 +0000 | [diff] [blame] | 175 | def GetCodeOwnersClient(host, project, branch): |
Edward Lesmes | 110823b | 2021-02-05 21:42:27 +0000 | [diff] [blame] | 176 | """Get a new OwnersClient. |
| 177 | |
Gavin Mak | 0f1addc | 2022-09-08 15:26:06 +0000 | [diff] [blame] | 178 | Uses GerritClient and raises an exception if code-owners plugin is not |
| 179 | available.""" |
Edward Lesmes | 8170c29 | 2021-03-19 20:04:43 +0000 | [diff] [blame] | 180 | if gerrit_util.IsCodeOwnersEnabledOnHost(host): |
Edward Lesmes | 88f712e | 2021-03-15 17:55:13 +0000 | [diff] [blame] | 181 | return GerritClient(host, project, branch) |
Gavin Mak | 0f1addc | 2022-09-08 15:26:06 +0000 | [diff] [blame] | 182 | raise Exception( |
| 183 | 'code-owners plugin is not enabled. Ask your host admin to enable it ' |
| 184 | 'on %s. Read more about code-owners at ' |
Gavin Mak | df2f111 | 2023-06-02 21:53:47 +0000 | [diff] [blame] | 185 | 'https://chromium-review.googlesource.com/' |
| 186 | 'plugins/code-owners/Documentation/index.html.' % host) |