debian-mirror-gitlab/lib/gitlab/ci/variables/collection/item.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module Ci
module Variables
class Collection
class Item
2021-04-17 20:07:23 +05:30
include Gitlab::Utils::StrongMemoize
2021-09-04 01:27:46 +05:30
VARIABLES_REGEXP = /\$\$|%%|\$(?<key>[a-zA-Z_][a-zA-Z0-9_]*)|\${\g<key>?}|%\g<key>%/.freeze.freeze
VARIABLE_REF_CHARS = %w[$ %].freeze
2021-04-17 20:07:23 +05:30
def initialize(key:, value:, public: true, file: false, masked: false, raw: false)
2019-02-15 15:39:39 +05:30
raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless
2018-11-08 19:23:39 +05:30
value.is_a?(String) || value.nil?
2021-04-17 20:07:23 +05:30
@variable = { key: key, value: value, public: public, file: file, masked: masked, raw: raw }
end
def value
@variable.fetch(:value)
end
def raw
@variable.fetch(:raw)
2018-05-09 12:01:36 +05:30
end
2022-08-27 11:52:29 +05:30
def file?
@variable.fetch(:file)
end
2018-05-09 12:01:36 +05:30
def [](key)
@variable.fetch(key)
end
def ==(other)
to_runner_variable == self.class.fabricate(other).to_runner_variable
end
2021-04-17 20:07:23 +05:30
def depends_on
strong_memoize(:depends_on) do
next if raw
2021-09-04 01:27:46 +05:30
next unless self.class.possible_var_reference?(value)
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
value.scan(VARIABLES_REGEXP).filter_map(&:last)
2021-04-17 20:07:23 +05:30
end
end
2018-05-09 12:01:36 +05:30
##
# If `file: true` has been provided we expose it, otherwise we
# don't expose `file` attribute at all (stems from what the runner
# expects).
#
def to_runner_variable
@variable.reject do |hash_key, hash_value|
2021-04-17 20:07:23 +05:30
(hash_key == :file || hash_key == :raw) && hash_value == false
2018-05-09 12:01:36 +05:30
end
end
def self.fabricate(resource)
case resource
when Hash
2021-01-03 14:25:43 +05:30
self.new(**resource.symbolize_keys)
2020-04-22 19:07:51 +05:30
when ::Ci::HasVariable
2021-01-03 14:25:43 +05:30
self.new(**resource.to_runner_variable)
2018-05-09 12:01:36 +05:30
when self
resource.dup
else
raise ArgumentError, "Unknown `#{resource.class}` variable resource!"
end
end
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
def self.possible_var_reference?(value)
return unless value
VARIABLE_REF_CHARS.any? { |symbol| value.include?(symbol) }
end
2021-04-17 20:07:23 +05:30
def to_s
return to_runner_variable.to_s unless depends_on
"#{to_runner_variable}, depends_on=#{depends_on}"
end
2018-05-09 12:01:36 +05:30
end
end
end
end
end