diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index 37936fc82421d21dd0818021c822aa48f3fcb1e2..88403a9de1e81167db63dff1acf5b3621b0c114c 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -26,7 +26,9 @@ module Gitlab private def create_node(key, factory) - factory.with(value: @config[key], key: key, parent: self) + factory + .value(config[key]) + .with(key: key, parent: self, global: global) factory.create! end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index 97e17b89c40b810c1089645b16eb695f8df3af03..e8b0160edc13a8766790856ba50001fa2be60aa3 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -8,13 +8,17 @@ module Gitlab class Entry class InvalidError < StandardError; end - attr_reader :config - attr_accessor :key, :parent, :description + attr_reader :config, :attributes + attr_accessor :key, :parent, :global, :description - def initialize(config) + def initialize(config, **attributes) @config = config @nodes = {} + (@attributes = attributes).each do |attribute, value| + public_send("#{attribute}=", value) + end + @validator = self.class.validator.new(self) @validator.validate end @@ -68,10 +72,6 @@ module Gitlab true end - def attributes - { key: @key, parent: @parent, description: @description } - end - def self.default end diff --git a/lib/gitlab/ci/config/node/factory.rb b/lib/gitlab/ci/config/node/factory.rb index b1457b81a45b9d0b8648c9ec9333214342b5370a..3f2cdf436e3a2f44f31ad8e479d176773f046767 100644 --- a/lib/gitlab/ci/config/node/factory.rb +++ b/lib/gitlab/ci/config/node/factory.rb @@ -13,38 +13,29 @@ module Gitlab @attributes = {} end + def value(value) + @value = value + self + end + def with(attributes) @attributes.merge!(attributes) self end def create! - raise InvalidFactory unless @attributes.has_key?(:value) + raise InvalidFactory unless defined?(@value) ## # We assume that unspecified entry is undefined. # See issue #18775. # - if @attributes[:value].nil? - fabricate(Node::Undefined, @node) + if @value.nil? + Node::Undefined.new(@node, @attributes) else - fabricate(@node, @attributes[:value]) + @node.new(@value, @attributes) end end - - def self.fabricate(node, value, **attributes) - node.new(value).tap do |entry| - entry.key = attributes[:key] - entry.parent = attributes[:parent] - entry.description = attributes[:description] - end - end - - private - - def fabricate(node, value) - self.class.fabricate(node, value, @attributes) - end end end end diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb index 64d8e39093decf3886fb209b053ff205eac513a2..dffa3326630b1d2d2e9595bd27f9da2769bfdbb2 100644 --- a/lib/gitlab/ci/config/node/global.rb +++ b/lib/gitlab/ci/config/node/global.rb @@ -51,6 +51,10 @@ module Gitlab def stages stages_defined? ? stages_value : types_value end + + def global + self + end end end end diff --git a/lib/gitlab/ci/config/node/jobs.rb b/lib/gitlab/ci/config/node/jobs.rb index 71893ba1d8879eb713802623c18947978fe76bfc..d7d61ade36d32852e5b5f47d062f8b831c3c2887 100644 --- a/lib/gitlab/ci/config/node/jobs.rb +++ b/lib/gitlab/ci/config/node/jobs.rb @@ -30,13 +30,13 @@ module Gitlab private def create_node(key, value) - node = key.to_s.start_with?('.') ? Node::HiddenJob : Node::Job + job_node = key.to_s.start_with?('.') ? Node::HiddenJob : Node::Job - attributes = { key: key, - parent: self, - description: "#{key} job definition." } + job_attributes = { key: key, + parent: self, + description: "#{key} job definition." } - Node::Factory.fabricate(node, value, attributes) + job_node.new(value, attributes.merge(job_attributes)) end end end diff --git a/lib/gitlab/ci/config/node/undefined.rb b/lib/gitlab/ci/config/node/undefined.rb index 7b18e3646750d747ed1245839161f63808008ef3..fedb9d020be017e6dd69d53a3cad1b779e03753e 100644 --- a/lib/gitlab/ci/config/node/undefined.rb +++ b/lib/gitlab/ci/config/node/undefined.rb @@ -19,7 +19,7 @@ module Gitlab validates :config, type: Class end - def initialize(node) + def initialize(node, **attributes) super @strategy = create_strategy(node, node.default) end @@ -34,9 +34,7 @@ module Gitlab if default.nil? Undefined::NullStrategy.new else - entry = Node::Factory - .fabricate(node, default, attributes) - + entry = node.new(default, attributes) Undefined::DefaultStrategy.new(entry) end end diff --git a/spec/lib/gitlab/ci/config/node/factory_spec.rb b/spec/lib/gitlab/ci/config/node/factory_spec.rb index 5b856d4498940a00269f7117f9587c1598592c7d..c912b1b204448e8d18d6894cb456e50fc19598ae 100644 --- a/spec/lib/gitlab/ci/config/node/factory_spec.rb +++ b/spec/lib/gitlab/ci/config/node/factory_spec.rb @@ -5,24 +5,10 @@ describe Gitlab::Ci::Config::Node::Factory do let(:factory) { described_class.new(entry_class) } let(:entry_class) { Gitlab::Ci::Config::Node::Script } - describe '.fabricate' do - it 'fabricates entry with attributes set' do - fabricated = described_class - .fabricate(entry_class, ['ls'], - parent: true, key: :test) - - expect(fabricated.parent).to be true - expect(fabricated.key).to eq :test - expect(fabricated.value).to eq ['ls'] - expect(fabricated.attributes) - .to eq(parent: true, key: :test, description: nil) - end - end - context 'when setting up a value' do it 'creates entry with valid value' do entry = factory - .with(value: ['ls', 'pwd']) + .value(['ls', 'pwd']) .create! expect(entry.value).to eq ['ls', 'pwd'] @@ -31,7 +17,7 @@ describe Gitlab::Ci::Config::Node::Factory do context 'when setting description' do it 'creates entry with description' do entry = factory - .with(value: ['ls', 'pwd']) + .value(['ls', 'pwd']) .with(description: 'test description') .create! @@ -43,7 +29,8 @@ describe Gitlab::Ci::Config::Node::Factory do context 'when setting key' do it 'creates entry with custom key' do entry = factory - .with(value: ['ls', 'pwd'], key: 'test key') + .value(['ls', 'pwd']) + .with(key: 'test key') .create! expect(entry.key).to eq 'test key' @@ -55,7 +42,8 @@ describe Gitlab::Ci::Config::Node::Factory do it 'creates entry with valid parent' do entry = factory - .with(value: 'ls', parent: parent) + .value('ls') + .with(parent: parent) .create! expect(entry.parent).to eq parent @@ -74,7 +62,7 @@ describe Gitlab::Ci::Config::Node::Factory do context 'when creating entry with nil value' do it 'creates an undefined entry' do entry = factory - .with(value: nil) + .value(nil) .create! expect(entry).to be_an_instance_of Gitlab::Ci::Config::Node::Undefined