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

project_attributes_pipeline.rb « pipelines « projects « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d742225ff7373e3f8f5e7c6483e1b07243a3668 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

module BulkImports
  module Projects
    module Pipelines
      class ProjectAttributesPipeline
        include Pipeline

        transformer ::BulkImports::Common::Transformers::ProhibitedAttributesTransformer

        def extract(context)
          download_service(tmp_dir, context).execute
          decompression_service(tmp_dir).execute
          project_attributes = json_decode(json_attributes)

          BulkImports::Pipeline::ExtractedData.new(data: project_attributes)
        end

        def transform(_, data)
          subrelations = config.portable_relations_tree.keys.map(&:to_s)

          Gitlab::ImportExport::AttributeCleaner.clean(
            relation_hash: data,
            relation_class: Project,
            excluded_keys: config.relation_excluded_keys(:project)
          ).except(*subrelations)
        end

        def load(_, data)
          portable.assign_attributes(data)
          portable.reconcile_shared_runners_setting!
          portable.drop_visibility_level!
          portable.save!
        end

        def after_run(_)
          FileUtils.remove_entry(tmp_dir)
        end

        def json_attributes
          @json_attributes ||= File.read(File.join(tmp_dir, filename))
        end

        private

        def tmp_dir
          @tmp_dir ||= Dir.mktmpdir
        end

        def config
          @config ||= BulkImports::FileTransfer.config_for(portable)
        end

        def download_service(tmp_dir, context)
          @download_service ||= BulkImports::FileDownloadService.new(
            configuration: context.configuration,
            relative_url: context.entity.relation_download_url_path(BulkImports::FileTransfer::BaseConfig::SELF_RELATION),
            dir: tmp_dir,
            filename: compressed_filename
          )
        end

        def decompression_service(tmp_dir)
          @decompression_service ||= BulkImports::FileDecompressionService.new(dir: tmp_dir, filename: compressed_filename)
        end

        def compressed_filename
          "#{filename}.gz"
        end

        def filename
          "#{BulkImports::FileTransfer::BaseConfig::SELF_RELATION}.json"
        end

        def json_decode(string)
          Gitlab::Json.parse(string)
        rescue JSON::ParserError => e
          Gitlab::ErrorTracking.log_exception(e)

          raise BulkImports::Error, 'Incorrect JSON format'
        end
      end
    end
  end
end