2023-04-23 21:23:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Config
|
|
|
|
module Loader
|
|
|
|
class MultiDocYaml
|
2023-05-27 22:25:52 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
MULTI_DOC_DIVIDER = /^---\s+/.freeze
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
def initialize(config, max_documents:, additional_permitted_classes: [])
|
2023-05-27 22:25:52 +05:30
|
|
|
@config = config
|
2023-04-23 21:23:45 +05:30
|
|
|
@max_documents = max_documents
|
2023-05-27 22:25:52 +05:30
|
|
|
@additional_permitted_classes = additional_permitted_classes
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def valid?
|
|
|
|
documents.all?(&:valid?)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def load_raw!
|
|
|
|
documents.map(&:load_raw!)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def load!
|
|
|
|
documents.map(&:load!)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :config, :max_documents, :additional_permitted_classes
|
|
|
|
|
|
|
|
# Valid YAML files can start with either a leading delimiter or no delimiter.
|
|
|
|
# To avoid counting a leading delimiter towards the document limit,
|
|
|
|
# this method splits the file by one more than the maximum number of permitted documents.
|
|
|
|
# It then discards the first document if it is blank.
|
|
|
|
def documents
|
|
|
|
docs = config
|
|
|
|
.split(MULTI_DOC_DIVIDER, max_documents_including_leading_delimiter)
|
|
|
|
.map { |d| Yaml.new(d, additional_permitted_classes: additional_permitted_classes) }
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
docs.shift if docs.first.blank?
|
|
|
|
docs
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
strong_memoize_attr :documents
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def max_documents_including_leading_delimiter
|
|
|
|
max_documents + 1
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|