blob: 3d15020fc1c2ba77aa56e0831da095f080816e4c [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
57
58class DepotToolsClient(OwnersClient):
59 """Implement OwnersClient using owners.py Database."""
Edward Lesmes829ce022020-11-18 18:30:31 +000060 def __init__(self, host, root, fopen, os_path, branch):
Edward Lesmesb4f42262020-11-10 23:41:35 +000061 super(DepotToolsClient, self).__init__(host)
62 self._root = root
Edward Lesmesb4721682020-11-19 22:59:57 +000063 self._fopen = fopen
64 self._os_path = os_path
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000065 self._branch = branch
Edward Lesmes829ce022020-11-18 18:30:31 +000066 self._db = owners.Database(root, fopen, os_path)
67 self._db.override_files = self._GetOriginalOwnersFiles()
68
69 def _GetOriginalOwnersFiles(self):
70 return {
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000071 f: scm.GIT.GetOldContents(self._root, f, self._branch)
72 for _, f in scm.GIT.CaptureStatus(self._root, self._branch)
73 if os.path.basename(f) == 'OWNERS'
Edward Lesmes829ce022020-11-18 18:30:31 +000074 }
Edward Lesmesb4f42262020-11-10 23:41:35 +000075
76 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000077 return sorted(self._db.all_possible_owners([path], None))
78
79 def GetChangeApprovalStatus(self, change_id):
80 data = gerrit_util.GetChange(
81 self._host, change_id,
82 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
83 'CURRENT_REVISION'])
84
85 reviewers = [r['email'] for r in data['reviewers']['REVIEWER']]
86
87 # Get reviewers that have approved this change
Edward Lesmes829ce022020-11-18 18:30:31 +000088 label = data['labels']['Code-Review']
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000089 max_value = max(int(v) for v in label['values'])
90 approvers = [v['email'] for v in label['all'] if v['value'] == max_value]
91
92 files = data['revisions'][data['current_revision']]['files']
93
94 self._db.load_data_needed_for(files)
95
96 status = {}
97 for f in files:
98 if self._db.is_covered_by(f, approvers):
99 status[f] = APPROVED
100 elif self._db.is_covered_by(f, reviewers):
101 status[f] = PENDING
102 else:
103 status[f] = INSUFFICIENT_REVIEWERS
104 return status
Edward Lesmesb4721682020-11-19 22:59:57 +0000105
106 def ValidateOwnersConfig(self, change_id):
107 data = gerrit_util.GetChange(
108 self._host, change_id,
109 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
110 'CURRENT_REVISION'])
111
112 files = data['revisions'][data['current_revision']]['files']
113
114 db = owners.Database(self._root, self._fopen, self._os_path)
115 try:
116 db.load_data_needed_for(
117 [f for f in files if os.path.basename(f) == 'OWNERS'])
118 except Exception as e:
119 raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e)