diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /lib/gitlab/config | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'lib/gitlab/config')
-rw-r--r-- | lib/gitlab/config/entry/composable_array.rb | 58 | ||||
-rw-r--r-- | lib/gitlab/config/entry/composable_hash.rb | 47 | ||||
-rw-r--r-- | lib/gitlab/config/entry/factory.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/config/entry/legacy_validation_helpers.rb | 14 | ||||
-rw-r--r-- | lib/gitlab/config/entry/simplifiable.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/config/entry/validators.rb | 8 |
6 files changed, 129 insertions, 2 deletions
diff --git a/lib/gitlab/config/entry/composable_array.rb b/lib/gitlab/config/entry/composable_array.rb new file mode 100644 index 00000000000..e7ad259e826 --- /dev/null +++ b/lib/gitlab/config/entry/composable_array.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module Gitlab + module Config + module Entry + ## + # Entry that represents a composable array definition + # + class ComposableArray < ::Gitlab::Config::Entry::Node + include ::Gitlab::Config::Entry::Validatable + include Gitlab::Utils::StrongMemoize + + # TODO: Refactor `Validatable` code so that validations can apply to a child class + # See: https://gitlab.com/gitlab-org/gitlab/-/issues/263231 + validations do + validates :config, type: Array + end + + def compose!(deps = nil) + super do + @entries = Array(@entries) + + # TODO: Isolate handling for a hash via: `[@config].flatten` to the `Needs` entry + # See: https://gitlab.com/gitlab-org/gitlab/-/issues/264376 + [@config].flatten.each_with_index do |value, index| + raise ArgumentError, 'Missing Composable class' unless composable_class + + composable_class_name = composable_class.name.demodulize.underscore + + @entries << ::Gitlab::Config::Entry::Factory.new(composable_class) + .value(value) + .with(key: composable_class_name, parent: self, description: "#{composable_class_name} definition") # rubocop:disable CodeReuse/ActiveRecord + .create! + end + + @entries.each do |entry| + entry.compose!(deps) + end + end + end + + def value + @entries.map(&:value) + end + + def descendants + @entries + end + + def composable_class + strong_memoize(:composable_class) do + opt(:composable_class) + end + end + end + end + end +end diff --git a/lib/gitlab/config/entry/composable_hash.rb b/lib/gitlab/config/entry/composable_hash.rb new file mode 100644 index 00000000000..9531b7e56fd --- /dev/null +++ b/lib/gitlab/config/entry/composable_hash.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Gitlab + module Config + module Entry + ## + # Entry that represents a composable hash definition + # Where each hash key can be any value written by the user + # + class ComposableHash < ::Gitlab::Config::Entry::Node + include ::Gitlab::Config::Entry::Validatable + + # TODO: Refactor `Validatable` code so that validations can apply to a child class + # See: https://gitlab.com/gitlab-org/gitlab/-/issues/263231 + validations do + validates :config, type: Hash + end + + def compose!(deps = nil) + super do + @config.each do |name, config| + entry_class = composable_class(name, config) + raise ArgumentError, 'Missing Composable class' unless entry_class + + entry_class_name = entry_class.name.demodulize.underscore + + factory = ::Gitlab::Config::Entry::Factory.new(entry_class) + .value(config || {}) + .with(key: name, parent: self, description: "#{name} #{entry_class_name} definition") # rubocop:disable CodeReuse/ActiveRecord + .metadata(name: name) + + @entries[name] = factory.create! + end + + @entries.each_value do |entry| + entry.compose!(deps) + end + end + end + + def composable_class(name, config) + opt(:composable_class) + end + end + end + end +end diff --git a/lib/gitlab/config/entry/factory.rb b/lib/gitlab/config/entry/factory.rb index 7c5ffaa7621..f76c98f7cbf 100644 --- a/lib/gitlab/config/entry/factory.rb +++ b/lib/gitlab/config/entry/factory.rb @@ -79,7 +79,7 @@ module Gitlab end def fabricate(entry_class, value = nil) - entry_class.new(value, @metadata) do |node| + entry_class.new(value, **@metadata) do |node| node.key = @attributes[:key] node.parent = @attributes[:parent] node.default = @attributes[:default] diff --git a/lib/gitlab/config/entry/legacy_validation_helpers.rb b/lib/gitlab/config/entry/legacy_validation_helpers.rb index 415f6f77214..be7d26fed4e 100644 --- a/lib/gitlab/config/entry/legacy_validation_helpers.rb +++ b/lib/gitlab/config/entry/legacy_validation_helpers.rb @@ -50,6 +50,12 @@ module Gitlab variables.values.flatten(1).all?(&method(:validate_alphanumeric)) end + def validate_string_or_hash_value_variables(variables, allowed_value_data) + variables.is_a?(Hash) && + variables.keys.all?(&method(:validate_alphanumeric)) && + variables.values.all? { |value| validate_string_or_hash_value_variable(value, allowed_value_data) } + end + def validate_alphanumeric(value) validate_string(value) || validate_integer(value) end @@ -62,6 +68,14 @@ module Gitlab value.is_a?(String) || value.is_a?(Symbol) end + def validate_string_or_hash_value_variable(value, allowed_value_data) + if value.is_a?(Hash) + (value.keys - allowed_value_data).empty? && value.values.all?(&method(:validate_alphanumeric)) + else + validate_alphanumeric(value) + end + end + def validate_regexp(value) Gitlab::UntrustedRegexp::RubySyntax.valid?(value) end diff --git a/lib/gitlab/config/entry/simplifiable.rb b/lib/gitlab/config/entry/simplifiable.rb index 315f1947e2c..ee28891a174 100644 --- a/lib/gitlab/config/entry/simplifiable.rb +++ b/lib/gitlab/config/entry/simplifiable.rb @@ -19,7 +19,7 @@ module Gitlab entry = self.class.entry_class(strategy) - @subject = entry.new(config, metadata, &blk) + @subject = entry.new(config, **metadata, &blk) super(@subject) end diff --git a/lib/gitlab/config/entry/validators.rb b/lib/gitlab/config/entry/validators.rb index a7ec98ace6e..2a386657e0b 100644 --- a/lib/gitlab/config/entry/validators.rb +++ b/lib/gitlab/config/entry/validators.rb @@ -274,6 +274,8 @@ module Gitlab def validate_each(record, attribute, value) if options[:array_values] validate_key_array_values(record, attribute, value) + elsif options[:allowed_value_data] + validate_key_hash_values(record, attribute, value, options[:allowed_value_data]) else validate_key_values(record, attribute, value) end @@ -290,6 +292,12 @@ module Gitlab record.errors.add(attribute, 'should be a hash of key value pairs, value can be an array') end end + + def validate_key_hash_values(record, attribute, value, allowed_value_data) + unless validate_string_or_hash_value_variables(value, allowed_value_data) + record.errors.add(attribute, 'should be a hash of key value pairs, value can be a hash') + end + end end class ExpressionValidator < ActiveModel::EachValidator |