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 |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame^] | 9 | import owners as owners_db |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 10 | import scm |
| 11 | |
| 12 | |
| 13 | APPROVED = 'APPROVED' |
| 14 | PENDING = 'PENDING' |
| 15 | INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS' |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 16 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 17 | |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 18 | class InvalidOwnersConfig(Exception): |
| 19 | pass |
| 20 | |
| 21 | |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 22 | class OwnersClient(object): |
| 23 | """Interact with OWNERS files in a repository. |
| 24 | |
| 25 | This class allows you to interact with OWNERS files in a repository both the |
| 26 | Gerrit Code-Owners plugin REST API, and the owners database implemented by |
| 27 | Depot Tools in owners.py: |
| 28 | |
| 29 | - List all the owners for a change. |
| 30 | - Check if a change has been approved. |
| 31 | - Check if the OWNERS configuration in a change is valid. |
| 32 | |
| 33 | All code should use this class to interact with OWNERS files instead of the |
| 34 | owners database in owners.py |
| 35 | """ |
| 36 | def __init__(self, host): |
| 37 | self._host = host |
| 38 | |
| 39 | def ListOwnersForFile(self, project, branch, 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 | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 46 | def GetChangeApprovalStatus(self, change_id): |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 47 | """Check the approval status for the latest revision_id in a change. |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 48 | |
| 49 | Returns a map of path to approval status, where the status can be one of: |
| 50 | - APPROVED: An owner of the file has reviewed the change. |
| 51 | - PENDING: An owner of the file has been added as a reviewer, but no owner |
| 52 | has approved. |
| 53 | - INSUFFICIENT_REVIEWERS: No owner of the file has been added as a reviewer. |
| 54 | """ |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 55 | raise Exception('Not implemented') |
| 56 | |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 57 | def ValidateOwnersConfig(self, change_id): |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 58 | """Check if the owners configuration in a change is valid.""" |
| 59 | raise Exception('Not implemented') |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 60 | |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 61 | def GetFilesApprovalStatus( |
| 62 | self, project, branch, paths, approvers, reviewers): |
| 63 | """Check the approval status for the given paths. |
| 64 | |
| 65 | Utility method to check for approval status when a change has not yet been |
| 66 | created, given reviewers and approvers. |
| 67 | |
| 68 | See GetChangeApprovalStatus for description of the returned value. |
| 69 | """ |
| 70 | approvers = set(approvers) |
| 71 | reviewers = set(reviewers) |
| 72 | status = {} |
| 73 | for path in paths: |
| 74 | path_owners = set(self.ListOwnersForFile(project, branch, path)) |
| 75 | if path_owners.intersection(approvers): |
| 76 | status[path] = APPROVED |
| 77 | elif path_owners.intersection(reviewers): |
| 78 | status[path] = PENDING |
| 79 | else: |
| 80 | status[path] = INSUFFICIENT_REVIEWERS |
| 81 | return status |
| 82 | |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 83 | |
| 84 | class DepotToolsClient(OwnersClient): |
| 85 | """Implement OwnersClient using owners.py Database.""" |
Edward Lesmes | eeca9c6 | 2020-11-20 00:00:17 +0000 | [diff] [blame] | 86 | def __init__(self, host, root, branch, fopen=open, os_path=os.path): |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 87 | super(DepotToolsClient, self).__init__(host) |
| 88 | self._root = root |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 89 | self._fopen = fopen |
| 90 | self._os_path = os_path |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 91 | self._branch = branch |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame^] | 92 | self._db = owners_db.Database(root, fopen, os_path) |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 93 | self._db.override_files = self._GetOriginalOwnersFiles() |
| 94 | |
| 95 | def _GetOriginalOwnersFiles(self): |
| 96 | return { |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 97 | f: scm.GIT.GetOldContents(self._root, f, self._branch) |
| 98 | for _, f in scm.GIT.CaptureStatus(self._root, self._branch) |
| 99 | if os.path.basename(f) == 'OWNERS' |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 100 | } |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 101 | |
| 102 | def ListOwnersForFile(self, _project, _branch, path): |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame^] | 103 | # all_possible_owners returns a dict {owner: [(path, distance)]}. We want to |
| 104 | # return a list of owners sorted by increasing distance. |
| 105 | distance_by_owner = self._db.all_possible_owners([path], None) |
| 106 | # We add a small random number to the distance, so that owners at the same |
| 107 | # distance are returned in random order to avoid overloading those who would |
| 108 | # appear first. |
| 109 | return sorted( |
| 110 | distance_by_owner, |
| 111 | key=lambda o: distance_by_owner[o][0][1] + random.random()) |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 112 | |
| 113 | def GetChangeApprovalStatus(self, change_id): |
| 114 | data = gerrit_util.GetChange( |
| 115 | self._host, change_id, |
| 116 | ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES', |
| 117 | 'CURRENT_REVISION']) |
| 118 | |
| 119 | reviewers = [r['email'] for r in data['reviewers']['REVIEWER']] |
| 120 | |
| 121 | # Get reviewers that have approved this change |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 122 | label = data['labels']['Code-Review'] |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 123 | max_value = max(int(v) for v in label['values']) |
| 124 | approvers = [v['email'] for v in label['all'] if v['value'] == max_value] |
| 125 | |
| 126 | files = data['revisions'][data['current_revision']]['files'] |
Edward Lesmes | e7d1862 | 2020-11-19 23:46:17 +0000 | [diff] [blame] | 127 | return self.GetFilesApprovalStatus(None, None, files, approvers, reviewers) |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 128 | |
| 129 | def ValidateOwnersConfig(self, change_id): |
| 130 | data = gerrit_util.GetChange( |
| 131 | self._host, change_id, |
| 132 | ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES', |
| 133 | 'CURRENT_REVISION']) |
| 134 | |
| 135 | files = data['revisions'][data['current_revision']]['files'] |
| 136 | |
Edward Lesmes | 64e8076 | 2020-11-24 19:46:45 +0000 | [diff] [blame^] | 137 | db = owners_db.Database(self._root, self._fopen, self._os_path) |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame] | 138 | try: |
| 139 | db.load_data_needed_for( |
| 140 | [f for f in files if os.path.basename(f) == 'OWNERS']) |
| 141 | except Exception as e: |
| 142 | raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e) |