Merge "Lookup candidate info from OSF member DB not git"

This commit is contained in:
Zuul 2018-04-10 14:55:55 +00:00 committed by Gerrit Code Review
commit a890d7869c

View File

@ -19,10 +19,7 @@ import json
import os import os
import pickle import pickle
import pytz import pytz
import re
import requests import requests
import six
import subprocess
import time import time
import yaml import yaml
@ -30,6 +27,7 @@ from six.moves.urllib.parse import quote_plus
from six.moves.urllib.request import urlopen from six.moves.urllib.request import urlopen
from openstack_election import config from openstack_election import config
from openstack_election import owners
# Library constants # Library constants
@ -85,14 +83,7 @@ def gerrit_query(url, params=None):
def get_email(filepath): def get_email(filepath):
cmd = ["git", "log", "--follow", "--format=%aE", filepath] return os.path.basename(filepath)
git = subprocess.Popen(cmd, stdout=subprocess.PIPE)
email = git.stdout.readlines()[-1][:-1]
# Force to text_type in py2 or py3
if isinstance(email, six.text_type):
return email
else:
return email.decode('utf-8')
def get_gerrit_account(email): def get_gerrit_account(email):
@ -105,30 +96,27 @@ def get_gerrit_account(email):
return accounts[0] return accounts[0]
def get_fullname(filepath): def get_irc(member, filepath=None):
member_data = member.get('data', [{}])[0]
return member_data.get('irc', '')
def get_fullname(member, filepath=None):
# Check if filepath is an exception # Check if filepath is an exception
if exceptions is None: if filepath and exceptions is None:
load_exceptions() load_exceptions()
if filepath in exceptions: if filepath and filepath in exceptions:
return exceptions[filepath] return exceptions[filepath]
# Otherwise query gerrit using git log email member_data = member.get('data', [{}])[0]
email = get_email(filepath) try:
fullname = get_gerrit_account(email)['name'] full_name = '%(first_name)s %(last_name)s' % (member_data)
except KeyError:
print('[I] Unable to retrieve fullname from OSF member DB for %s' %
(member))
full_name = u''
# Remove parenthesis content return full_name
fullname = re.sub(r"\([^)]*\)", "", fullname)
# Strip double space and trailing spaces
fullname = re.sub(r" ", " ", fullname).strip()
# Make sure first letter of first and last component are upper-case
s = fullname.split()
fullname = (
[s[0][0].upper() + s[0][1:]] +
s[1:-1] +
[s[-1][0].upper() + s[-1][1:]]
)
return u" ".join(fullname)
def get_reviews(query): def get_reviews(query):
@ -215,20 +203,22 @@ def build_candidates_list(election=conf['release']):
for project in project_list: for project in project_list:
project_prefix = os.path.join(CANDIDATE_PATH, election, project) project_prefix = os.path.join(CANDIDATE_PATH, election, project)
file_list = list(filter( file_list = list(filter(
lambda x: x.endswith(".txt"), lambda x: '@' in x,
os.listdir(project_prefix), os.listdir(project_prefix),
)) ))
candidates_list = [] candidates_list = []
for candidate_file in file_list: for candidate_file in file_list:
filepath = os.path.join(project_prefix, candidate_file) filepath = os.path.join(project_prefix, candidate_file)
email = get_email(filepath)
member = owners.lookup_member(email)
candidates_list.append( candidates_list.append(
{ {
'url': ('%s/%s/plain/%s' % 'url': ('%s/%s/plain/%s' %
(CGIT_URL, ELECTION_REPO, (CGIT_URL, ELECTION_REPO,
quote_plus(filepath, safe='/'))), quote_plus(filepath, safe='/'))),
'ircname': candidate_file[:-4].replace('`', r'\`'), 'email': email,
'email': get_email(filepath), 'ircname': get_irc(member),
'fullname': get_fullname(filepath) 'fullname': get_fullname(member, filepath=filepath)
}) })
candidates_list.sort(key=lambda x: x['fullname']) candidates_list.sort(key=lambda x: x['fullname'])