debian-mirror-gitlab/lib/gitlab/import_export/members_mapper.rb

150 lines
4.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class MembersMapper
2020-01-01 13:55:28 +05:30
def initialize(exported_members:, user:, importable:)
2017-08-17 22:00:37 +05:30
@exported_members = user.admin? ? exported_members : []
2016-06-22 15:30:34 +05:30
@user = user
2020-01-01 13:55:28 +05:30
@importable = importable
2016-06-22 15:30:34 +05:30
# This needs to run first, as second call would be from #map
2020-03-13 15:44:24 +05:30
# which means Project/Group members already exist.
2016-06-22 15:30:34 +05:30
ensure_default_member!
end
def map
@map ||=
begin
@exported_members.inject(missing_keys_tracking_hash) do |hash, member|
2016-09-13 17:45:13 +05:30
if member['user']
old_user_id = member['user']['id']
2020-04-22 19:07:51 +05:30
existing_user = User.find_by(find_user_query(member))
2016-09-13 17:45:13 +05:30
hash[old_user_id] = existing_user.id if existing_user && add_team_member(member, existing_user)
else
add_team_member(member)
2016-06-22 15:30:34 +05:30
end
2016-09-13 17:45:13 +05:30
2016-06-22 15:30:34 +05:30
hash
end
end
end
def default_user_id
@user.id
end
2021-01-03 14:25:43 +05:30
def include?(old_user_id)
map.has_key?(old_user_id)
2017-08-17 22:00:37 +05:30
end
2016-06-22 15:30:34 +05:30
private
def missing_keys_tracking_hash
Hash.new do |_, key|
default_user_id
end
end
def ensure_default_member!
2020-03-13 15:44:24 +05:30
return if user_already_member?
2020-06-23 00:09:42 +05:30
@importable.members.destroy_all # rubocop: disable Cop/DestroyAll
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
relation_class.create!(user: @user, access_level: highest_access_level, source_id: @importable.id, importing: true)
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2020-01-01 13:55:28 +05:30
raise e, "Error adding importer user to #{@importable.class} members. #{e.message}"
2016-06-22 15:30:34 +05:30
end
2020-03-13 15:44:24 +05:30
def user_already_member?
member = @importable.members&.first
2020-04-08 14:13:33 +05:30
member&.user == @user && member.access_level >= highest_access_level
2020-03-13 15:44:24 +05:30
end
2016-09-13 17:45:13 +05:30
def add_team_member(member, existing_user = nil)
2021-01-03 14:25:43 +05:30
return true if existing_user && @importable.members.exists?(user_id: existing_user.id)
2016-06-22 15:30:34 +05:30
member['user'] = existing_user
2020-04-22 19:07:51 +05:30
member_hash = member_hash(member)
2016-06-22 15:30:34 +05:30
2020-04-22 19:07:51 +05:30
member = relation_class.create(member_hash)
if member.persisted?
log_member_addition(member_hash)
true
else
log_member_addition_failure(member_hash, member.errors.full_messages)
false
end
2016-06-22 15:30:34 +05:30
end
def member_hash(member)
2019-09-04 21:01:54 +05:30
parsed_hash(member).merge(
2020-01-01 13:55:28 +05:30
'source_id' => @importable.id,
2019-09-04 21:01:54 +05:30
'importing' => true,
2020-04-08 14:13:33 +05:30
'access_level' => [member['access_level'], highest_access_level].min
2019-09-04 21:01:54 +05:30
).except('user_id')
2016-11-03 12:29:30 +05:30
end
def parsed_hash(member)
2020-01-01 13:55:28 +05:30
Gitlab::ImportExport::AttributeCleaner.clean(relation_hash: member.deep_stringify_keys,
relation_class: relation_class)
2016-06-22 15:30:34 +05:30
end
2020-01-01 13:55:28 +05:30
def find_user_query(member)
2020-03-13 15:44:24 +05:30
user_arel[:email].eq(member['user']['email'])
2016-06-22 15:30:34 +05:30
end
def user_arel
@user_arel ||= User.arel_table
end
2020-01-01 13:55:28 +05:30
def relation_class
case @importable
2020-04-08 14:13:33 +05:30
when ::Project
2020-01-01 13:55:28 +05:30
ProjectMember
2020-04-08 14:13:33 +05:30
when ::Group
2020-01-01 13:55:28 +05:30
GroupMember
end
end
2020-04-08 14:13:33 +05:30
def highest_access_level
return relation_class::OWNER if relation_class == GroupMember
relation_class::MAINTAINER
end
2020-04-22 19:07:51 +05:30
def log_member_addition(member_hash)
log_params = base_log_params(member_hash)
log_params[:message] = '[Project/Group Import] Added new member'
logger.info(log_params)
end
def log_member_addition_failure(member_hash, errors)
log_params = base_log_params(member_hash)
log_params[:message] = "[Project/Group Import] Member addition failed: #{errors&.join(', ')}"
logger.info(log_params)
end
def base_log_params(member_hash)
{
user_id: member_hash['user']&.id,
access_level: member_hash['access_level'],
importable_type: @importable.class.to_s,
importable_id: @importable.id,
root_namespace_id: @importable.try(:root_ancestor)&.id
}
end
def logger
@logger ||= Gitlab::Import::Logger.build
end
2016-06-22 15:30:34 +05:30
end
end
end