diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-08-17 11:07:38 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-09-04 15:17:01 +0300 |
commit | ee9fd5c226fcf83b8a4a0bf2814c9d0efc530659 (patch) | |
tree | 8de152254815e2762e66b0dd2989324b300e5b15 /lib | |
parent | 2f05e34b3c909aaf0d31ab6e10809bf3d0fc6626 (diff) |
Improve extendable CI/CD config hash collection
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/ci/config/extendable/collection.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/ci/config/extendable/entry.rb | 26 |
2 files changed, 27 insertions, 16 deletions
diff --git a/lib/gitlab/ci/config/extendable/collection.rb b/lib/gitlab/ci/config/extendable/collection.rb index e59884439a2..cdc8eb6614d 100644 --- a/lib/gitlab/ci/config/extendable/collection.rb +++ b/lib/gitlab/ci/config/extendable/collection.rb @@ -6,26 +6,23 @@ module Gitlab include Enumerable ExtensionError = Class.new(StandardError) + InvalidExtensionError = Class.new(ExtensionError) CircularDependencyError = Class.new(ExtensionError) def initialize(hash) - @hash = hash + @hash = hash.dup + + each { |entry| entry.extend! if entry.extensible? } end def each - @hash.each_pair do |key, value| - next unless value.key?(:extends) - + @hash.each_key do |key| yield Extendable::Entry.new(key, @hash) end end - def extend! - each do |entry| - raise ExtensionError unless entry.valid? - - entry.extend! - end + def to_hash + @hash.to_h end end end diff --git a/lib/gitlab/ci/config/extendable/entry.rb b/lib/gitlab/ci/config/extendable/entry.rb index 96b0bd1a2ce..d5eef647b3e 100644 --- a/lib/gitlab/ci/config/extendable/entry.rb +++ b/lib/gitlab/ci/config/extendable/entry.rb @@ -19,9 +19,9 @@ module Gitlab @value ||= @context.fetch(@key) end - def base + def base_hash Extendable::Entry - .new(extends, @context, self) + .new(extends_key, @context, self) .extend! end @@ -29,8 +29,8 @@ module Gitlab value.key?(:extends) end - def extends - value.fetch(:extends).to_sym + def extends_key + value.fetch(:extends).to_s.to_sym end def path @@ -38,16 +38,30 @@ module Gitlab end def extend! - if path.count(key) > 1 + if circular_dependency? raise Extendable::Collection::CircularDependencyError end + if invalid_extends_key? + raise Extendable::Collection::InvalidExtensionError + end + if extensible? - @context[key] = base.deep_merge(value) + @context[key] = base_hash.deep_merge(value) else value end end + + private + + def circular_dependency? + path.count(key) > 1 + end + + def invalid_extends_key? + !@context.key?(key) + end end end end |