Commit 1ac62de2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extract CI entry node validator and improve naming

parent 159aed1c
......@@ -15,6 +15,7 @@ module Gitlab
@global = Node::Global.new(@config)
@global.process!
@global.validate!
end
def valid?
......
......@@ -25,7 +25,7 @@ module Gitlab
private
def create_node(key, factory)
def create(key, factory)
factory
.value(config[key])
.with(key: key, parent: self, global: global)
......@@ -50,12 +50,12 @@ module Gitlab
def helpers(*nodes)
nodes.each do |symbol|
define_method("#{symbol}_defined?") do
@nodes[symbol].try(:defined?)
@entries[symbol].try(:defined?)
end
define_method("#{symbol}_value") do
raise Entry::InvalidError unless valid?
@nodes[symbol].try(:value)
@entries[symbol].try(:value)
end
alias_method symbol.to_sym, "#{symbol}_value".to_sym
......
......@@ -13,7 +13,7 @@ module Gitlab
def initialize(config, **attributes)
@config = config
@nodes = {}
@entries = {}
(@attributes = attributes).each do |attribute, value|
public_send("#{attribute}=", value)
......@@ -24,8 +24,18 @@ module Gitlab
end
def process!
compose! unless leaf?
@validator.validate(:processed) if valid?
return unless valid?
nodes.each do |key, essence|
@entries[key] = create(key, essence)
end
@entries.each_value(&:process!)
end
def validate!
@validator.validate(:after)
@entries.each_value(&:validate!)
end
def leaf?
......@@ -37,7 +47,7 @@ module Gitlab
end
def descendants
@nodes.values
@entries.values
end
def ancestors
......@@ -49,18 +59,18 @@ module Gitlab
end
def errors
@validator.messages + @nodes.values.flat_map(&:errors)
@validator.messages + @entries.values.flat_map(&:errors)
end
def value
if leaf?
@config
else
meaningful = @nodes.select do |_key, value|
meaningful = @entries.select do |_key, value|
value.defined? && value.relevant?
end
Hash[meaningful.map { |key, node| [key, node.value] }]
Hash[meaningful.map { |key, entry| [key, entry.value] }]
end
end
......@@ -85,17 +95,7 @@ module Gitlab
private
def compose!
return unless valid?
nodes.each do |key, essence|
@nodes[key] = create_node(key, essence)
end
@nodes.each_value(&:process!)
end
def create_node(key, essence)
def create(entry, essence)
raise NotImplementedError
end
end
......
......@@ -10,7 +10,7 @@ module Gitlab
validations do
validates :config, type: Hash
validate :jobs_presence, on: :processed
validate :jobs_presence, on: :after
def jobs_presence
unless relevant?
......@@ -24,12 +24,12 @@ module Gitlab
end
def relevant?
@nodes.values.any?(&:relevant?)
@entries.values.any?(&:relevant?)
end
private
def create_node(name, config)
def create(name, config)
job_node(name).new(config, job_attributes(name))
end
......
......@@ -35,7 +35,10 @@ describe Gitlab::Ci::Config::Node::Jobs do
end
context 'when processed' do
before { entry.process! }
before do
entry.process!
entry.validate!
end
it 'returns error about no visible jobs defined' do
expect(entry.errors)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment