2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Config
|
|
|
|
module Entry
|
|
|
|
##
|
|
|
|
# This mixin is responsible for adding DSL, which purpose is to
|
|
|
|
# simplifly process of adding child nodes.
|
|
|
|
#
|
|
|
|
# This can be used only if parent node is a configuration entry that
|
|
|
|
# holds a hash as a configuration value, for example:
|
|
|
|
#
|
|
|
|
# job:
|
|
|
|
# script: ...
|
|
|
|
# artifacts: ...
|
|
|
|
#
|
|
|
|
module Configurable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
include Validatable
|
|
|
|
|
|
|
|
validations do
|
2019-05-18 00:54:41 +05:30
|
|
|
validates :config, type: Hash, unless: :skip_config_hash_validation?
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def compose!(deps = nil)
|
|
|
|
return unless valid?
|
|
|
|
|
|
|
|
self.class.nodes.each do |key, factory|
|
2019-05-18 00:54:41 +05:30
|
|
|
# If we override the config type validation
|
|
|
|
# we can end with different config types like String
|
|
|
|
next unless config.is_a?(Hash)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
factory
|
|
|
|
.value(config[key])
|
|
|
|
.with(key: key, parent: self)
|
|
|
|
|
|
|
|
entries[key] = factory.create!
|
|
|
|
end
|
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
|
|
|
|
entries.each_value do |entry|
|
|
|
|
entry.compose!(deps)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
def skip_config_hash_validation?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
class_methods do
|
|
|
|
def nodes
|
|
|
|
Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def entry(key, entry, metadata)
|
|
|
|
factory = ::Gitlab::Config::Entry::Factory.new(entry)
|
|
|
|
.with(description: metadata[:description])
|
2019-03-02 22:35:43 +05:30
|
|
|
.with(default: metadata[:default])
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
(@nodes ||= {}).merge!(key.to_sym => factory)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
def helpers(*nodes)
|
|
|
|
nodes.each do |symbol|
|
|
|
|
define_method("#{symbol}_defined?") do
|
|
|
|
entries[symbol]&.specified?
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method("#{symbol}_value") do
|
|
|
|
return unless entries[symbol] && entries[symbol].valid?
|
|
|
|
|
|
|
|
entries[symbol].value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|