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 | |
| 17 | class OwnersClient(object): |
| 18 | """Interact with OWNERS files in a repository. |
| 19 | |
| 20 | This class allows you to interact with OWNERS files in a repository both the |
| 21 | Gerrit Code-Owners plugin REST API, and the owners database implemented by |
| 22 | Depot Tools in owners.py: |
| 23 | |
| 24 | - List all the owners for a change. |
| 25 | - Check if a change has been approved. |
| 26 | - Check if the OWNERS configuration in a change is valid. |
| 27 | |
| 28 | All code should use this class to interact with OWNERS files instead of the |
| 29 | owners database in owners.py |
| 30 | """ |
| 31 | def __init__(self, host): |
| 32 | self._host = host |
| 33 | |
| 34 | def ListOwnersForFile(self, project, branch, path): |
| 35 | """List all owners for a file.""" |
| 36 | raise Exception('Not implemented') |
| 37 | |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 38 | def GetChangeApprovalStatus(self, change_id): |
| 39 | """Check the approval status for the latest patch in a change. |
| 40 | |
| 41 | Returns a map of path to approval status, where the status can be one of: |
| 42 | - APPROVED: An owner of the file has reviewed the change. |
| 43 | - PENDING: An owner of the file has been added as a reviewer, but no owner |
| 44 | has approved. |
| 45 | - INSUFFICIENT_REVIEWERS: No owner of the file has been added as a reviewer. |
| 46 | """ |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 47 | raise Exception('Not implemented') |
| 48 | |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 49 | def IsOwnerConfigurationValid(self, change_id, patch): |
Edward Lesmes | 91bb750 | 2020-11-06 00:50:24 +0000 | [diff] [blame] | 50 | """Check if the owners configuration in a change is valid.""" |
| 51 | raise Exception('Not implemented') |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | class DepotToolsClient(OwnersClient): |
| 55 | """Implement OwnersClient using owners.py Database.""" |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 56 | def __init__(self, host, root, fopen, os_path, branch): |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 57 | super(DepotToolsClient, self).__init__(host) |
| 58 | self._root = root |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 59 | self._branch = branch |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 60 | self._db = owners.Database(root, fopen, os_path) |
| 61 | self._db.override_files = self._GetOriginalOwnersFiles() |
| 62 | |
| 63 | def _GetOriginalOwnersFiles(self): |
| 64 | return { |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 65 | f: scm.GIT.GetOldContents(self._root, f, self._branch) |
| 66 | for _, f in scm.GIT.CaptureStatus(self._root, self._branch) |
| 67 | if os.path.basename(f) == 'OWNERS' |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 68 | } |
Edward Lesmes | b4f4226 | 2020-11-10 23:41:35 +0000 | [diff] [blame] | 69 | |
| 70 | def ListOwnersForFile(self, _project, _branch, path): |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 71 | return sorted(self._db.all_possible_owners([path], None)) |
| 72 | |
| 73 | def GetChangeApprovalStatus(self, change_id): |
| 74 | data = gerrit_util.GetChange( |
| 75 | self._host, change_id, |
| 76 | ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES', |
| 77 | 'CURRENT_REVISION']) |
| 78 | |
| 79 | reviewers = [r['email'] for r in data['reviewers']['REVIEWER']] |
| 80 | |
| 81 | # Get reviewers that have approved this change |
Edward Lesmes | 829ce02 | 2020-11-18 18:30:31 +0000 | [diff] [blame] | 82 | label = data['labels']['Code-Review'] |
Edward Lesmes | d4e6fb6 | 2020-11-17 00:17:58 +0000 | [diff] [blame] | 83 | max_value = max(int(v) for v in label['values']) |
| 84 | approvers = [v['email'] for v in label['all'] if v['value'] == max_value] |
| 85 | |
| 86 | files = data['revisions'][data['current_revision']]['files'] |
| 87 | |
| 88 | self._db.load_data_needed_for(files) |
| 89 | |
| 90 | status = {} |
| 91 | for f in files: |
| 92 | if self._db.is_covered_by(f, approvers): |
| 93 | status[f] = APPROVED |
| 94 | elif self._db.is_covered_by(f, reviewers): |
| 95 | status[f] = PENDING |
| 96 | else: |
| 97 | status[f] = INSUFFICIENT_REVIEWERS |
| 98 | return status |