Skip to content

Instantly share code, notes, and snippets.

@seansawyer
Created October 15, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seansawyer/b36fe28a20c7f4cc97a05be7f34403fb to your computer and use it in GitHub Desktop.
Save seansawyer/b36fe28a20c7f4cc97a05be7f34403fb to your computer and use it in GitHub Desktop.
Randomly choose some teammates for code review, etc.
"""Randomly choose a list of teammates for code review, etc."""
from argparse import ArgumentParser
from getpass import getuser
from itertools import combinations
from random import randint
parser = ArgumentParser(description=__doc__)
parser.add_argument('--email',
help='Print full email address (by default only the shorter username is printed)',
action='store_true')
parser.add_argument('--exclude',
help=('A reviewer to exclude '
'(by default the current login user is excluded). '
'You may supply --exclude repeatedly. '
'Note that --include takes precedence over --exclude.'),
action='append')
parser.add_argument('--include',
help=('A reviewer to include in addition to configured team members '
'(defaults to all team members except the current login user). '
'Note that --include takes precedence over --exclude.'),
action='append')
parser.add_argument('--include-me',
help=('A shorthand to include the current login user, '
'who is normally excluded.'),
action='store_true')
parser.add_argument('-n',
help='The number of revieweres to choose (defaults to 5).',
default=5,
type=int)
teammates = set([
'aguerrera',
'gstevens',
'tmiller',
'vsimmons',
])
def run():
args = parser.parse_args()
for reviewer in sorted(choose_reviewers(args)):
if args.email:
print(reviewer + '@some.domain')
else:
print(reviewer)
def choose_reviewers(args):
candidates = teammates.copy()
# Remove the current login user from consideration.
current_user = getuser()
candidates.remove(current_user)
# Remove any non-empty names supplied via --exclude.
if args.exclude is not None:
for name in args.exclude:
if name:
candidates.remove(name)
# Build an initial list of reviewers from non-empty explicit includes
reviewers = list()
if args.include_me:
reviewers.append(current_user)
if args.include is not None:
for name in args.include:
if name:
try:
candidates.remove(name)
except KeyError:
pass # already not a member
reviewers.append(name)
# Choose a random combination of remaining candidates to get n reviewers.
m = args.n - len(reviewers)
choices = list(combinations(candidates, m))
for name in choices[randint(0, len(choices)-1)]:
reviewers.append(name)
return reviewers
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment