Commit ccbdb402 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Integrate CI job stage entry into CI configuration

parent 1b624ba4
......@@ -20,21 +20,21 @@ module Gitlab
end
@validator = self.class.validator.new(self)
@validator.validate
@validator.validate(:new)
end
def process!
return unless valid?
nodes.each do |key, essence|
@entries[key] = create(key, essence)
end
compose!
@entries.each_value(&:process!)
end
def validate!
@validator.validate(:processed)
if @validator.valid?(:new)
@validator.validate(:processed)
end
@entries.each_value(&:validate!)
end
......@@ -95,6 +95,12 @@ module Gitlab
private
def compose!
nodes.each do |key, essence|
@entries[key] = create(key, essence)
end
end
def create(entry, essence)
raise NotImplementedError
end
......
......@@ -7,6 +7,30 @@ module Gitlab
#
class Job < Entry
include Configurable
node :stage, Stage,
description: 'Pipeline stage this job will be executed into.'
node :type, Stage,
description: 'Deprecated: stage this job will be executed into.'
helpers :stage, :type
def value
@config.merge(stage: stage_value)
end
private
def compose!
super
if type_defined? && !stage_defined?
@entries[:stage] = @entries[:type]
end
@entries.delete(:type)
end
end
end
end
......
......@@ -9,7 +9,7 @@ module Gitlab
include Validatable
validations do
validates :config, key: true
validates :config, type: String
validates :global, required_attribute: true
validate :known_stage, on: :processed
......@@ -27,7 +27,7 @@ module Gitlab
end
def self.default
:test
'test'
end
end
end
......
......@@ -1082,21 +1082,21 @@ EOT
config = YAML.dump({ rspec: { script: "test", type: 1 } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:type config should be a string")
end
it "returns errors if job stage is not a pre-defined stage" do
config = YAML.dump({ rspec: { script: "test", type: "acceptance" } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test, deploy")
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:type config should be one of defined stages (build, test, deploy)")
end
it "returns errors if job stage is not a defined stage" do
config = YAML.dump({ types: ["build", "test"], rspec: { script: "test", type: "acceptance" } })
expect do
GitlabCiYamlProcessor.new(config, path)
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "rspec job: stage parameter should be build, test")
end.to raise_error(GitlabCiYamlProcessor::ValidationError, "jobs:rspec:type config should be one of defined stages (build, test)")
end
it "returns errors if stages is not an array" do
......
......@@ -129,8 +129,8 @@ describe Gitlab::Ci::Config::Node::Global do
describe '#jobs' do
it 'returns jobs configuration' do
expect(global.jobs)
.to eq(rspec: { script: 'rspec' },
spinach: { script: 'spinach' })
.to eq(rspec: { script: 'rspec', stage: 'test' },
spinach: { script: 'spinach', stage: 'test' })
end
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Job do
let(:entry) { described_class.new(config) }
let(:entry) { described_class.new(config, global: global) }
let(:global) { spy('Global') }
describe 'validations' do
before do
entry.process!
entry.validate!
end
context 'when entry config value is correct' do
let(:config) { { script: 'rspec' } }
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq(script: 'rspec')
expect(entry.value)
.to eq(script: 'rspec',
stage: 'test')
end
end
......
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Jobs do
let(:entry) { described_class.new(config) }
let(:entry) { described_class.new(config, global: spy) }
describe 'validations' do
context 'when entry config value is correct' do
......@@ -61,8 +61,9 @@ describe Gitlab::Ci::Config::Node::Jobs do
describe '#value' do
it 'returns key value' do
expect(entry.value).to eq(rspec: { script: 'rspec' },
spinach: { script: 'spinach' })
expect(entry.value)
.to eq(rspec: { script: 'rspec', stage: 'test' },
spinach: { script: 'spinach', stage: 'test' })
end
end
......
......@@ -6,7 +6,11 @@ describe Gitlab::Ci::Config::Node::Stage do
describe 'validations' do
context 'when stage config value is correct' do
let(:config) { :build }
let(:config) { 'build' }
before do
allow(global).to receive(:stages).and_return(%w[build])
end
describe '#value' do
it 'returns a stage key' do
......@@ -39,16 +43,16 @@ describe Gitlab::Ci::Config::Node::Stage do
it 'reports errors about wrong type' do
expect(stage.errors)
.to include 'stage config should be a string or symbol'
.to include 'stage config should be a string'
end
end
context 'when stage is not present in global configuration' do
let(:config) { :unknown }
let(:config) { 'unknown' }
before do
allow(global)
.to receive(:stages).and_return([:test, :deploy])
.to receive(:stages).and_return(%w[test deploy])
end
it 'reports error about missing stage' do
......@@ -65,7 +69,7 @@ describe Gitlab::Ci::Config::Node::Stage do
describe '#known?' do
before do
allow(global).to receive(:stages).and_return([:test, :deploy])
allow(global).to receive(:stages).and_return(%w[test deploy])
end
context 'when stage is not known' do
......@@ -77,7 +81,7 @@ describe Gitlab::Ci::Config::Node::Stage do
end
context 'when stage is known' do
let(:config) { :test }
let(:config) { 'test' }
it 'returns false' do
expect(stage.known?).to be true
......@@ -87,7 +91,7 @@ describe Gitlab::Ci::Config::Node::Stage do
describe '.default' do
it 'returns default stage' do
expect(described_class.default).to eq :test
expect(described_class.default).to eq 'test'
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