debian-mirror-gitlab/lib/gitlab/ci/config/external/mapper.rb

74 lines
1.9 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module External
class Mapper
2019-02-15 15:39:39 +05:30
include Gitlab::Utils::StrongMemoize
FILE_CLASSES = [
External::File::Remote,
External::File::Template,
External::File::Local,
External::File::Project
].freeze
AmbigiousSpecificationError = Class.new(StandardError)
def initialize(values, project:, sha:, user:)
@locations = Array.wrap(values.fetch(:include, []))
2018-12-13 13:39:08 +05:30
@project = project
@sha = sha
2019-02-15 15:39:39 +05:30
@user = user
2018-12-13 13:39:08 +05:30
end
def process
2019-02-15 15:39:39 +05:30
locations
.compact
.map(&method(:normalize_location))
.map(&method(:select_first_matching))
2018-12-13 13:39:08 +05:30
end
private
2019-02-15 15:39:39 +05:30
attr_reader :locations, :project, :sha, :user
# convert location if String to canonical form
def normalize_location(location)
if location.is_a?(String)
normalize_location_string(location)
else
location.deep_symbolize_keys
end
end
2018-12-13 13:39:08 +05:30
2019-02-15 15:39:39 +05:30
def normalize_location_string(location)
2018-12-13 13:39:08 +05:30
if ::Gitlab::UrlSanitizer.valid?(location)
2019-02-15 15:39:39 +05:30
{ remote: location }
2018-12-13 13:39:08 +05:30
else
2019-02-15 15:39:39 +05:30
{ local: location }
end
end
def select_first_matching(location)
matching = FILE_CLASSES.map do |file_class|
file_class.new(location, context)
end.select(&:matching?)
raise AmbigiousSpecificationError, "Include `#{location.to_json}` needs to match exactly one accessor!" unless matching.one?
matching.first
end
def context
strong_memoize(:context) do
External::File::Base::Context.new(project, sha, user)
2018-12-13 13:39:08 +05:30
end
end
end
end
end
end
end