summaryrefslogtreecommitdiff
path: root/lib/gitlab/phabricator_import/user_finder.rb
blob: 4b50431e0e016310b198598ec22b81df5fbbb302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module Gitlab
  module PhabricatorImport
    class UserFinder
      def initialize(project, phids)
        @project, @phids = project, phids
        @loaded_phids = Set.new
      end

      def find(phid)
        found_user = object_map.get_gitlab_model(phid) do
          find_user_for_phid(phid)
        end

        loaded_phids << phid

        found_user
      end

      private

      attr_reader :project, :phids, :loaded_phids

      def object_map
        @object_map ||= Gitlab::PhabricatorImport::Cache::Map.new(project)
      end

      def find_user_for_phid(phid)
        phabricator_user = phabricator_users.find { |u| u.phabricator_id == phid }
        return unless phabricator_user

        project.authorized_users.find_by_username(phabricator_user.username)
      end

      def phabricator_users
        @user_responses ||= client.users(users_to_request).flat_map(&:users)
      end

      def users_to_request
        phids - loaded_phids.to_a
      end

      def client
        @client ||=
          Gitlab::PhabricatorImport::Conduit::User
            .new(phabricator_url: project.import_data.data['phabricator_url'],
                 api_token: project.import_data.credentials[:api_token])
      end
    end
  end
end