Skip to content
Snippets Groups Projects
Commit 1ac62de2 authored by Grzegorz Bizon's avatar Grzegorz Bizon
Browse files

Extract CI entry node validator and improve naming

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