summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/product/parallel.rb
blob: 5c78a8f68c74d948f5dcc10224c617e6fecbee06 (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 Entry
        ##
        # Entry that represents a parallel job config.
        #
        module Product
          class Parallel < ::Gitlab::Config::Entry::Simplifiable
            strategy :ParallelBuilds, if: -> (config) { config.is_a?(Numeric) }
            strategy :MatrixBuilds, if: -> (config) { config.is_a?(Hash) }

            PARALLEL_LIMIT = 50

            class ParallelBuilds < ::Gitlab::Config::Entry::Node
              include ::Gitlab::Config::Entry::Validatable

              validations do
                validates :config, numericality: { only_integer: true,
                                                   greater_than_or_equal_to: 2,
                                                   less_than_or_equal_to: Entry::Product::Parallel::PARALLEL_LIMIT },
                                   allow_nil: true

                validate do
                  next unless opt(:allowed_strategies)
                  next if opt(:allowed_strategies).include?(:numeric)

                  errors.add(:config, 'cannot use "parallel: <number>".')
                end
              end

              def value
                { number: super.to_i }
              end
            end

            class MatrixBuilds < ::Gitlab::Config::Entry::Node
              include ::Gitlab::Config::Entry::Attributable
              include ::Gitlab::Config::Entry::Configurable

              PERMITTED_KEYS = %i[matrix].freeze

              validations do
                validates :config, allowed_keys: PERMITTED_KEYS
                validates :config, required_keys: PERMITTED_KEYS

                validate do
                  next unless opt(:allowed_strategies)
                  next if opt(:allowed_strategies).include?(:matrix)

                  errors.add(:config, 'cannot use "parallel: matrix".')
                end
              end

              entry :matrix, Entry::Product::Matrix,
                description: 'Variables definition for matrix builds'
            end

            class UnknownStrategy < ::Gitlab::Config::Entry::Node
              def errors
                ["#{location} should be an integer or a hash"]
              end
            end
          end
        end
      end
    end
  end
end