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