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

80 lines
2.2 KiB
Ruby
Raw Normal View History

2018-12-23 12:14:25 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class MembersMapper
def initialize(exported_members:, user:, project:)
2017-08-17 22:00:37 +05:30
@exported_members = user.admin? ? exported_members : []
2016-06-22 15:30:34 +05:30
@user = user
@project = project
# This needs to run first, as second call would be from #map
# which means project members already exist.
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']
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-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
2017-08-17 22:00:37 +05:30
def include?(old_author_id)
map.keys.include?(old_author_id) && map[old_author_id] != default_user_id
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!
2018-11-20 20:47:30 +05:30
@project.project_members.destroy_all # rubocop: disable DestroyAll
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
ProjectMember.create!(user: @user, access_level: ProjectMember::MAINTAINER, source_id: @project.id, importing: true)
2016-06-22 15:30:34 +05:30
end
2016-09-13 17:45:13 +05:30
def add_team_member(member, existing_user = nil)
2016-06-22 15:30:34 +05:30
member['user'] = existing_user
ProjectMember.create(member_hash(member)).persisted?
end
def member_hash(member)
2016-11-03 12:29:30 +05:30
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-06-22 15:30:34 +05:30
end
def find_project_user_query(member)
2017-08-17 22:00:37 +05:30
user_arel[:email].eq(member['user']['email']).or(user_arel[:username].eq(member['user']['username']))
2016-06-22 15:30:34 +05:30
end
def user_arel
@user_arel ||= User.arel_table
end
end
end
end