1# Copyright (C) 2020 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the 'License'); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an 'AS IS' BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Find main reviewers for git push commands.""" 15 16from collections.abc import MutableMapping 17import math 18import random 19from typing import List, Set, Union 20 21# To randomly pick one of multiple reviewers, we put them in a List[str] 22# to work with random.choice efficiently. 23# To pick all of multiple reviewers, we use a Set[str]. 24 25# A ProjMapping maps a project path string to 26# (1) a single reviewer email address as a string, or 27# (2) a List of multiple reviewers to be randomly picked, or 28# (3) a Set of multiple reviewers to be all added. 29ProjMapping = MutableMapping[str, Union[str, List[str], Set[str]]] 30 31# Rust crate owners (reviewers). 32RUST_CRATE_OWNERS: ProjMapping = { 33 'rust/crates/anyhow': 'mmaurer@google.com', 34 # more rust crate owners could be added later 35 # if so, consider modifying the quotas in RUST_REVIEWERS 36} 37 38PROJ_REVIEWERS: ProjMapping = { 39 # define non-rust project reviewers here 40} 41 42# Combine all roject reviewers. 43PROJ_REVIEWERS.update(RUST_CRATE_OWNERS) 44 45# Reviewers for external/rust/crates projects not found in PROJ_REVIEWER. 46# Each person has a quota, the number of projects to review. 47# The sum of these quotas should ideally be at least the number of Rust 48# projects, but this only matters if we have many entries in RUST_CRATE_OWNERS, 49# as we subtract a person's owned crates from their quota. 50RUST_REVIEWERS: dict[str, float] = { 51 'ivanlozano@google.com': 20, 52 'jeffv@google.com': 20, 53 'mmaurer@google.com': 20, 54 'srhines@google.com': 20, 55 'tweek@google.com': 20, 56 # If a Rust reviewer needs to take a vacation, comment out the line, 57 # and distribute the quota to other reviewers. 58} 59 60 61# pylint: disable=invalid-name 62def add_proj_count(projects: MutableMapping[str, float], reviewer: str, n: float) -> None: 63 """Add n to the number of projects owned by the reviewer.""" 64 if reviewer in projects: 65 projects[reviewer] += n 66 else: 67 projects[reviewer] = n 68 69 70# Random Rust reviewers are selected from RUST_REVIEWER_LIST, 71# which is created from RUST_REVIEWERS and PROJ_REVIEWERS. 72# A person P in RUST_REVIEWERS will occur in the RUST_REVIEWER_LIST N times, 73# if N = RUST_REVIEWERS[P] - (number of projects owned by P in PROJ_REVIEWERS) 74# is greater than 0. N is rounded up by math.ceil. 75def create_rust_reviewer_list() -> List[str]: 76 """Create a list of duplicated reviewers for weighted random selection.""" 77 # Count number of projects owned by each reviewer. 78 rust_reviewers = set(RUST_REVIEWERS.keys()) 79 projects: dict[str, float] = {} # map from owner to number of owned projects 80 for value in PROJ_REVIEWERS.values(): 81 if isinstance(value, str): # single reviewer for a project 82 add_proj_count(projects, value, 1) 83 continue 84 # multiple reviewers share one project, count only rust_reviewers 85 # pylint: disable=bad-builtin 86 reviewers = set(filter(lambda x: x in rust_reviewers, value)) 87 if reviewers: 88 count = 1.0 / len(reviewers) # shared among all reviewers 89 for name in reviewers: 90 add_proj_count(projects, name, count) 91 result = [] 92 for (x, n) in RUST_REVIEWERS.items(): 93 if x in projects: # reduce x's quota by the number of assigned ones 94 n = n - projects[x] 95 if n > 0: 96 result.extend([x] * math.ceil(n)) 97 if result: 98 return result 99 # Something was wrong or quotas were too small so that nobody 100 # was selected from the RUST_REVIEWERS. Select everyone!! 101 return list(RUST_REVIEWERS.keys()) 102 103 104RUST_REVIEWER_LIST: List[str] = create_rust_reviewer_list() 105 106 107def find_reviewers(proj_path: str) -> str: 108 """Returns an empty string or a reviewer parameter(s) for git push.""" 109 index = proj_path.find('/external/') 110 if index >= 0: # full path 111 proj_path = proj_path[(index + len('/external/')):] 112 elif proj_path.startswith('external/'): # relative path 113 proj_path = proj_path[len('external/'):] 114 if proj_path in PROJ_REVIEWERS: 115 reviewers = PROJ_REVIEWERS[proj_path] 116 # pylint: disable=isinstance-second-argument-not-valid-type 117 if isinstance(reviewers, List): # pick any one reviewer 118 return 'r=' + random.choice(reviewers) 119 if isinstance(reviewers, Set): # add all reviewers in sorted order 120 # pylint: disable=bad-builtin 121 return ','.join(map(lambda x: 'r=' + x, sorted(reviewers))) 122 # reviewers must be a string 123 return 'r=' + reviewers 124 if proj_path.startswith('rust/crates/'): 125 return 'r=' + random.choice(RUST_REVIEWER_LIST) 126 return '' 127