processor.rb 1.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
module Gitlab
  module Ci
    module External
      class Processor
        FileError = Class.new(StandardError)

        def initialize(values, project, sha)
          @values = values
          @external_files = Gitlab::Ci::External::Mapper.new(values, project, sha).process
          @content = {}
        end

        def perform
          return values if external_files.empty?

          external_files.each do |external_file|
            validate_external_file(external_file)
            @content.deep_merge!(content_of(external_file))
          end

          append_inline_content
          remove_include_keyword
        end

        private

        attr_reader :values, :external_files, :content

        def validate_external_file(external_file)
          unless external_file.valid?
            raise FileError, external_file.error_message
          end
        end

        def content_of(external_file)
          Gitlab::Ci::Config::Loader.new(external_file.content).load!
        end

        def append_inline_content
          @content.deep_merge!(@values)
        end

        def remove_include_keyword
          content.delete(:include)
          content
        end
      end
    end
  end
end