debian-mirror-gitlab/lib/gitlab/ci/config/yaml.rb

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

71 lines
1.8 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module Yaml
AVAILABLE_TAGS = [Config::Yaml::Tags::Reference].freeze
2023-04-23 21:23:45 +05:30
MAX_DOCUMENTS = 2
2021-03-11 19:13:27 +05:30
2023-05-27 22:25:52 +05:30
class Loader
def initialize(content, project: nil)
@content = content
@project = project
end
def load!
2021-03-11 19:13:27 +05:30
ensure_custom_tags
2023-05-27 22:25:52 +05:30
if project.present? && ::Feature.enabled?(:ci_multi_doc_yaml, project)
::Gitlab::Config::Loader::MultiDocYaml.new(
2023-04-23 21:23:45 +05:30
content,
max_documents: MAX_DOCUMENTS,
additional_permitted_classes: AVAILABLE_TAGS
2023-05-27 22:25:52 +05:30
).load!
2023-04-23 21:23:45 +05:30
else
2023-05-27 22:25:52 +05:30
::Gitlab::Config::Loader::Yaml
.new(content, additional_permitted_classes: AVAILABLE_TAGS)
.load!
2023-04-23 21:23:45 +05:30
end
2021-03-11 19:13:27 +05:30
end
2023-05-27 22:25:52 +05:30
def to_result
Yaml::Result.new(config: load!, error: nil)
rescue ::Gitlab::Config::Loader::FormatError => e
Yaml::Result.new(error: e)
end
2021-03-11 19:13:27 +05:30
private
2023-05-27 22:25:52 +05:30
attr_reader :content, :project
2021-03-11 19:13:27 +05:30
def ensure_custom_tags
@ensure_custom_tags ||= begin
AVAILABLE_TAGS.each { |klass| Psych.add_tag(klass.tag, klass) }
true
end
end
end
2023-05-27 22:25:52 +05:30
class << self
def load!(content, project: nil)
Loader.new(content, project: project).to_result.then do |result|
##
# raise an error for backwards compatibility
#
raise result.error unless result.valid?
result.content
end
end
def load_result!(content, project: nil)
Loader.new(content, project: project).to_result
end
end
2021-03-11 19:13:27 +05:30
end
end
end
end