retry.rb 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents a retry config for a job.
        #
        class Retry < Simplifiable
          strategy :SimpleRetry, if: -> (config) { config.is_a?(Integer) }
          strategy :FullRetry, if: -> (config) { config.is_a?(Hash) }

          class SimpleRetry < Entry::Node
            include Entry::Validatable

            validations do
              validates :config, numericality: { only_integer: true,
                                                 greater_than_or_equal_to: 0,
                                                 less_than_or_equal_to: 2 }
            end
M
Markus Doits 已提交
20

M
Markus Doits 已提交
21 22 23 24 25 26
            def value
              {
                max: config
              }
            end

M
Markus Doits 已提交
27 28 29
            def location
              'retry'
            end
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
          end

          class FullRetry < Entry::Node
            include Entry::Validatable
            include Entry::Attributable

            ALLOWED_KEYS = %i[max when].freeze
            attributes :max, :when

            validations do
              validates :config, allowed_keys: ALLOWED_KEYS

              with_options allow_nil: true do
                validates :max, numericality: { only_integer: true,
                                                greater_than_or_equal_to: 0,
                                                less_than_or_equal_to: 2 }

                validates :when, array_of_strings_or_string: true
                validates :when,
                          allowed_array_values: { in: FullRetry.possible_retry_when_values },
                          if: -> (config) { config.when.is_a?(Array) }
                validates :when,
                          inclusion: { in: FullRetry.possible_retry_when_values },
                          if: -> (config) { config.when.is_a?(String) }
              end
            end

            def self.possible_retry_when_values
M
Markus Doits 已提交
58 59 60 61 62 63 64 65 66
              @possible_retry_when_values ||= CommitStatus.failure_reasons.keys.map(&:to_s) + ['always']
            end

            def value
              super.tap do |config|
                # make sure that `when` is an array, because we allow it to
                # be passed as a String in config for simplicity
                config[:when] = Array.wrap(config[:when]) if config[:when]
              end
67
            end
M
Markus Doits 已提交
68 69 70 71

            def location
              'retry'
            end
72 73 74 75 76 77
          end

          class UnknownStrategy < Entry::Node
            def errors
              ["#{location} has to be either an integer or a hash"]
            end
M
Markus Doits 已提交
78 79 80 81

            def location
              'retry config'
            end
82 83 84 85 86 87 88 89 90
          end

          def self.default
          end
        end
      end
    end
  end
end