2018-09-05 04:22:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-16 08:48:38 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
2018-09-05 04:08:12 -04:00
|
|
|
class Extendable
|
2018-08-16 08:48:38 -04:00
|
|
|
class Entry
|
2019-06-05 04:25:55 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-09-05 04:08:12 -04:00
|
|
|
InvalidExtensionError = Class.new(Extendable::ExtensionError)
|
|
|
|
CircularDependencyError = Class.new(Extendable::ExtensionError)
|
|
|
|
NestingTooDeepError = Class.new(Extendable::ExtensionError)
|
|
|
|
|
2018-09-05 04:17:13 -04:00
|
|
|
MAX_NESTING_LEVELS = 10
|
|
|
|
|
2018-08-16 08:48:38 -04:00
|
|
|
attr_reader :key
|
|
|
|
|
2018-08-16 09:30:06 -04:00
|
|
|
def initialize(key, context, parent = nil)
|
2018-08-16 08:48:38 -04:00
|
|
|
@key = key
|
2018-08-16 09:30:06 -04:00
|
|
|
@context = context
|
2018-08-16 08:48:38 -04:00
|
|
|
@parent = parent
|
2018-08-17 05:42:57 -04:00
|
|
|
|
2018-09-05 03:57:36 -04:00
|
|
|
unless @context.key?(@key)
|
|
|
|
raise StandardError, 'Invalid entry key!'
|
|
|
|
end
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
|
|
|
|
2018-08-17 05:42:57 -04:00
|
|
|
def extensible?
|
|
|
|
value.is_a?(Hash) && value.key?(:extends)
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
|
|
|
|
2018-08-16 09:01:27 -04:00
|
|
|
def value
|
2019-06-05 04:25:55 -04:00
|
|
|
strong_memoize(:value) do
|
|
|
|
@context.fetch(@key)
|
|
|
|
end
|
2018-08-16 09:01:27 -04:00
|
|
|
end
|
2018-08-16 08:48:38 -04:00
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
def base_hashes!
|
|
|
|
strong_memoize(:base_hashes) do
|
|
|
|
extends_keys.map do |key|
|
|
|
|
Extendable::Entry
|
|
|
|
.new(key, @context, self)
|
|
|
|
.extend!
|
|
|
|
end
|
|
|
|
end
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
def extends_keys
|
|
|
|
strong_memoize(:extends_keys) do
|
|
|
|
next unless extensible?
|
|
|
|
|
|
|
|
Array(value.fetch(:extends)).map(&:to_s).map(&:to_sym)
|
|
|
|
end
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
|
|
|
|
2018-09-05 03:57:36 -04:00
|
|
|
def ancestors
|
2019-06-05 04:25:55 -04:00
|
|
|
strong_memoize(:ancestors) do
|
|
|
|
Array(@parent&.ancestors) + Array(@parent&.key)
|
|
|
|
end
|
2018-08-16 09:30:06 -04:00
|
|
|
end
|
|
|
|
|
2018-08-16 08:48:38 -04:00
|
|
|
def extend!
|
2018-08-17 05:42:57 -04:00
|
|
|
return value unless extensible?
|
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
if unknown_extensions.any?
|
2018-09-05 04:08:12 -04:00
|
|
|
raise Entry::InvalidExtensionError,
|
2019-06-05 04:25:55 -04:00
|
|
|
"#{key}: unknown keys in `extends` (#{show_keys(unknown_extensions)})"
|
2018-08-16 09:30:06 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
if invalid_bases.any?
|
2018-09-05 04:08:12 -04:00
|
|
|
raise Entry::InvalidExtensionError,
|
2019-06-05 04:25:55 -04:00
|
|
|
"#{key}: invalid base hashes in `extends` (#{show_keys(invalid_bases)})"
|
2018-08-17 04:07:38 -04:00
|
|
|
end
|
|
|
|
|
2018-09-05 03:57:36 -04:00
|
|
|
if nesting_too_deep?
|
2018-09-05 04:08:12 -04:00
|
|
|
raise Entry::NestingTooDeepError,
|
2018-09-05 04:52:53 -04:00
|
|
|
"#{key}: nesting too deep in `extends`"
|
2018-09-05 03:57:36 -04:00
|
|
|
end
|
|
|
|
|
2018-08-17 05:42:57 -04:00
|
|
|
if circular_dependency?
|
2018-09-05 04:08:12 -04:00
|
|
|
raise Entry::CircularDependencyError,
|
2018-09-05 04:17:13 -04:00
|
|
|
"#{key}: circular dependency detected in `extends`"
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
2018-08-17 05:42:57 -04:00
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
merged = {}
|
|
|
|
base_hashes!.each { |h| merged.deep_merge!(h) }
|
|
|
|
|
|
|
|
@context[key] = merged.deep_merge!(value)
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
2018-08-17 04:07:38 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
def show_keys(keys)
|
|
|
|
keys.join(', ')
|
|
|
|
end
|
|
|
|
|
2018-09-05 03:57:36 -04:00
|
|
|
def nesting_too_deep?
|
|
|
|
ancestors.count > MAX_NESTING_LEVELS
|
|
|
|
end
|
|
|
|
|
2018-08-17 04:07:38 -04:00
|
|
|
def circular_dependency?
|
2022-05-03 05:07:47 -04:00
|
|
|
ancestors.include?(key) # rubocop:disable Performance/AncestorsInclude
|
2018-08-17 04:07:38 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
def unknown_extensions
|
|
|
|
strong_memoize(:unknown_extensions) do
|
|
|
|
extends_keys.reject { |key| @context.key?(key) }
|
|
|
|
end
|
2018-08-17 04:07:38 -04:00
|
|
|
end
|
2018-08-17 05:42:57 -04:00
|
|
|
|
2019-06-05 04:25:55 -04:00
|
|
|
def invalid_bases
|
|
|
|
strong_memoize(:invalid_bases) do
|
|
|
|
extends_keys.reject { |key| @context[key].is_a?(Hash) }
|
|
|
|
end
|
2018-08-17 05:42:57 -04:00
|
|
|
end
|
2018-08-16 08:48:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|