summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/node/configurable.rb
blob: d60f87f3f94d5e127932c410600aa2183c89a3bc (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
module Gitlab
  module Ci
    class Config
      module Node
        ##
        # This mixin is responsible for adding DSL, which purpose is to
        # simplifly process of adding child nodes.
        #
        # This can be used only if parent node is a configuration entry that
        # holds a hash as a configuration value, for example:
        #
        # job:
        #   script: ...
        #   artifacts: ...
        #
        module Configurable
          extend ActiveSupport::Concern

          def allowed_nodes
            self.class.allowed_nodes || {}
          end

          private

          def prevalidate!
            unless @value.is_a?(Hash)
              @errors << 'should be a configuration entry with hash value'
            end
          end

          def create_node(key, factory)
            factory.with(value: @value[key])
            factory.nullify! unless @value.has_key?(key)
            factory.create!
          end

          class_methods do
            def allowed_nodes
              Hash[@allowed_nodes.map { |key, factory| [key, factory.dup] }]
            end

            private

            def allow_node(symbol, entry_class, metadata)
              factory = Node::Factory.new(entry_class)
                .with(description: metadata[:description])

              define_method(symbol) do
                raise Entry::InvalidError unless valid?

                @nodes[symbol].try(:value)
              end

              (@allowed_nodes ||= {}).merge!(symbol => factory)
            end
          end
        end
      end
    end
  end
end