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