blob: e5f2be1f0eb9dd66beb7cdce6f3ef7260436a8b3 [file] [log] [blame]
Edward Lesmes91bb7502020-11-06 00:50:24 +00001# 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 Lesmesd4e6fb62020-11-17 00:17:58 +00005import os
6
Edward Lesmes829ce022020-11-18 18:30:31 +00007import gerrit_util
Edward Lesmesb4f42262020-11-10 23:41:35 +00008import owners
Edward Lesmesd4e6fb62020-11-17 00:17:58 +00009import scm
10
11
12APPROVED = 'APPROVED'
13PENDING = 'PENDING'
14INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS'
Edward Lesmesb4f42262020-11-10 23:41:35 +000015
Edward Lesmes91bb7502020-11-06 00:50:24 +000016
Edward Lesmesb4721682020-11-19 22:59:57 +000017class InvalidOwnersConfig(Exception):
18 pass
19
20
Edward Lesmes91bb7502020-11-06 00:50:24 +000021class 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 Lesmesd4e6fb62020-11-17 00:17:58 +000042 def GetChangeApprovalStatus(self, change_id):
Edward Lesmesb4721682020-11-19 22:59:57 +000043 """Check the approval status for the latest revision_id in a change.
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000044
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 Lesmes91bb7502020-11-06 00:50:24 +000051 raise Exception('Not implemented')
52
Edward Lesmesb4721682020-11-19 22:59:57 +000053 def ValidateOwnersConfig(self, change_id):
Edward Lesmes91bb7502020-11-06 00:50:24 +000054 """Check if the owners configuration in a change is valid."""
55 raise Exception('Not implemented')
Edward Lesmesb4f42262020-11-10 23:41:35 +000056
Edward Lesmese7d18622020-11-19 23:46:17 +000057 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 Lesmesb4f42262020-11-10 23:41:35 +000079
80class DepotToolsClient(OwnersClient):
81 """Implement OwnersClient using owners.py Database."""
Edward Lesmes829ce022020-11-18 18:30:31 +000082 def __init__(self, host, root, fopen, os_path, branch):
Edward Lesmesb4f42262020-11-10 23:41:35 +000083 super(DepotToolsClient, self).__init__(host)
84 self._root = root
Edward Lesmesb4721682020-11-19 22:59:57 +000085 self._fopen = fopen
86 self._os_path = os_path
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000087 self._branch = branch
Edward Lesmes829ce022020-11-18 18:30:31 +000088 self._db = owners.Database(root, fopen, os_path)
89 self._db.override_files = self._GetOriginalOwnersFiles()
90
91 def _GetOriginalOwnersFiles(self):
92 return {
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000093 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 Lesmes829ce022020-11-18 18:30:31 +000096 }
Edward Lesmesb4f42262020-11-10 23:41:35 +000097
98 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000099 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 Lesmes829ce022020-11-18 18:30:31 +0000110 label = data['labels']['Code-Review']
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000111 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 Lesmese7d18622020-11-19 23:46:17 +0000115 return self.GetFilesApprovalStatus(None, None, files, approvers, reviewers)
Edward Lesmesb4721682020-11-19 22:59:57 +0000116
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)