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

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

module Gitlab
  module Ci
    module Variables
      module Helpers
        class << self
          def merge_variables(current_vars, new_vars)
            return current_vars if new_vars.blank?

            current_vars = transform_to_array(current_vars) if current_vars.is_a?(Hash)
            new_vars = transform_to_array(new_vars) if new_vars.is_a?(Hash)

            (new_vars + current_vars).uniq { |var| var[:key] }
          end

          def transform_to_array(vars)
            vars.to_h.map do |key, data|
              if data.is_a?(Hash)
                { key: key.to_s, **data.except(:key) }
              else
                { key: key.to_s, value: data }
              end
            end
          end

          def inherit_yaml_variables(from:, to:, inheritance:)
            merge_variables(apply_inheritance(from, inheritance), to)
          end

          private

          def apply_inheritance(variables, inheritance)
            case inheritance
            when true then variables
            when false then []
            when Array then variables.select { |var| inheritance.include?(var[:key]) }
            end
          end
        end
      end
    end
  end
end