diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 026a5ac97caf3841e6ce61dc3fcc71523aa3adfc..9a60c5ab8427e235e3f3946f9e4d5d9172c2d69d 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -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::ParserError => e + raise ValidationError, e.message end def builds_for_stage_and_ref(stage, ref, tag = false, trigger_request = nil) diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb new file mode 100644 index 0000000000000000000000000000000000000000..8f88ccf5bf4ed6fd2302febbf8beed552445572d --- /dev/null +++ b/lib/gitlab/ci/config.rb @@ -0,0 +1,21 @@ +module Gitlab + module Ci + class Config + class ParserError < StandardError; end + + def initialize(config) + @config = YAML.safe_load(config, [Symbol], [], true) + + unless @config.is_a?(Hash) + raise ParserError, 'YAML should be a hash' + end + + @config = @config.deep_symbolize_keys + end + + def to_hash + @config + end + end + end +end diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..6e2517067145de3673446fa21e9203e1380009b6 --- /dev/null +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe Gitlab::Ci::Config do + let(:config) do + described_class.new(yml) + end + + context 'when yml 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 + end +end