members_mapper.rb 2.05 KB
Newer Older
1
module Gitlab
James Lopez's avatar
James Lopez committed
2
3
  module ImportExport
    class MembersMapper
4
      def initialize(exported_members:, user:, project:)
5
        @exported_members = user.admin? ? exported_members : []
James Lopez's avatar
James Lopez committed
6
        @user = user
7
        @project = project
8

9
        # This needs to run first, as second call would be from #map
James Lopez's avatar
James Lopez committed
10
        # which means project members already exist.
James Lopez's avatar
James Lopez committed
11
12
        ensure_default_member!
      end
13

James Lopez's avatar
James Lopez committed
14
      def map
15
16
        @map ||=
          begin
James Lopez's avatar
James Lopez committed
17
            @exported_members.inject(missing_keys_tracking_hash) do |hash, member|
18
19
20
21
22
23
              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)
24
              end
25

26
27
28
              hash
            end
          end
James Lopez's avatar
James Lopez committed
29
30
      end

31
32
33
34
35
36
      def default_user_id
        @user.id
      end

      private

James Lopez's avatar
James Lopez committed
37
38
      def missing_keys_tracking_hash
        Hash.new do |_, key|
39
          default_user_id
James Lopez's avatar
James Lopez committed
40
41
42
43
        end
      end

      def ensure_default_member!
James Lopez's avatar
James Lopez committed
44
        @project.project_members.destroy_all
45

James Lopez's avatar
James Lopez committed
46
47
        ProjectMember.create!(user: @user, access_level: ProjectMember::MASTER, source_id: @project.id, importing: true)
      end
48

49
      def add_team_member(member, existing_user = nil)
James Lopez's avatar
James Lopez committed
50
        member['user'] = existing_user
James Lopez's avatar
James Lopez committed
51

52
        ProjectMember.create(member_hash(member)).persisted?
James Lopez's avatar
James Lopez committed
53
54
55
      end

      def member_hash(member)
56
57
58
59
60
61
        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)
James Lopez's avatar
James Lopez committed
62
63
64
      end

      def find_project_user_query(member)
65
        user_arel[:email].eq(member['user']['email']).or(user_arel[:username].eq(member['user']['username']))
James Lopez's avatar
James Lopez committed
66
67
68
69
      end

      def user_arel
        @user_arel ||= User.arel_table
James Lopez's avatar
James Lopez committed
70
71
72
73
      end
    end
  end
end