2016-04-14 15:10:57 +00:00
|
|
|
module Gitlab
|
2016-03-10 17:43:57 +00:00
|
|
|
module ImportExport
|
|
|
|
class MembersMapper
|
|
|
|
|
2016-06-01 16:03:51 +00:00
|
|
|
attr_reader :note_member_list
|
2016-03-10 17:43:57 +00:00
|
|
|
|
2016-05-13 14:25:27 +00:00
|
|
|
def initialize(exported_members:, user:, project:)
|
2016-03-10 17:43:57 +00:00
|
|
|
@exported_members = exported_members
|
2016-03-11 11:55:50 +00:00
|
|
|
@user = user
|
2016-05-13 14:25:27 +00:00
|
|
|
@project = project
|
2016-05-13 10:33:13 +00:00
|
|
|
@note_member_list = []
|
|
|
|
|
2016-06-13 14:12:40 +00:00
|
|
|
# This needs to run first, as second call would be from #map
|
2016-05-13 17:51:07 +00:00
|
|
|
# which means project members already exist.
|
2016-06-02 08:59:54 +00:00
|
|
|
ensure_default_member!
|
|
|
|
end
|
2016-05-13 10:33:13 +00:00
|
|
|
|
2016-06-02 08:59:54 +00:00
|
|
|
def map
|
2016-06-01 16:03:51 +00:00
|
|
|
@map ||=
|
|
|
|
begin
|
2016-06-02 08:59:54 +00:00
|
|
|
@exported_members.inject(missing_keys_tracking_hash) do |hash, member|
|
2016-06-01 16:03:51 +00:00
|
|
|
existing_user = User.where(find_project_user_query(member)).first
|
2016-06-02 08:59:54 +00:00
|
|
|
old_user_id = member['user']['id']
|
2016-06-13 14:12:40 +00:00
|
|
|
if existing_user && add_user_as_team_member(existing_user, member)
|
2016-06-01 16:03:51 +00:00
|
|
|
hash[old_user_id] = existing_user.id
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
2016-03-11 11:55:50 +00:00
|
|
|
end
|
|
|
|
|
2016-06-13 14:12:40 +00:00
|
|
|
def default_user_id
|
|
|
|
@user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-02 08:59:54 +00:00
|
|
|
def missing_keys_tracking_hash
|
|
|
|
Hash.new do |_, key|
|
|
|
|
@note_member_list << key
|
2016-06-13 14:12:40 +00:00
|
|
|
default_user_id
|
2016-06-02 08:59:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_default_member!
|
|
|
|
ProjectMember.create!(user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true)
|
|
|
|
end
|
2016-06-01 16:03:51 +00:00
|
|
|
|
|
|
|
def add_user_as_team_member(existing_user, member)
|
2016-03-11 11:55:50 +00:00
|
|
|
member['user'] = existing_user
|
2016-06-02 08:59:54 +00:00
|
|
|
|
2016-06-13 14:12:40 +00:00
|
|
|
ProjectMember.create(member_hash(member)).persisted?
|
2016-03-11 11:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def member_hash(member)
|
2016-05-19 13:36:20 +00:00
|
|
|
member.except('id').merge(source_id: @project.id, importing: true)
|
2016-03-11 11:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_project_user_query(member)
|
|
|
|
user_arel[:username].eq(member['user']['username']).or(user_arel[:email].eq(member['user']['email']))
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_arel
|
|
|
|
@user_arel ||= User.arel_table
|
2016-03-10 17:43:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|