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

documents.rb « yaml « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04a31da8a2e8bd25f2036f803fb9f7e45c775856 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Yaml
        class Documents
          include Gitlab::Utils::StrongMemoize

          attr_reader :errors

          def initialize(documents)
            @documents = documents
            @errors = []

            parsed_first_document
          end

          def valid?
            errors.none?
          end

          def header
            return unless has_header?

            parsed_first_document
          end

          def content
            return documents.last.raw if has_header?

            documents.first&.raw || ''
          end

          private

          attr_reader :documents

          def has_header?
            return false unless parsed_first_document.is_a?(Hash)

            documents.count > 1 && parsed_first_document.key?(:spec)
          end

          def parsed_first_document
            return {} if documents.count == 0

            documents.first.load!
          rescue ::Gitlab::Config::Loader::FormatError => e
            errors << e.message
          end
          strong_memoize_attr :parsed_first_document
        end
      end
    end
  end
end