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

config.rb « import_export « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1a62e3b25a59bcfbfc29dca62a8cf8e776290d8 (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
# frozen_string_literal: true

module Gitlab
  module ImportExport
    class Config
      def initialize(config: Gitlab::ImportExport.config_file)
        @config = config
        @hash = parse_yaml
        @hash.deep_symbolize_keys!
        @ee_hash = @hash.delete(:ee) || {}

        @hash[:tree] = normalize_tree(@hash[:tree])
        @hash[:import_only_tree] = normalize_tree(@hash[:import_only_tree] || {})
        @ee_hash[:tree] = normalize_tree(@ee_hash[:tree] || {})
      end

      # Returns a Hash of the YAML file, including EE specific data if EE is
      # used.
      def to_h
        if merge_ee?
          deep_merge(@hash, @ee_hash)
        else
          @hash
        end
      end

      private

      def deep_merge(hash_a, hash_b)
        hash_a.deep_merge(hash_b) do |_, this_val, other_val|
          this_val.to_a + other_val.to_a
        end
      end

      def normalize_tree(item)
        case item
        when Array
          item.reduce({}) do |hash, subitem|
            hash.merge!(normalize_tree(subitem))
          end
        when Hash
          item.transform_values(&method(:normalize_tree))
        when Symbol
          { item => {} }
        else
          raise ArgumentError, "#{item} needs to be Array, Hash, Symbol or NilClass"
        end
      end

      def merge_ee?
        Gitlab.ee?
      end

      def parse_yaml
        YAML.safe_load_file(@config, aliases: true, permitted_classes: [Symbol])
      end
    end
  end
end