debian-mirror-gitlab/lib/gitlab/ci/config/entry/variable.rb

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

107 lines
3.3 KiB
Ruby
Raw Normal View History

2022-10-11 01:57:18 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module Entry
##
# Entry that represents a CI/CD variable.
#
class Variable < ::Gitlab::Config::Entry::Simplifiable
strategy :SimpleVariable, if: -> (config) { SimpleVariable.applies_to?(config) }
strategy :ComplexVariable, if: -> (config) { ComplexVariable.applies_to?(config) }
class SimpleVariable < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
class << self
def applies_to?(config)
Gitlab::Config::Entry::Validators::AlphanumericValidator.validate(config)
end
end
validations do
validates :key, alphanumeric: true
validates :config, alphanumeric: true
end
def value
@config.to_s
end
def value_with_data
{ value: @config.to_s }
end
2023-01-13 00:05:48 +05:30
def value_with_prefill_data
value_with_data
end
2022-10-11 01:57:18 +05:30
end
class ComplexVariable < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
2023-03-04 22:38:38 +05:30
include ::Gitlab::Config::Entry::Attributable
2022-10-11 01:57:18 +05:30
class << self
def applies_to?(config)
2023-03-04 22:38:38 +05:30
config.is_a?(Hash)
2022-10-11 01:57:18 +05:30
end
end
2023-03-04 22:38:38 +05:30
attributes :value, :description, :expand, :options, prefix: :config
2022-10-11 01:57:18 +05:30
validations do
validates :key, alphanumeric: true
2023-03-04 22:38:38 +05:30
validates :config_value, alphanumeric: true, allow_nil: true
validates :config_description, alphanumeric: true, allow_nil: true
2023-03-17 16:20:25 +05:30
validates :config_expand, boolean: true, allow_nil: true
2023-03-04 22:38:38 +05:30
validates :config_options, array_of_strings: true, allow_nil: true
2022-10-11 01:57:18 +05:30
validate do
allowed_value_data = Array(opt(:allowed_value_data))
if allowed_value_data.any?
extra_keys = config.keys - allowed_value_data
errors.add(:config, "uses invalid data keys: #{extra_keys.join(', ')}") if extra_keys.present?
else
errors.add(:config, "must be a string")
end
2023-03-04 22:38:38 +05:30
if config_options.present? && config_options.exclude?(config_value)
errors.add(:config, 'value must be present in options')
end
2022-10-11 01:57:18 +05:30
end
end
def value
2023-03-04 22:38:38 +05:30
# Needed since the `Entry::Node` provides `value` (which is current hash)
2022-10-11 01:57:18 +05:30
config_value.to_s
end
def value_with_data
2023-03-17 16:20:25 +05:30
{
value: config_value.to_s,
raw: (!config_expand if has_config_expand?)
}.compact
2023-01-13 00:05:48 +05:30
end
def value_with_prefill_data
value_with_data.merge(
2023-03-04 22:38:38 +05:30
description: config_description,
options: config_options
2023-01-13 00:05:48 +05:30
).compact
2022-10-11 01:57:18 +05:30
end
end
class UnknownStrategy < ::Gitlab::Config::Entry::Node
def errors
["variable definition must be either a string or a hash"]
end
end
end
end
end
end
end