blob: 783ae9d9c9162c8ff8f7c3afe5856c31015149ad [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
Edward Lesmes64e80762020-11-24 19:46:45 +00006import random
Edward Lesmesd4e6fb62020-11-17 00:17:58 +00007
Edward Lesmes829ce022020-11-18 18:30:31 +00008import gerrit_util
Edward Lesmes64e80762020-11-24 19:46:45 +00009import owners as owners_db
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000010import scm
11
12
13APPROVED = 'APPROVED'
14PENDING = 'PENDING'
15INSUFFICIENT_REVIEWERS = 'INSUFFICIENT_REVIEWERS'
Edward Lesmesb4f42262020-11-10 23:41:35 +000016
Edward Lesmes91bb7502020-11-06 00:50:24 +000017
Edward Lesmesb4721682020-11-19 22:59:57 +000018class InvalidOwnersConfig(Exception):
19 pass
20
21
Edward Lesmes91bb7502020-11-06 00:50:24 +000022class OwnersClient(object):
23 """Interact with OWNERS files in a repository.
24
25 This class allows you to interact with OWNERS files in a repository both the
26 Gerrit Code-Owners plugin REST API, and the owners database implemented by
27 Depot Tools in owners.py:
28
29 - List all the owners for a change.
30 - Check if a change has been approved.
31 - Check if the OWNERS configuration in a change is valid.
32
33 All code should use this class to interact with OWNERS files instead of the
34 owners database in owners.py
35 """
36 def __init__(self, host):
37 self._host = host
38
39 def ListOwnersForFile(self, project, branch, path):
Edward Lesmes64e80762020-11-24 19:46:45 +000040 """List all owners for a file.
41
42 The returned list is sorted so that better owners appear first.
43 """
Edward Lesmes91bb7502020-11-06 00:50:24 +000044 raise Exception('Not implemented')
45
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000046 def GetChangeApprovalStatus(self, change_id):
Edward Lesmesb4721682020-11-19 22:59:57 +000047 """Check the approval status for the latest revision_id in a change.
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000048
49 Returns a map of path to approval status, where the status can be one of:
50 - APPROVED: An owner of the file has reviewed the change.
51 - PENDING: An owner of the file has been added as a reviewer, but no owner
52 has approved.
53 - INSUFFICIENT_REVIEWERS: No owner of the file has been added as a reviewer.
54 """
Edward Lesmes91bb7502020-11-06 00:50:24 +000055 raise Exception('Not implemented')
56
Edward Lesmesb4721682020-11-19 22:59:57 +000057 def ValidateOwnersConfig(self, change_id):
Edward Lesmes91bb7502020-11-06 00:50:24 +000058 """Check if the owners configuration in a change is valid."""
59 raise Exception('Not implemented')
Edward Lesmesb4f42262020-11-10 23:41:35 +000060
Edward Lesmese7d18622020-11-19 23:46:17 +000061 def GetFilesApprovalStatus(
62 self, project, branch, paths, approvers, reviewers):
63 """Check the approval status for the given paths.
64
65 Utility method to check for approval status when a change has not yet been
66 created, given reviewers and approvers.
67
68 See GetChangeApprovalStatus for description of the returned value.
69 """
70 approvers = set(approvers)
71 reviewers = set(reviewers)
72 status = {}
73 for path in paths:
74 path_owners = set(self.ListOwnersForFile(project, branch, path))
75 if path_owners.intersection(approvers):
76 status[path] = APPROVED
77 elif path_owners.intersection(reviewers):
78 status[path] = PENDING
79 else:
80 status[path] = INSUFFICIENT_REVIEWERS
81 return status
82
Edward Lesmesb4f42262020-11-10 23:41:35 +000083
84class DepotToolsClient(OwnersClient):
85 """Implement OwnersClient using owners.py Database."""
Edward Lesmeseeca9c62020-11-20 00:00:17 +000086 def __init__(self, host, root, branch, fopen=open, os_path=os.path):
Edward Lesmesb4f42262020-11-10 23:41:35 +000087 super(DepotToolsClient, self).__init__(host)
88 self._root = root
Edward Lesmesb4721682020-11-19 22:59:57 +000089 self._fopen = fopen
90 self._os_path = os_path
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000091 self._branch = branch
Edward Lesmes64e80762020-11-24 19:46:45 +000092 self._db = owners_db.Database(root, fopen, os_path)
Edward Lesmes829ce022020-11-18 18:30:31 +000093 self._db.override_files = self._GetOriginalOwnersFiles()
94
95 def _GetOriginalOwnersFiles(self):
96 return {
Edward Lesmesd4e6fb62020-11-17 00:17:58 +000097 f: scm.GIT.GetOldContents(self._root, f, self._branch)
98 for _, f in scm.GIT.CaptureStatus(self._root, self._branch)
99 if os.path.basename(f) == 'OWNERS'
Edward Lesmes829ce022020-11-18 18:30:31 +0000100 }
Edward Lesmesb4f42262020-11-10 23:41:35 +0000101
102 def ListOwnersForFile(self, _project, _branch, path):
Edward Lesmes64e80762020-11-24 19:46:45 +0000103 # all_possible_owners returns a dict {owner: [(path, distance)]}. We want to
104 # return a list of owners sorted by increasing distance.
105 distance_by_owner = self._db.all_possible_owners([path], None)
106 # We add a small random number to the distance, so that owners at the same
107 # distance are returned in random order to avoid overloading those who would
108 # appear first.
109 return sorted(
110 distance_by_owner,
111 key=lambda o: distance_by_owner[o][0][1] + random.random())
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000112
113 def GetChangeApprovalStatus(self, change_id):
114 data = gerrit_util.GetChange(
115 self._host, change_id,
116 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
117 'CURRENT_REVISION'])
118
119 reviewers = [r['email'] for r in data['reviewers']['REVIEWER']]
120
121 # Get reviewers that have approved this change
Edward Lesmes829ce022020-11-18 18:30:31 +0000122 label = data['labels']['Code-Review']
Edward Lesmesd4e6fb62020-11-17 00:17:58 +0000123 max_value = max(int(v) for v in label['values'])
124 approvers = [v['email'] for v in label['all'] if v['value'] == max_value]
125
126 files = data['revisions'][data['current_revision']]['files']
Edward Lesmese7d18622020-11-19 23:46:17 +0000127 return self.GetFilesApprovalStatus(None, None, files, approvers, reviewers)
Edward Lesmesb4721682020-11-19 22:59:57 +0000128
129 def ValidateOwnersConfig(self, change_id):
130 data = gerrit_util.GetChange(
131 self._host, change_id,
132 ['DETAILED_ACCOUNTS', 'DETAILED_LABELS', 'CURRENT_FILES',
133 'CURRENT_REVISION'])
134
135 files = data['revisions'][data['current_revision']]['files']
136
Edward Lesmes64e80762020-11-24 19:46:45 +0000137 db = owners_db.Database(self._root, self._fopen, self._os_path)
Edward Lesmesb4721682020-11-19 22:59:57 +0000138 try:
139 db.load_data_needed_for(
140 [f for f in files if os.path.basename(f) == 'OWNERS'])
141 except Exception as e:
142 raise InvalidOwnersConfig('Error parsing OWNERS files:\n%s' % e)