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