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 | |
| 57 | |
| 58 | class DepotToolsClient(OwnersClient): |
| 59 | """Implement OwnersClient using owners.py Database.""" |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 60 | def __init__(self, host, root, fopen, os_path, branch): |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 61 | super(DepotToolsClient, self).__init__(host) |
| 62 | self._root = root |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame^] | 63 | self._fopen = fopen |
| 64 | self._os_path = os_path |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 65 | self._branch = branch |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 66 | self._db = owners.Database(root, fopen, os_path) |
| 67 | self._db.override_files = self._GetOriginalOwnersFiles() |
| 68 | |
| 69 | def _GetOriginalOwnersFiles(self): |
| 70 | return { |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 71 | f: scm.GIT.GetOldContents(self._root, f, self._branch) |
| 72 | for _, f in scm.GIT.CaptureStatus(self._root, self._branch) |
| 73 | if os.path.basename(f) == 'OWNERS' |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 74 | } |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 75 | |
| 76 | def ListOwnersForFile(self, _project, _branch, path): |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 77 | return sorted(self._db.all_possible_owners([path], None)) |
| 78 | |
| 79 | def GetChangeApprovalStatus(self, change_id): |
| 80 | data = gerrit_util.GetChange( |
| 81 | self._host, change_id, |
| 82 | ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES', |
| 83 | 'CURRENT_REVISION']) |
| 84 | |
| 85 | reviewers = [r['email'] for r in data['reviewers']['REVIEWER']] |
| 86 | |
| 87 | # Get reviewers that have approved this change |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 88 | label = data['labels']['Code-Review'] |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 89 | max_value = max(int(v) for v in label['values']) |
| 90 | approvers = [v['email'] for v in label['all'] if v['value'] == max_value] |
| 91 | |
| 92 | files = data['revisions'][data['current_revision']]['files'] |
| 93 | |
| 94 | self._db.load_data_needed_for(files) |
| 95 | |
| 96 | status = {} |
| 97 | for f in files: |
| 98 | if self._db.is_covered_by(f, approvers): |
| 99 | status[f] = APPROVED |
| 100 | elif self._db.is_covered_by(f, reviewers): |
| 101 | status[f] = PENDING |
| 102 | else: |
| 103 | status[f] = INSUFFICIENT_REVIEWERS |
| 104 | return status |
Edward Lesmes | b472168 | 2020-11-19 22:59:57 +0000 | [diff] [blame^] | 105 | |
| 106 | def ValidateOwnersConfig(self, change_id): |
| 107 | data = gerrit_util.GetChange( |
| 108 | self._host, change_id, |
| 109 | ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES', |
| 110 | 'CURRENT_REVISION']) |
| 111 | |
| 112 | files = data['revisions'][data['current_revision']]['files'] |
| 113 | |
| 114 | db = owners.Database(self._root, self._fopen, self._os_path) |
| 115 | try: |
| 116 | db.load_data_needed_for( |
| 117 | [f for f in files if os.path.basename(f) == 'OWNERS']) |
| 118 | except Exception as e: |
| 119 | raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e) |