Merge "Only associate preferred addresses with owners"

This commit is contained in:
Zuul 2021-04-07 00:31:18 +00:00 committed by Gerrit Code Review
commit fe297b0cf5
1 changed files with 62 additions and 51 deletions

View File

@ -343,24 +343,41 @@ def main(options):
# their first and record specific account # their first and record specific account
# details # details
if new: if new:
# Get the set of all E-mail addresses # Get the E-mail address preferred in
# Gerrit knows for this owner's account # Gerrit for this owner's account
emails = utils.query_gerrit( email = change['owner'].get('email')
'accounts/%s/emails' %
change['owner']['_account_id'], # If the owner has no preferred address, use the
verbose=options.verbose) # committer address for the first revision
if not email:
print(
'ACCOUNT WITHOUT PREFERRED EMAIL: %s'
% owner,
file=sys.stderr)
full = utils.query_gerrit('changes/', params={
'q': change['_number'],
'o': [
'ALL_COMMITS',
'ALL_REVISIONS',
'DETAILED_ACCOUNTS',
],
}, verbose=options.verbose)[0]
for rev in full['revisions'].values():
if rev['_number'] == 1:
email = rev[
'commit']['committer']['email']
break
# Find duplicate addresses and merge # Find duplicate addresses and merge
# accounts when that happens # accounts when that happens
for email in emails: address = normalize_email(email)
address = normalize_email(email['email']) if address in all_emails:
if address in all_emails: owner = all_emails[address]
owner = all_emails[address] duplicates[new_owner] = owner
duplicates[new_owner] = owner print(
print( 'MERGING DUPLICATE ACCOUNT: %s into %s'
'MERGING DUPLICATE ACCOUNT: %s into %s' % (new_owner, owner), file=sys.stderr)
% (new_owner, owner), file=sys.stderr) continue
break
# For newly found non-duplicate owners, # For newly found non-duplicate owners,
# initialize the global change count, # initialize the global change count,
@ -400,46 +417,40 @@ def main(options):
# We only want to add addresses if this is a # We only want to add addresses if this is a
# new owner or a new duplicate # new owner or a new duplicate
if new: if new:
# Iterate over each E-mail address # Normalize the address before
for email in emails: # performing any matching since
# Normalize the address before # Gerrit doesn't do a great job of
# performing any matching since # this on its own
# Gerrit doesn't do a great job of address = normalize_email(email)
# this on its own
address = normalize_email(email['email'])
# Track this in the full list of all # Track this in the full list of all
# known E-mail addresses # known E-mail addresses
all_emails[address] = owner all_emails[address] = owner
# Whether Gerrit considers this the # Store the preferred E-mail address
# preferred E-mail address # under its own key since it has a
preferred = email.get('preferred', False) # special status, but only if this
# is not a duplicate account
if owner == new_owner:
owners[owner]['preferred'] = email
# Store the preferred E-mail address # If this was already added to
# under its own key since it has a # the extras list due to an
# special status, but only if this # additional pre-normalized
# is not a duplicate account # copy, remove it there
if preferred and owner == new_owner: if address in owners[owner]['extra']:
owners[owner]['preferred'] = address owners[owner]['extra'].remove(address)
# If this was already added to # Store a list of non-preferred
# the extras list due to an # addresses, deduplicating them in
# additional pre-normalized # case they match post-normalization
# copy, remove it there # and treating duplicate preferred
if address in owners[owner]['extra']: # addresses as # non-preferred
owners[owner]['extra'].remove(address) else:
if ((address not in owners[owner]['extra'])
# Store a list of non-preferred and (address != owners[owner].get(
# addresses, deduplicating them in 'preferred', ''))):
# case they match post-normalization owners[owner]['extra'].append(address)
# and treating duplicate preferred
# addresses as # non-preferred
else:
if ((address not in owners[owner]['extra'])
and (address != owners[owner].get(
'preferred', ''))):
owners[owner]['extra'].append(address)
# If we've seen this owner on another change # If we've seen this owner on another change
# in a repo under this project-team then # in a repo under this project-team then