summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-15 21:07:51 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2016-07-15 21:07:51 +0200
commit41bcbdd8c2412769a376cd37541ad6e65a1af1f2 (patch)
tree09a1a1ba53ef8dcfe17e6851c0937052a2f2f77a
parent615c9730e7783e82287d2b65f58da6336d3d2410 (diff)
downloadgitlab-ce-41bcbdd8c2412769a376cd37541ad6e65a1af1f2.tar.gz
Add metadata to new CI config and expose job name
-rw-r--r--lib/ci/gitlab_ci_yaml_processor.rb4
-rw-r--r--lib/gitlab/ci/config/node/configurable.rb3
-rw-r--r--lib/gitlab/ci/config/node/entry.rb7
-rw-r--r--lib/gitlab/ci/config/node/factory.rb10
-rw-r--r--lib/gitlab/ci/config/node/global.rb5
-rw-r--r--lib/gitlab/ci/config/node/job.rb20
-rw-r--r--lib/gitlab/ci/config/node/jobs.rb9
-rw-r--r--spec/lib/gitlab/ci/config/node/factory_spec.rb37
-rw-r--r--spec/lib/gitlab/ci/config/node/global_spec.rb13
-rw-r--r--spec/lib/gitlab/ci/config/node/job_spec.rb9
-rw-r--r--spec/lib/gitlab/ci/config/node/jobs_spec.rb12
11 files changed, 60 insertions, 69 deletions
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb
index 3e4767cc9f6..0704e8f1683 100644
--- a/lib/ci/gitlab_ci_yaml_processor.rb
+++ b/lib/ci/gitlab_ci_yaml_processor.rb
@@ -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
diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb
index b33d743e2c3..10b2db86d24 100644
--- a/lib/gitlab/ci/config/node/configurable.rb
+++ b/lib/gitlab/ci/config/node/configurable.rb
@@ -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
diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb
index 9640103ea22..011c3be849e 100644
--- a/lib/gitlab/ci/config/node/entry.rb
+++ b/lib/gitlab/ci/config/node/entry.rb
@@ -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)
diff --git a/lib/gitlab/ci/config/node/factory.rb b/lib/gitlab/ci/config/node/factory.rb
index b509c5edf59..707b052e6a8 100644
--- a/lib/gitlab/ci/config/node/factory.rb
+++ b/lib/gitlab/ci/config/node/factory.rb
@@ -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
diff --git a/lib/gitlab/ci/config/node/global.rb b/lib/gitlab/ci/config/node/global.rb
index 4a958735599..bedacd904cc 100644
--- a/lib/gitlab/ci/config/node/global.rb
+++ b/lib/gitlab/ci/config/node/global.rb
@@ -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
diff --git a/lib/gitlab/ci/config/node/job.rb b/lib/gitlab/ci/config/node/job.rb
index 483be2a21cc..9280412a638 100644
--- a/lib/gitlab/ci/config/node/job.rb
+++ b/lib/gitlab/ci/config/node/job.rb
@@ -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!
diff --git a/lib/gitlab/ci/config/node/jobs.rb b/lib/gitlab/ci/config/node/jobs.rb
index 3c1851b9fea..3cabcd6b763 100644
--- a/lib/gitlab/ci/config/node/jobs.rb
+++ b/lib/gitlab/ci/config/node/jobs.rb
@@ -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
diff --git a/spec/lib/gitlab/ci/config/node/factory_spec.rb b/spec/lib/gitlab/ci/config/node/factory_spec.rb
index 4e6f2419e13..d26185ba585 100644
--- a/spec/lib/gitlab/ci/config/node/factory_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/factory_spec.rb
@@ -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
diff --git a/spec/lib/gitlab/ci/config/node/global_spec.rb b/spec/lib/gitlab/ci/config/node/global_spec.rb
index dfcaebe35be..2a071b57c72 100644
--- a/spec/lib/gitlab/ci/config/node/global_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/global_spec.rb
@@ -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
diff --git a/spec/lib/gitlab/ci/config/node/job_spec.rb b/spec/lib/gitlab/ci/config/node/job_spec.rb
index 4c7ac9949cc..b2559e6e73c 100644
--- a/spec/lib/gitlab/ci/config/node/job_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/job_spec.rb
@@ -1,7 +1,7 @@
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])
diff --git a/spec/lib/gitlab/ci/config/node/jobs_spec.rb b/spec/lib/gitlab/ci/config/node/jobs_spec.rb
index c4c130abb6d..4f08f2f9b69 100644
--- a/spec/lib/gitlab/ci/config/node/jobs_spec.rb
+++ b/spec/lib/gitlab/ci/config/node/jobs_spec.rb
@@ -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