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