blob: da259a3c0507303e8a42992bcfd557c51da3e99e [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
17class 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 Lesmesd4e6fb62020-11-17 00:17:58 +000038 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 Lesmes91bb7502020-11-06 00:50:24 +000047 raise Exception('Not implemented')
48
Edward Lesmesb4f42262020-11-10 23:41:35 +000049 def IsOwnerConfigurationValid(self, change_id, patch):
Edward Lesmes91bb7502020-11-06 00:50:24 +000050 """Check if the owners configuration in a change is valid."""
51 raise Exception('Not implemented')
Edward Lesmesb4f42262020-11-10 23:41:35 +000052
53
54class DepotToolsClient(OwnersClient):
55 """Implement OwnersClient using owners.py Database."""
Edward Lesmes829ce022020-11-18 18:30:31 +000056 def __init__(self, host, root, fopen, os_path, branch):
Edward Lesmesb4f42262020-11-10 23:41:35 +000057 super(DepotToolsClient, self).__init__(host)
58 self._root = root
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000059 self._branch = branch
Edward Lesmes829ce022020-11-18 18:30:31 +000060 self._db = owners.Database(root, fopen, os_path)
61 self._db.override_files = self._GetOriginalOwnersFiles()
62
63 def _GetOriginalOwnersFiles(self):
64 return {
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000065 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 Lesmes829ce022020-11-18 18:30:31 +000068 }
Edward Lesmesb4f42262020-11-10 23:41:35 +000069
70 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000071 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 Lesmes829ce022020-11-18 18:30:31 +000082 label = data['labels']['Code-Review']
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000083 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