summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/trigger.rb
blob: 7202784842a44abad5a8dbc65d0622efb43b3494 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents a cross-project downstream trigger.
        #
        class Trigger < ::Gitlab::Config::Entry::Simplifiable
          strategy :SimpleTrigger, if: -> (config) { config.is_a?(String) }
          strategy :ComplexTrigger, if: -> (config) { config.is_a?(Hash) }

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

            validations { validates :config, presence: true }

            def value
              { project: @config }
            end
          end

          class ComplexTrigger < ::Gitlab::Config::Entry::Simplifiable
            strategy :CrossProjectTrigger, if: -> (config) { !config.key?(:include) }

            strategy :SameProjectTrigger, if: -> (config) do
              ::Feature.enabled?(:ci_parent_child_pipeline, default_enabled: true) &&
                config.key?(:include)
            end

            class CrossProjectTrigger < ::Gitlab::Config::Entry::Node
              include ::Gitlab::Config::Entry::Validatable
              include ::Gitlab::Config::Entry::Attributable

              ALLOWED_KEYS = %i[project branch strategy].freeze
              attributes :project, :branch, :strategy

              validations do
                validates :config, presence: true
                validates :config, allowed_keys: ALLOWED_KEYS
                validates :project, presence: true
                validates :branch, type: String, allow_nil: true
                validates :strategy, type: String, inclusion: { in: %w[depend], message: 'should be depend' }, allow_nil: true
              end
            end

            class SameProjectTrigger < ::Gitlab::Config::Entry::Node
              include ::Gitlab::Config::Entry::Validatable
              include ::Gitlab::Config::Entry::Attributable
              include ::Gitlab::Config::Entry::Configurable

              INCLUDE_MAX_SIZE = 3
              ALLOWED_KEYS = %i[strategy include].freeze
              attributes :strategy

              validations do
                validates :config, presence: true
                validates :config, allowed_keys: ALLOWED_KEYS
                validates :strategy, type: String, inclusion: { in: %w[depend], message: 'should be depend' }, allow_nil: true
              end

              entry :include, ::Gitlab::Ci::Config::Entry::Includes,
                description: 'List of external YAML files to include.',
                reserved: true,
                metadata: { max_size: INCLUDE_MAX_SIZE }

              def value
                @config
              end
            end

            class UnknownStrategy < ::Gitlab::Config::Entry::Node
              def errors
                if ::Feature.enabled?(:ci_parent_child_pipeline, default_enabled: true)
                  ['config must specify either project or include']
                else
                  ['config must specify project']
                end
              end
            end
          end

          class UnknownStrategy < ::Gitlab::Config::Entry::Node
            def errors
              ["#{location} has to be either a string or a hash"]
            end
          end
        end
      end
    end
  end
end