Welcome to mirror list, hosted at ThFree Co, Russian Federation.

yaml.rb « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 729e7e3ac05dcf6c7620b783415ca7e545f0e0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Yaml
        AVAILABLE_TAGS = [Config::Yaml::Tags::Reference].freeze
        MAX_DOCUMENTS = 2

        class Loader
          def initialize(content, project: nil)
            @content = content
            @project = project
          end

          def load!
            ensure_custom_tags

            if project.present? && ::Feature.enabled?(:ci_multi_doc_yaml, project)
              ::Gitlab::Config::Loader::MultiDocYaml.new(
                content,
                max_documents: MAX_DOCUMENTS,
                additional_permitted_classes: AVAILABLE_TAGS,
                reject_empty: true
              ).load!
            else
              ::Gitlab::Config::Loader::Yaml
                .new(content, additional_permitted_classes: AVAILABLE_TAGS)
                .load!
            end
          end

          def to_result
            Yaml::Result.new(config: load!, error: nil)
          rescue ::Gitlab::Config::Loader::FormatError => e
            Yaml::Result.new(error: e)
          end

          private

          attr_reader :content, :project

          def ensure_custom_tags
            @ensure_custom_tags ||= begin
              AVAILABLE_TAGS.each { |klass| Psych.add_tag(klass.tag, klass) }

              true
            end
          end
        end

        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
      end
    end
  end
end