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

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

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