debian-mirror-gitlab/lib/gitlab/config/entry/node.rb

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

157 lines
3.4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Config
module Entry
##
# Base abstract class for each configuration entry node.
#
class Node
InvalidError = Class.new(StandardError)
attr_reader :config, :metadata
2022-03-02 08:16:31 +05:30
attr_accessor :key, :parent, :default, :description, :deprecation
2019-02-15 15:39:39 +05:30
def initialize(config, **metadata)
@config = config
@metadata = metadata
@entries = {}
2020-07-28 23:09:34 +05:30
@warnings = []
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
yield(self) if block_given?
2019-02-15 15:39:39 +05:30
self.class.aspects.to_a.each do |aspect|
instance_exec(&aspect)
end
end
def [](key)
@entries[key] || Entry::Undefined.new
end
def compose!(deps = nil)
return unless valid?
yield if block_given?
end
def leaf?
@entries.none?
end
def descendants
@entries.values
end
def ancestors
@parent ? @parent.ancestors + [@parent] : []
end
2019-07-07 11:18:12 +05:30
def opt(key)
opt = metadata[key]
opt = @parent.opt(key) if opt.nil? && @parent
opt
end
2019-02-15 15:39:39 +05:30
def valid?
errors.none?
end
def errors
[]
end
2020-07-28 23:09:34 +05:30
def warnings
@warnings + descendants.flat_map(&:warnings)
end
def add_warning(message)
@warnings << "#{location} #{message}"
end
2019-02-15 15:39:39 +05:30
def value
if leaf?
@config
else
meaningful = @entries.select do |_key, value|
value.specified? && value.relevant?
end
2020-07-28 23:09:34 +05:30
meaningful.transform_values { |entry| entry.value }
2019-02-15 15:39:39 +05:30
end
end
def specified?
true
end
def relevant?
true
end
def location
name = @key.presence || self.class.name.to_s.demodulize
.underscore.humanize.downcase
ancestors.map(&:key).append(name).compact.join(':')
end
def inspect
val = leaf? ? config : descendants
unspecified = specified? ? '' : '(unspecified) '
"#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
end
2019-07-07 11:18:12 +05:30
def hash?
@config.is_a?(Hash)
end
2022-07-23 23:45:48 +05:30
def array?
@config.is_a?(Array)
end
2019-07-07 11:18:12 +05:30
def string?
@config.is_a?(String)
end
def integer?
@config.is_a?(Integer)
end
2019-03-02 22:35:43 +05:30
def self.default(**)
2019-02-15 15:39:39 +05:30
end
def self.aspects
@aspects ||= []
end
2019-12-26 22:10:19 +05:30
def self.with_aspect(blk)
self.aspects.append(blk)
end
2019-02-15 15:39:39 +05:30
private
attr_reader :entries
2022-03-02 08:16:31 +05:30
def log_and_warn_deprecated_entry(entry)
user = metadata[:user]
project = metadata[:project]
if project && user
Gitlab::AppJsonLogger.info(event: 'ci_used_deprecated_keyword',
entry: entry.key.to_s,
user_id: user.id,
project_id: project.id)
end
deprecation = entry.deprecation
add_warning(
"`#{entry.key}` is deprecated in " \
"#{deprecation[:deprecated]} and will be removed in #{deprecation[:removed]}."
)
end
2019-02-15 15:39:39 +05:30
end
end
end
end