2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Variables
|
|
|
|
module Helpers
|
|
|
|
class << self
|
|
|
|
def merge_variables(current_vars, new_vars)
|
2022-10-11 01:57:18 +05:30
|
|
|
return current_vars if new_vars.blank?
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
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)
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
(new_vars + current_vars).uniq { |var| var[:key] }
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def transform_to_array(vars)
|
|
|
|
return [] if vars.blank?
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
vars.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
|
2021-04-29 21:17:54 +05:30
|
|
|
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
|
2022-10-11 01:57:18 +05:30
|
|
|
when false then []
|
2021-04-29 21:17:54 +05:30
|
|
|
when Array then variables.select { |var| inheritance.include?(var[:key]) }
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|