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

94 lines
2.7 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
2019-12-21 20:55:43 +05:30
MAX_INCLUDES = 100
2019-07-07 11:18:12 +05:30
2019-02-15 15:39:39 +05:30
FILE_CLASSES = [
External::File::Remote,
External::File::Template,
External::File::Local,
2020-04-08 14:13:33 +05:30
External::File::Project,
External::File::Artifact
2019-02-15 15:39:39 +05:30
].freeze
2019-07-07 11:18:12 +05:30
Error = Class.new(StandardError)
AmbigiousSpecificationError = Class.new(Error)
DuplicateIncludesError = Class.new(Error)
TooManyIncludesError = Class.new(Error)
2019-12-21 20:55:43 +05:30
def initialize(values, context)
2019-02-15 15:39:39 +05:30
@locations = Array.wrap(values.fetch(:include, []))
2019-12-21 20:55:43 +05:30
@context = context
2018-12-13 13:39:08 +05:30
end
def process
2019-07-07 11:18:12 +05:30
return [] if locations.empty?
2019-02-15 15:39:39 +05:30
locations
.compact
.map(&method(:normalize_location))
2019-07-07 11:18:12 +05:30
.each(&method(:verify_duplicates!))
2019-02-15 15:39:39 +05:30
.map(&method(:select_first_matching))
2018-12-13 13:39:08 +05:30
end
private
2019-12-21 20:55:43 +05:30
attr_reader :locations, :context
delegate :expandset, to: :context
2019-02-15 15:39:39 +05:30
# 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
2019-07-07 11:18:12 +05:30
def verify_duplicates!(location)
if expandset.count >= MAX_INCLUDES
raise TooManyIncludesError, "Maximum of #{MAX_INCLUDES} nested includes are allowed!"
end
# We scope location to context, as this allows us to properly support
2019-12-21 20:55:43 +05:30
# relative includes, and similarly looking relative in another project
2019-07-07 11:18:12 +05:30
# does not trigger duplicate error
scoped_location = location.merge(
2019-12-21 20:55:43 +05:30
context_project: context.project,
context_sha: context.sha)
2019-07-07 11:18:12 +05:30
unless expandset.add?(scoped_location)
raise DuplicateIncludesError, "Include `#{location.to_json}` was already included!"
end
end
2019-02-15 15:39:39 +05:30
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
2018-12-13 13:39:08 +05:30
end
end
end
end
end