blob: 6ed0c6b297352802e8a023c00841b1204634c3ff [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 Lesmesb4f42262020-11-10 23:41:35 +00007import owners
Edward Lesmesd4e6fb62020-11-17 00:17:58 +00008import scm
9
10
11APPROVED = 'APPROVED'
12PENDING = 'PENDING'
13INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS'
Edward Lesmesb4f42262020-11-10 23:41:35 +000014
Edward Lesmes91bb7502020-11-06 00:50:24 +000015
16class 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 Lesmesd4e6fb62020-11-17 00:17:58 +000037 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 Lesmes91bb7502020-11-06 00:50:24 +000046 raise Exception('Not implemented')
47
Edward Lesmesb4f42262020-11-10 23:41:35 +000048 def IsOwnerConfigurationValid(self, change_id, patch):
Edward Lesmes91bb7502020-11-06 00:50:24 +000049 """Check if the owners configuration in a change is valid."""
50 raise Exception('Not implemented')
Edward Lesmesb4f42262020-11-10 23:41:35 +000051
52
53class DepotToolsClient(OwnersClient):
54 """Implement OwnersClient using owners.py Database."""
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000055 def __init__(self, host, root, branch):
Edward Lesmesb4f42262020-11-10 23:41:35 +000056 super(DepotToolsClient, self).__init__(host)
57 self._root = root
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000058 self._branch = branch
Edward Lesmesb4f42262020-11-10 23:41:35 +000059 self._db = owners.Database(root, open, os.path)
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000060 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 Lesmesb4f42262020-11-10 23:41:35 +000065
66 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000067 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