Commit be78a556 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'refactor/ci-config-add-facade' into 'master'

Add new GitLab CI configuration facade class

## What does this MR do?

This MR is a first iteration of major CI configuration refactoring. See #17139 and #15060 for more details.

## What are the relevant issue numbers?

Closes #17139

See merge request !4462
parents 5b83abcc 5ef104df
......@@ -12,18 +12,14 @@ module Ci
attr_reader :before_script, :after_script, :image, :services, :path, :cache
def initialize(config, path = nil)
@config = YAML.safe_load(config, [Symbol], [], true)
@config = Gitlab::Ci::Config.new(config).to_hash
@path = path
unless @config.is_a? Hash
raise ValidationError, "YAML should be a hash"
end
@config = @config.deep_symbolize_keys
initial_parsing
validate!
rescue Gitlab::Ci::Config::Loader::FormatError => e
raise ValidationError, e.message
end
def builds_for_stage_and_ref(stage, ref, tag = false, trigger_request = nil)
......
module Gitlab
module Ci
class Config
class LoaderError < StandardError; end
def initialize(config)
loader = Loader.new(config)
@config = loader.load!
end
def to_hash
@config
end
end
end
end
module Gitlab
module Ci
class Config
class Loader
class FormatError < StandardError; end
def initialize(config)
@config = YAML.safe_load(config, [Symbol], [], true)
end
def valid?
@config.is_a?(Hash)
end
def load!
unless valid?
raise FormatError, 'Invalid configuration format'
end
@config.deep_symbolize_keys
end
end
end
end
end
require 'spec_helper'
describe Gitlab::Ci::Config::Loader do
let(:loader) { described_class.new(yml) }
context 'when yaml syntax is correct' do
let(:yml) { 'image: ruby:2.2' }
describe '#valid?' do
it 'returns true' do
expect(loader.valid?).to be true
end
end
describe '#load!' do
it 'returns a valid hash' do
expect(loader.load!).to eq(image: 'ruby:2.2')
end
end
end
context 'when yaml syntax is incorrect' do
let(:yml) { '// incorrect' }
describe '#valid?' do
it 'returns false' do
expect(loader.valid?).to be false
end
end
describe '#load!' do
it 'raises error' do
expect { loader.load! }.to raise_error(
Gitlab::Ci::Config::Loader::FormatError,
'Invalid configuration format'
)
end
end
end
context 'when yaml config is empty' do
let(:yml) { '' }
describe '#valid?' do
it 'returns false' do
expect(loader.valid?).to be false
end
end
end
end
require 'spec_helper'
describe Gitlab::Ci::Config do
let(:config) do
described_class.new(yml)
end
context 'when config is valid' do
let(:yml) do
<<-EOS
image: ruby:2.2
rspec:
script:
- gem install rspec
- rspec
EOS
end
describe '#to_hash' do
it 'returns hash created from string' do
hash = {
image: 'ruby:2.2',
rspec: {
script: ['gem install rspec',
'rspec']
}
}
expect(config.to_hash).to eq hash
end
end
context 'when config is invalid' do
let(:yml) { '// invalid' }
describe '.new' do
it 'raises error' do
expect { config }.to raise_error(
Gitlab::Ci::Config::Loader::FormatError,
/Invalid configuration format/
)
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