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

95 lines
2.6 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
# 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']
2020-01-01 13:55:28 +05:30
existing_user = User.where(find_user_query(member)).first
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
2017-08-17 22:00:37 +05:30
def include?(old_author_id)
2019-09-30 21:07:59 +05:30
map.has_key?(old_author_id) && map[old_author_id] != default_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-01-01 13:55:28 +05:30
@importable.members.destroy_all # rubocop: disable DestroyAll
2017-08-17 22:00:37 +05:30
2020-01-01 13:55:28 +05:30
relation_class.create!(user: @user, access_level: relation_class::MAINTAINER, source_id: @importable.id, importing: true)
2019-10-12 21:52:04 +05:30
rescue => 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
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
2020-01-01 13:55:28 +05:30
relation_class.create(member_hash(member)).persisted?
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-01-01 13:55:28 +05:30
'access_level' => [member['access_level'], relation_class::MAINTAINER].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)
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
2020-01-01 13:55:28 +05:30
def relation_class
case @importable
when Project
ProjectMember
when Group
GroupMember
end
end
2016-06-22 15:30:34 +05:30
end
end
end