Commit 41bcbdd8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add metadata to new CI config and expose job name

parent 615c9730
...@@ -82,7 +82,7 @@ module Ci ...@@ -82,7 +82,7 @@ module Ci
stage: job[:stage], stage: job[:stage],
commands: [job[:before_script] || @before_script, job[:script]].flatten.compact.join("\n"), commands: [job[:before_script] || @before_script, job[:script]].flatten.compact.join("\n"),
tag_list: job[:tags] || [], tag_list: job[:tags] || [],
name: name, name: job[:name],
only: job[:only], only: job[:only],
except: job[:except], except: job[:except],
allow_failure: job[:allow_failure] || false, allow_failure: job[:allow_failure] || false,
...@@ -113,7 +113,7 @@ module Ci ...@@ -113,7 +113,7 @@ module Ci
def validate_job_keys!(name, job) def validate_job_keys!(name, job)
job.keys.each do |key| job.keys.each do |key|
unless ALLOWED_JOB_KEYS.include? key unless (ALLOWED_JOB_KEYS + %i[name]).include? key
raise ValidationError, "#{name} job: unknown parameter #{key}" raise ValidationError, "#{name} job: unknown parameter #{key}"
end end
end end
......
...@@ -28,8 +28,7 @@ module Gitlab ...@@ -28,8 +28,7 @@ module Gitlab
def create(key, factory) def create(key, factory)
factory factory
.value(@config[key]) .value(@config[key])
.parent(self) .with(key: key, parent: self)
.with(key: key)
factory.create! factory.create!
end end
......
...@@ -11,13 +11,10 @@ module Gitlab ...@@ -11,13 +11,10 @@ module Gitlab
attr_reader :config, :attributes attr_reader :config, :attributes
attr_accessor :key, :parent, :description attr_accessor :key, :parent, :description
def initialize(config, **attributes) def initialize(config, **metadata)
@config = config @config = config
@entries = {} @entries = {}
@metadata = metadata
(@attributes = attributes).each do |attribute, value|
public_send("#{attribute}=", value)
end
@validator = self.class.validator.new(self) @validator = self.class.validator.new(self)
@validator.validate(:new) @validator.validate(:new)
......
...@@ -10,6 +10,7 @@ module Gitlab ...@@ -10,6 +10,7 @@ module Gitlab
def initialize(node) def initialize(node)
@node = node @node = node
@metadata = {}
@attributes = {} @attributes = {}
end end
...@@ -18,8 +19,8 @@ module Gitlab ...@@ -18,8 +19,8 @@ module Gitlab
self self
end end
def parent(parent) def metadata(metadata)
@parent = parent @metadata.merge!(metadata)
self self
end end
...@@ -30,7 +31,6 @@ module Gitlab ...@@ -30,7 +31,6 @@ module Gitlab
def create! def create!
raise InvalidFactory unless defined?(@value) raise InvalidFactory unless defined?(@value)
raise InvalidFactory unless defined?(@parent)
## ##
# We assume that unspecified entry is undefined. # We assume that unspecified entry is undefined.
...@@ -60,9 +60,9 @@ module Gitlab ...@@ -60,9 +60,9 @@ module Gitlab
end end
def fabricate(node, value = nil) def fabricate(node, value = nil)
node.new(value).tap do |entry| node.new(value, @metadata).tap do |entry|
entry.key = @attributes[:key] entry.key = @attributes[:key]
entry.parent = @attributes[:parent] || @parent entry.parent = @attributes[:parent]
entry.description = @attributes[:description] entry.description = @attributes[:description]
end end
end end
......
...@@ -54,9 +54,8 @@ module Gitlab ...@@ -54,9 +54,8 @@ module Gitlab
def compose_jobs! def compose_jobs!
factory = Node::Factory.new(Node::Jobs) factory = Node::Factory.new(Node::Jobs)
.value(@config.except(*nodes.keys)) .value(@config.except(*nodes.keys))
.parent(self) .with(key: :jobs, parent: self,
.with(key: :jobs, global: self) description: 'Jobs definition for this pipeline')
.with(description: 'Jobs definition for this pipeline')
@entries[:jobs] = factory.create! @entries[:jobs] = factory.create!
end end
......
...@@ -10,12 +10,9 @@ module Gitlab ...@@ -10,12 +10,9 @@ module Gitlab
validations do validations do
validates :config, presence: true validates :config, presence: true
with_options on: :processed do
validates :name, presence: true validates :name, presence: true
validates :name, type: Symbol validates :name, type: Symbol
end end
end
node :before_script, Script, node :before_script, Script,
description: 'Global before script overridden in this job.' description: 'Global before script overridden in this job.'
...@@ -38,7 +35,7 @@ module Gitlab ...@@ -38,7 +35,7 @@ module Gitlab
helpers :before_script, :script, :stage, :type, :after_script, :cache helpers :before_script, :script, :stage, :type, :after_script, :cache
def name def name
@key @metadata[:name]
end end
def value def value
...@@ -48,11 +45,12 @@ module Gitlab ...@@ -48,11 +45,12 @@ module Gitlab
private private
def to_hash def to_hash
{ before_script: before_script_value, { name: name,
script: script_value, before_script: before_script,
stage: stage_value, script: script,
cache: cache_value, stage: stage,
after_script: after_script_value } cache: cache,
after_script: after_script }
end end
def compose! def compose!
......
...@@ -31,14 +31,15 @@ module Gitlab ...@@ -31,14 +31,15 @@ module Gitlab
private private
def create(name, config) def create(name, config)
Node::Factory.new(node(name)) Node::Factory.new(job_class(name))
.value(config || {}) .value(config || {})
.parent(self) .metadata(name: name)
.with(key: name, description: "#{name} job definition.") .with(key: name, parent: self,
description: "#{name} job definition.")
.create! .create!
end end
def node(name) def job_class(name)
if name.to_s.start_with?('.') if name.to_s.start_with?('.')
Node::HiddenJob Node::HiddenJob
else else
......
...@@ -4,32 +4,20 @@ describe Gitlab::Ci::Config::Node::Factory do ...@@ -4,32 +4,20 @@ describe Gitlab::Ci::Config::Node::Factory do
describe '#create!' do describe '#create!' do
let(:factory) { described_class.new(node) } let(:factory) { described_class.new(node) }
let(:node) { Gitlab::Ci::Config::Node::Script } let(:node) { Gitlab::Ci::Config::Node::Script }
let(:parent) { double('parent') }
context 'when setting a concrete value' do context 'when setting a concrete value' do
it 'creates entry with valid value' do it 'creates entry with valid value' do
entry = factory entry = factory
.value(['ls', 'pwd']) .value(['ls', 'pwd'])
.parent(parent)
.create! .create!
expect(entry.value).to eq ['ls', 'pwd'] expect(entry.value).to eq ['ls', 'pwd']
end end
it 'sets parent attributes' do
entry = factory
.value('ls')
.parent(parent)
.create!
expect(entry.parent).to eq parent
end
context 'when setting description' do context 'when setting description' do
it 'creates entry with description' do it 'creates entry with description' do
entry = factory entry = factory
.value(['ls', 'pwd']) .value(['ls', 'pwd'])
.parent(parent)
.with(description: 'test description') .with(description: 'test description')
.create! .create!
...@@ -42,7 +30,6 @@ describe Gitlab::Ci::Config::Node::Factory do ...@@ -42,7 +30,6 @@ describe Gitlab::Ci::Config::Node::Factory do
it 'creates entry with custom key' do it 'creates entry with custom key' do
entry = factory entry = factory
.value(['ls', 'pwd']) .value(['ls', 'pwd'])
.parent(parent)
.with(key: 'test key') .with(key: 'test key')
.create! .create!
...@@ -56,7 +43,6 @@ describe Gitlab::Ci::Config::Node::Factory do ...@@ -56,7 +43,6 @@ describe Gitlab::Ci::Config::Node::Factory do
it 'creates entry with valid parent' do it 'creates entry with valid parent' do
entry = factory entry = factory
.value('ls') .value('ls')
.parent(parent)
.with(parent: object) .with(parent: object)
.create! .create!
...@@ -73,23 +59,28 @@ describe Gitlab::Ci::Config::Node::Factory do ...@@ -73,23 +59,28 @@ describe Gitlab::Ci::Config::Node::Factory do
end end
end end
context 'when not setting parent object' do
it 'raises error' do
expect { factory.value('ls').create! }.to raise_error(
Gitlab::Ci::Config::Node::Factory::InvalidFactory
)
end
end
context 'when creating entry with nil value' do context 'when creating entry with nil value' do
it 'creates an undefined entry' do it 'creates an undefined entry' do
entry = factory entry = factory
.value(nil) .value(nil)
.parent(parent)
.create! .create!
expect(entry).to be_an_instance_of Gitlab::Ci::Config::Node::Undefined expect(entry).to be_an_instance_of Gitlab::Ci::Config::Node::Undefined
end end
end end
context 'when passing metadata' do
let(:node) { spy('node') }
it 'passes metadata as a parameter' do
factory
.value('some value')
.metadata(some: 'hash')
.create!
expect(node).to have_received(:new)
.with('some value', { some: 'hash' })
end
end
end end
end end
...@@ -128,11 +128,14 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -128,11 +128,14 @@ describe Gitlab::Ci::Config::Node::Global do
describe '#jobs' do describe '#jobs' do
it 'returns jobs configuration' do it 'returns jobs configuration' do
expect(global.jobs) expect(global.jobs).to eq(
.to eq(rspec: { script: %w[rspec ls], rspec: { name: :rspec,
script: %w[rspec ls],
stage: 'test' }, stage: 'test' },
spinach: { script: %w[spinach], spinach: { name: :spinach,
stage: 'test' }) script: %w[spinach],
stage: 'test' }
)
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Config::Node::Job do describe Gitlab::Ci::Config::Node::Job do
let(:entry) { described_class.new(config, key: :rspec) } let(:entry) { described_class.new(config, name: :rspec) }
before do before do
entry.process! entry.process!
...@@ -19,7 +19,7 @@ describe Gitlab::Ci::Config::Node::Job do ...@@ -19,7 +19,7 @@ describe Gitlab::Ci::Config::Node::Job do
end end
context 'when job name is empty' do context 'when job name is empty' do
let(:entry) { described_class.new(config, key: ''.to_sym) } let(:entry) { described_class.new(config, name: ''.to_sym) }
it 'reports error' do it 'reports error' do
expect(entry.errors) expect(entry.errors)
...@@ -35,7 +35,7 @@ describe Gitlab::Ci::Config::Node::Job do ...@@ -35,7 +35,7 @@ describe Gitlab::Ci::Config::Node::Job do
describe '#errors' do describe '#errors' do
it 'reports error about a config type' do it 'reports error about a config type' do
expect(entry.errors) expect(entry.errors)
.to include 'rspec config should be a hash' .to include 'job config should be a hash'
end end
end end
end end
...@@ -62,7 +62,8 @@ describe Gitlab::Ci::Config::Node::Job do ...@@ -62,7 +62,8 @@ describe Gitlab::Ci::Config::Node::Job do
it 'returns correct value' do it 'returns correct value' do
expect(entry.value) expect(entry.value)
.to eq(before_script: %w[ls pwd], .to eq(name: :rspec,
before_script: %w[ls pwd],
script: %w[rspec], script: %w[rspec],
stage: 'test', stage: 'test',
after_script: %w[cleanup]) after_script: %w[cleanup])
......
...@@ -61,10 +61,12 @@ describe Gitlab::Ci::Config::Node::Jobs do ...@@ -61,10 +61,12 @@ describe Gitlab::Ci::Config::Node::Jobs do
describe '#value' do describe '#value' do
it 'returns key value' do it 'returns key value' do
expect(entry.value) expect(entry.value).to eq(
.to eq(rspec: { script: %w[rspec], rspec: { name: :rspec,
script: %w[rspec],
stage: 'test' }, stage: 'test' },
spinach: { script: %w[spinach], spinach: { name: :spinach,
script: %w[spinach],
stage: 'test' }) stage: 'test' })
end end
end end
......
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