debian-mirror-gitlab/lib/gitlab/ci/config/extendable/entry.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
class Extendable
class Entry
2019-09-04 21:01:54 +05:30
include Gitlab::Utils::StrongMemoize
2018-11-20 20:47:30 +05:30
InvalidExtensionError = Class.new(Extendable::ExtensionError)
CircularDependencyError = Class.new(Extendable::ExtensionError)
NestingTooDeepError = Class.new(Extendable::ExtensionError)
MAX_NESTING_LEVELS = 10
attr_reader :key
def initialize(key, context, parent = nil)
@key = key
@context = context
@parent = parent
unless @context.key?(@key)
raise StandardError, 'Invalid entry key!'
end
end
def extensible?
value.is_a?(Hash) && value.key?(:extends)
end
def value
2019-09-04 21:01:54 +05:30
strong_memoize(:value) do
@context.fetch(@key)
end
2018-11-20 20:47:30 +05:30
end
2019-09-04 21:01:54 +05:30
def base_hashes!
strong_memoize(:base_hashes) do
extends_keys.map do |key|
Extendable::Entry
.new(key, @context, self)
.extend!
end
end
2018-11-20 20:47:30 +05:30
end
2019-09-04 21:01:54 +05:30
def extends_keys
strong_memoize(:extends_keys) do
next unless extensible?
Array(value.fetch(:extends)).map(&:to_s).map(&:to_sym)
end
2018-11-20 20:47:30 +05:30
end
def ancestors
2019-09-04 21:01:54 +05:30
strong_memoize(:ancestors) do
Array(@parent&.ancestors) + Array(@parent&.key)
end
2018-11-20 20:47:30 +05:30
end
def extend!
return value unless extensible?
2019-09-04 21:01:54 +05:30
if unknown_extensions.any?
2018-11-20 20:47:30 +05:30
raise Entry::InvalidExtensionError,
2019-09-04 21:01:54 +05:30
"#{key}: unknown keys in `extends` (#{show_keys(unknown_extensions)})"
2018-11-20 20:47:30 +05:30
end
2019-09-04 21:01:54 +05:30
if invalid_bases.any?
2018-11-20 20:47:30 +05:30
raise Entry::InvalidExtensionError,
2019-09-04 21:01:54 +05:30
"#{key}: invalid base hashes in `extends` (#{show_keys(invalid_bases)})"
2018-11-20 20:47:30 +05:30
end
if nesting_too_deep?
raise Entry::NestingTooDeepError,
"#{key}: nesting too deep in `extends`"
end
if circular_dependency?
raise Entry::CircularDependencyError,
"#{key}: circular dependency detected in `extends`"
end
2019-09-04 21:01:54 +05:30
merged = {}
base_hashes!.each { |h| merged.deep_merge!(h) }
@context[key] = merged.deep_merge!(value)
2018-11-20 20:47:30 +05:30
end
private
2019-09-04 21:01:54 +05:30
def show_keys(keys)
keys.join(', ')
end
2018-11-20 20:47:30 +05:30
def nesting_too_deep?
ancestors.count > MAX_NESTING_LEVELS
end
def circular_dependency?
ancestors.include?(key)
end
2019-09-04 21:01:54 +05:30
def unknown_extensions
strong_memoize(:unknown_extensions) do
extends_keys.reject { |key| @context.key?(key) }
end
2018-11-20 20:47:30 +05:30
end
2019-09-04 21:01:54 +05:30
def invalid_bases
strong_memoize(:invalid_bases) do
extends_keys.reject { |key| @context[key].is_a?(Hash) }
end
2018-11-20 20:47:30 +05:30
end
end
end
end
end
end