def _merge_user_profiles(self, user_profiles): merged_user = {} # merged user profile # collect ordinary fields for key in [ 'seq', 'user_name', 'user_id', 'gerrit_id', 'launchpad_id', 'companies', 'static' ]: value = next((v.get(key) for v in user_profiles if v.get(key)), None) if value: merged_user[key] = value # update user_id, prefer it to be equal to launchpad_id merged_user['user_id'] = (merged_user.get('launchpad_id') or merged_user.get('user_id')) # merge emails emails = set([]) core_in = set([]) for u in user_profiles: emails |= set(u.get('emails', [])) core_in |= set(u.get('core', [])) merged_user['emails'] = list(emails) if core_in: merged_user['core'] = list(core_in) # merge companies merged_companies = merged_user['companies'] for u in user_profiles: companies = u.get('companies') if companies: if (companies[0]['company_name'] != self._get_independent() or len(companies) > 1): merged_companies = companies break merged_user['companies'] = merged_companies self._update_user_affiliation(merged_user) seqs = set(u.get('seq') for u in user_profiles if u.get('seq')) if len(seqs) > 1: # profiles are merged, keep only one, remove others seqs.remove(merged_user['seq']) for u in user_profiles: if u.get('seq') in seqs: LOG.debug('Delete user: %s', u) user_processor.delete_user(self.runtime_storage_inst, u) return merged_user
def _merge_user_profiles(self, user_profiles): merged_user = {} # merged user profile # collect ordinary fields for key in ['seq', 'user_name', 'user_id', 'gerrit_id', 'launchpad_id', 'companies', 'static']: value = next((v.get(key) for v in user_profiles if v.get(key)), None) if value: merged_user[key] = value # update user_id, prefer it to be equal to launchpad_id merged_user['user_id'] = (merged_user.get('launchpad_id') or merged_user.get('user_id')) # merge emails emails = set([]) core_in = set([]) for u in user_profiles: emails |= set(u.get('emails', [])) core_in |= set(u.get('core', [])) merged_user['emails'] = list(emails) if core_in: merged_user['core'] = list(core_in) # merge companies merged_companies = merged_user['companies'] for u in user_profiles: companies = u.get('companies') if companies: if (companies[0]['company_name'] != self._get_independent() or len(companies) > 1): merged_companies = companies break merged_user['companies'] = merged_companies self._update_user_affiliation(merged_user) seqs = set(u.get('seq') for u in user_profiles if u.get('seq')) if len(seqs) > 1: # profiles are merged, keep only one, remove others seqs.remove(merged_user['seq']) for u in user_profiles: if u.get('seq') in seqs: LOG.debug('Delete user: %s', u) user_processor.delete_user( self.runtime_storage_inst, u) return merged_user
def _merge_user_profiles(self, user_profiles): LOG.debug("Merge profiles: %s", user_profiles) # check of there are more than 1 launchpad_id nor gerrit_id lp_ids = set(u.get("launchpad_id") for u in user_profiles if u.get("launchpad_id")) if len(lp_ids) > 1: LOG.info("Ambiguous launchpad ids: %s on profiles: %s", lp_ids, user_profiles) g_ids = set(u.get("gerrit_id") for u in user_profiles if u.get("gerrit_id")) if len(g_ids) > 1: LOG.info("Ambiguous gerrit ids: %s on profiles: %s", g_ids, user_profiles) merged_user = {} # merged user profile # collect ordinary fields for key in [ "seq", "user_name", "user_id", "gerrit_id", "github_id", "launchpad_id", "companies", "static", "ldap_id", ]: value = next((v.get(key) for v in user_profiles if v.get(key)), None) if value: merged_user[key] = value # update user_id, prefer it to be equal to launchpad_id merged_user["user_id"] = merged_user.get("launchpad_id") or merged_user.get("user_id") # merge emails emails = set([]) core_in = set([]) for u in user_profiles: emails |= set(u.get("emails", [])) core_in |= set(u.get("core", [])) merged_user["emails"] = list(emails) if core_in: merged_user["core"] = list(core_in) # merge companies merged_companies = merged_user["companies"] for u in user_profiles: companies = u.get("companies") if companies: if companies[0]["company_name"] != self._get_independent() or len(companies) > 1: merged_companies = companies break merged_user["companies"] = merged_companies self._update_user_affiliation(merged_user) seqs = set(u.get("seq") for u in user_profiles if u.get("seq")) if len(seqs) > 1: # profiles are merged, keep only one, remove others seqs.remove(merged_user["seq"]) for u in user_profiles: if u.get("seq") in seqs: LOG.debug("Delete user: %s", u) user_processor.delete_user(self.runtime_storage_inst, u) return merged_user