Welcome to mirror list, hosted at ThFree Co, Russian Federation.

users_importer.rb « importers « bitbucket_server_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b0b059b397965dbb969c1ed19f789829e9b2d1a (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

module Gitlab
  module BitbucketServerImport
    module Importers
      class UsersImporter
        include Loggable
        include Gitlab::Import::UserFromMention

        BATCH_SIZE = 100

        def initialize(project)
          @project = project
          @project_id = project.id
        end

        attr_reader :project, :project_id

        def execute
          log_info(import_stage: 'import_users', message: 'starting')

          current = page_counter.current

          loop do
            log_info(
              import_stage: 'import_users',
              message: "importing page #{current} using batch size #{BATCH_SIZE}"
            )

            users = client.users(project_key, page_offset: current, limit: BATCH_SIZE).to_a

            break if users.empty?

            cache_users(users)

            current += 1
            page_counter.set(current)
          end

          page_counter.expire!

          log_info(import_stage: 'import_users', message: 'finished')
        end

        private

        def cache_users(users)
          users_hash = users.each_with_object({}) do |user, hash|
            cache_key = source_user_cache_key('bitbucket_server', project_id, user.username)
            hash[cache_key] = user.email
          end

          cache_multiple(users_hash)
        end

        def client
          @client ||= BitbucketServer::Client.new(project.import_data.credentials)
        end

        def project_key
          project.import_data.data['project_key']
        end

        def page_counter
          @page_counter ||= Gitlab::Import::PageCounter.new(project, :users, 'bitbucket-server-importer')
        end
      end
    end
  end
end