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

base_input.rb « inputs « interpolation « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5648c4d31eaab35f35eb7692f8733106b35ecacf (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
94
95
96
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Interpolation
        class Inputs
          ##
          # This is a common abstraction for all input types
          class BaseInput
            ArgumentNotValidError = Class.new(StandardError)

            # Checks whether the class matches the type in the specification
            def self.matches?(spec)
              raise NotImplementedError
            end

            # Human readable type used in error messages
            def self.type_name
              raise NotImplementedError
            end

            # Checks whether the provided value is of the given type
            def valid_value?(value)
              raise NotImplementedError
            end

            attr_reader :errors, :name, :spec, :value

            def initialize(name:, spec:, value:)
              @name = name
              @errors = []

              # Treat minimal spec definition (nil) as a valid hash:
              #   spec:
              #     inputs:
              #       website:
              @spec = spec || {} # specification from input definition
              @value = value     # actual value provided by the user

              validate!
            end

            def to_hash
              raise ArgumentNotValidError unless valid?

              { name => actual_value }
            end

            def valid?
              @errors.none?
            end

            private

            def validate!
              return error('required value has not been provided') if required_input? && value.nil?

              # validate default value
              if !required_input? && !valid_value?(default)
                return error("default value is not a #{self.class.type_name}")
              end

              # validate provided value
              error("provided value is not a #{self.class.type_name}") unless valid_value?(actual_value)
            end

            def error(message)
              @errors.push("`#{name}` input: #{message}")
            end

            def actual_value
              # nil check is to support boolean values.
              value.nil? ? default : value
            end

            # An input specification without a default value is required.
            # For example:
            # ```yaml
            # spec:
            #   inputs:
            #     website:
            # ```
            def required_input?
              !spec.key?(:default)
            end

            def default
              spec[:default]
            end
          end
        end
      end
    end
  end
end