diff options
Diffstat (limited to 'lib/gitlab/ci/variables')
-rw-r--r-- | lib/gitlab/ci/variables/collection.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/ci/variables/collection/item.rb | 13 |
2 files changed, 27 insertions, 6 deletions
diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb index e2a8af9c26b..ef9ba1b73c7 100644 --- a/lib/gitlab/ci/variables/collection.rb +++ b/lib/gitlab/ci/variables/collection.rb @@ -24,6 +24,10 @@ module Gitlab self end + def compact + Collection.new(select { |variable| !variable.value.nil? }) + end + def concat(resources) return self if resources.nil? @@ -64,11 +68,19 @@ module Gitlab end def expand_value(value, keep_undefined: false) - value.gsub(ExpandVariables::VARIABLES_REGEXP) do + value.gsub(Item::VARIABLES_REGEXP) do match = Regexp.last_match - result = @variables_by_key[match[1] || match[2]]&.value - result ||= match[0] if keep_undefined - result + if match[:key] + # we matched variable + if variable = @variables_by_key[match[:key]] + variable.value + elsif keep_undefined + match[0] + end + else + # we escape sequence + match[0] + end end end diff --git a/lib/gitlab/ci/variables/collection/item.rb b/lib/gitlab/ci/variables/collection/item.rb index 77da2c4cb91..0217e6129ca 100644 --- a/lib/gitlab/ci/variables/collection/item.rb +++ b/lib/gitlab/ci/variables/collection/item.rb @@ -7,6 +7,9 @@ module Gitlab class Item include Gitlab::Utils::StrongMemoize + VARIABLES_REGEXP = /\$\$|%%|\$(?<key>[a-zA-Z_][a-zA-Z0-9_]*)|\${\g<key>?}|%\g<key>%/.freeze.freeze + VARIABLE_REF_CHARS = %w[$ %].freeze + def initialize(key:, value:, public: true, file: false, masked: false, raw: false) raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless value.is_a?(String) || value.nil? @@ -34,9 +37,9 @@ module Gitlab strong_memoize(:depends_on) do next if raw - next unless ExpandVariables.possible_var_reference?(value) + next unless self.class.possible_var_reference?(value) - value.scan(ExpandVariables::VARIABLES_REGEXP).map(&:first) + value.scan(VARIABLES_REGEXP).filter_map(&:last) end end @@ -64,6 +67,12 @@ module Gitlab end end + def self.possible_var_reference?(value) + return unless value + + VARIABLE_REF_CHARS.any? { |symbol| value.include?(symbol) } + end + def to_s return to_runner_variable.to_s unless depends_on |