2016-04-14 15:10:57 +00:00
|
|
|
module Gitlab
|
2016-03-10 17:43:57 +00:00
|
|
|
module ImportExport
|
|
|
|
class MembersMapper
|
2016-05-13 14:25:27 +00:00
|
|
|
def initialize(exported_members:, user:, project:)
|
2017-01-18 13:13:01 +00:00
|
|
|
@exported_members = user.admin? ? 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
|
|
|
|
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-08-05 13:57:47 +00:00
|
|
|
if member['user']
|
|
|
|
old_user_id = member['user']['id']
|
|
|
|
existing_user = User.where(find_project_user_query(member)).first
|
|
|
|
hash[old_user_id] = existing_user.id if existing_user && add_team_member(member, existing_user)
|
|
|
|
else
|
|
|
|
add_team_member(member)
|
2016-06-01 16:03:51 +00:00
|
|
|
end
|
2016-08-05 13:57:47 +00:00
|
|
|
|
2016-06-01 16:03:51 +00:00
|
|
|
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|
|
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!
|
2017-01-31 15:32:26 +00:00
|
|
|
@project.project_members.each do |member|
|
|
|
|
member.destroy
|
|
|
|
end
|
|
|
|
|
2016-06-02 08:59:54 +00:00
|
|
|
ProjectMember.create!(user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true)
|
|
|
|
end
|
2016-06-01 16:03:51 +00:00
|
|
|
|
2016-08-05 13:57:47 +00:00
|
|
|
def add_team_member(member, existing_user = nil)
|
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-10-27 10:22:18 +00:00
|
|
|
parsed_hash(member).merge('source_id' => @project.id, 'importing' => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_hash(member)
|
|
|
|
Gitlab::ImportExport::AttributeCleaner.clean(relation_hash: member.deep_stringify_keys,
|
|
|
|
relation_class: ProjectMember)
|
2016-03-11 11:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_project_user_query(member)
|
2017-01-18 13:13:01 +00:00
|
|
|
user_arel[:email].eq(member['user']['email']).or(user_arel[:username].eq(member['user']['username']))
|
2016-03-11 11:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_arel
|
|
|
|
@user_arel ||= User.arel_table
|
2016-03-10 17:43:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|