debian-mirror-gitlab/lib/gitlab/config/loader/multi_doc_yaml.rb

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

56 lines
1.6 KiB
Ruby
Raw Normal View History

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
2023-06-20 00:43:36 +05:30
def initialize(config, max_documents:, additional_permitted_classes: [], reject_empty: false)
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-06-20 00:43:36 +05:30
@reject_empty = reject_empty
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
2023-06-20 00:43:36 +05:30
attr_reader :config, :max_documents, :additional_permitted_classes, :reject_empty
2023-05-27 22:25:52 +05:30
# 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?
2023-06-20 00:43:36 +05:30
docs.reject!(&:blank?) if reject_empty
2023-05-27 22:25:52 +05:30
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