2016-04-14 11:10:57 -04:00
|
|
|
module Gitlab
|
2016-03-10 12:43:57 -05:00
|
|
|
module ImportExport
|
|
|
|
class MembersMapper
|
2016-06-13 10:55:51 -04:00
|
|
|
attr_reader :missing_author_ids
|
2016-03-10 12:43:57 -05:00
|
|
|
|
2016-05-13 10:25:27 -04:00
|
|
|
def initialize(exported_members:, user:, project:)
|
2016-03-10 12:43:57 -05:00
|
|
|
@exported_members = exported_members
|
2016-03-11 06:55:50 -05:00
|
|
|
@user = user
|
2016-05-13 10:25:27 -04:00
|
|
|
@project = project
|
2016-06-13 10:55:51 -04:00
|
|
|
@missing_author_ids = []
|
2016-05-13 06:33:13 -04:00
|
|
|
|
2016-06-13 10:12:40 -04:00
|
|
|
# This needs to run first, as second call would be from #map
|
2016-05-13 13:51:07 -04:00
|
|
|
# which means project members already exist.
|
2016-06-02 04:59:54 -04:00
|
|
|
ensure_default_member!
|
|
|
|
end
|
2016-05-13 06:33:13 -04:00
|
|
|
|
2016-06-02 04:59:54 -04:00
|
|
|
def map
|
2016-06-01 12:03:51 -04:00
|
|
|
@map ||=
|
|
|
|
begin
|
2016-06-02 04:59:54 -04:00
|
|
|
@exported_members.inject(missing_keys_tracking_hash) do |hash, member|
|
2016-08-05 09:57:47 -04: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 12:03:51 -04:00
|
|
|
end
|
2016-08-05 09:57:47 -04:00
|
|
|
|
2016-06-01 12:03:51 -04:00
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
2016-03-11 06:55:50 -05:00
|
|
|
end
|
|
|
|
|
2016-06-13 10:12:40 -04:00
|
|
|
def default_user_id
|
|
|
|
@user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-02 04:59:54 -04:00
|
|
|
def missing_keys_tracking_hash
|
|
|
|
Hash.new do |_, key|
|
2016-06-13 10:55:51 -04:00
|
|
|
@missing_author_ids << key
|
2016-06-13 10:12:40 -04:00
|
|
|
default_user_id
|
2016-06-02 04:59:54 -04: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 12:03:51 -04:00
|
|
|
|
2016-08-05 09:57:47 -04:00
|
|
|
def add_team_member(member, existing_user = nil)
|
2016-03-11 06:55:50 -05:00
|
|
|
member['user'] = existing_user
|
2016-06-02 04:59:54 -04:00
|
|
|
|
2016-06-13 10:12:40 -04:00
|
|
|
ProjectMember.create(member_hash(member)).persisted?
|
2016-03-11 06:55:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def member_hash(member)
|
2016-10-27 06:22:18 -04: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 06:55:50 -05: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 12:43:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|