summaryrefslogtreecommitdiff
path: root/app/services/bulk_import_service.rb
blob: bebf9153ce79780abbb8076607a3877d462b38c3 (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
# frozen_string_literal: true

# Entry point of the BulkImport feature.
# This service receives a Gitlab Instance connection params
# and a list of groups to be imported.
#
# Process topography:
#
#       sync      |   async
#                 |
#  User +--> P1 +----> Pn +---+
#                 |     ^     | Enqueue new job
#                 |     +-----+
#
# P1 (sync)
#
# - Create a BulkImport record
# - Create a BulkImport::Entity for each group to be imported
# - Enqueue a BulkImportWorker job (P2) to import the given groups (entities)
#
# Pn (async)
#
# - For each group to be imported (BulkImport::Entity.with_status(:created))
#   - Import the group data
#   - Create entities for each subgroup of the imported group
#   - Enqueue a BulkImportService job (Pn) to import the new entities (subgroups)
#
class BulkImportService
  attr_reader :current_user, :params, :credentials

  def initialize(current_user, params, credentials)
    @current_user = current_user
    @params = params
    @credentials = credentials
  end

  def execute
    bulk_import = create_bulk_import

    BulkImportWorker.perform_async(bulk_import.id)
  end

  private

  def create_bulk_import
    BulkImport.transaction do
      bulk_import = BulkImport.create!(user: current_user, source_type: 'gitlab')
      bulk_import.create_configuration!(credentials.slice(:url, :access_token))

      params.each do |entity|
        BulkImports::Entity.create!(
          bulk_import: bulk_import,
          source_type: entity[:source_type],
          source_full_path: entity[:source_full_path],
          destination_name: entity[:destination_name],
          destination_namespace: entity[:destination_namespace]
        )
      end

      bulk_import
    end
  end
end