summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/variables/helpers.rb
blob: e2a54f90ecb97e48e7f42f2d792a8c26728ce583 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Variables
      module Helpers
        class << self
          def merge_variables(current_vars, new_vars)
            current_vars = transform_from_yaml_variables(current_vars)
            new_vars = transform_from_yaml_variables(new_vars)

            transform_to_yaml_variables(
              current_vars.merge(new_vars)
            )
          end

          def transform_to_yaml_variables(vars)
            vars.to_h.map do |key, value|
              { key: key.to_s, value: value, public: true }
            end
          end

          def transform_from_yaml_variables(vars)
            return vars.stringify_keys if vars.is_a?(Hash)

            vars.to_a.map { |var| [var[:key].to_s, var[:value]] }.to_h
          end
        end
      end
    end
  end
end