Commit 9a4117ab authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add support for `extends` key in CI/CD configuration

parent 88086559
...@@ -4,12 +4,17 @@ module Gitlab ...@@ -4,12 +4,17 @@ module Gitlab
# Base GitLab CI Configuration facade # Base GitLab CI Configuration facade
# #
class Config class Config
# EE would override this and utilize opts argument ConfigError = Class.new(StandardError)
def initialize(config, opts = {}) def initialize(config, opts = {})
@config = Loader.new(config).load! @config = Extendable::Collection
.new(build_config(config, opts))
.to_hash
@global = Entry::Global.new(@config) @global = Entry::Global.new(@config)
@global.compose! @global.compose!
rescue Loader::FormatError, Extendable::Collection::ExtensionError => e
raise Config::ConfigError, e.message
end end
def valid? def valid?
...@@ -58,6 +63,11 @@ module Gitlab ...@@ -58,6 +63,11 @@ module Gitlab
def jobs def jobs
@global.jobs_value @global.jobs_value
end end
# 'opts' argument is used in EE see /ee/lib/ee/gitlab/ci/config.rb
def build_config(config, opts = {})
Loader.new(config).load!
end
end end
end end
end end
...@@ -9,9 +9,10 @@ module Gitlab ...@@ -9,9 +9,10 @@ module Gitlab
include Configurable include Configurable
include Attributable include Attributable
ALLOWED_KEYS = %i[tags script only except type image services allow_failure ALLOWED_KEYS = %i[tags script only except type image services
type stage when artifacts cache dependencies before_script allow_failure type stage when artifacts cache
after_script variables environment coverage retry].freeze dependencies before_script after_script variables
environment coverage retry extends].freeze
validations do validations do
validates :config, allowed_keys: ALLOWED_KEYS validates :config, allowed_keys: ALLOWED_KEYS
...@@ -32,6 +33,7 @@ module Gitlab ...@@ -32,6 +33,7 @@ module Gitlab
'always or manual' } 'always or manual' }
validates :dependencies, array_of_strings: true validates :dependencies, array_of_strings: true
validates :extends, type: String
end end
end end
...@@ -81,7 +83,8 @@ module Gitlab ...@@ -81,7 +83,8 @@ module Gitlab
:cache, :image, :services, :only, :except, :variables, :cache, :image, :services, :only, :except, :variables,
:artifacts, :commands, :environment, :coverage, :retry :artifacts, :commands, :environment, :coverage, :retry
attributes :script, :tags, :allow_failure, :when, :dependencies, :retry attributes :script, :tags, :allow_failure, :when, :dependencies,
:retry, :extends
def compose!(deps = nil) def compose!(deps = nil)
super do super do
......
...@@ -16,7 +16,7 @@ module Gitlab ...@@ -16,7 +16,7 @@ module Gitlab
end end
initial_parsing initial_parsing
rescue Gitlab::Ci::Config::Loader::FormatError => e rescue Gitlab::Ci::Config::ConfigError => e
raise ValidationError, e.message raise ValidationError, e.message
end end
......
require 'spec_helper' require 'fast_spec_helper'
require_dependency 'active_model'
describe Gitlab::Ci::Config do describe Gitlab::Ci::Config do
let(:config) do let(:config) do
...@@ -42,6 +44,36 @@ describe Gitlab::Ci::Config do ...@@ -42,6 +44,36 @@ describe Gitlab::Ci::Config do
end end
end end
context 'when using extendable hash' do
let(:yml) do
<<-EOS
image: ruby:2.2
rspec:
script: rspec
test:
extends: rspec
image: ruby:alpine
EOS
end
it 'correctly extends the hash' do
hash = {
image: 'ruby:2.2',
rspec: { script: 'rspec' },
test: {
extends: 'rspec',
image: 'ruby:alpine',
script: 'rspec'
}
}
expect(config).to be_valid
expect(config.to_hash).to eq hash
end
end
context 'when config is invalid' do context 'when config is invalid' do
context 'when yml is incorrect' do context 'when yml is incorrect' do
let(:yml) { '// invalid' } let(:yml) { '// invalid' }
...@@ -49,7 +81,7 @@ describe Gitlab::Ci::Config do ...@@ -49,7 +81,7 @@ describe Gitlab::Ci::Config do
describe '.new' do describe '.new' do
it 'raises error' do it 'raises error' do
expect { config }.to raise_error( expect { config }.to raise_error(
::Gitlab::Ci::Config::Loader::FormatError, described_class::ConfigError,
/Invalid configuration format/ /Invalid configuration format/
) )
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