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

inputs.rb « input « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 743ae2ecf1eb456973677d8d4c70b4bc4c795cb8 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Input
      ##
      # Inputs::Input class represents user-provided inputs, configured using `with:` keyword.
      #
      # Input arguments are only valid with an associated component's inputs specification from component's header.
      #
      class Inputs
        UnknownSpecArgumentError = Class.new(StandardError)

        ARGUMENTS = [
          Input::Arguments::Required, # Input argument is required
          Input::Arguments::Default,  # Input argument has a default value
          Input::Arguments::Options,  # Input argument that needs to be allowlisted
          Input::Arguments::Unknown   # Input argument has not been recognized
        ].freeze

        def initialize(spec, args)
          @spec = spec
          @args = args
          @inputs = []
          @errors = []

          validate!
          fabricate!
        end

        def errors
          @errors + @inputs.flat_map(&:errors)
        end

        def valid?
          errors.none?
        end

        def unknown
          @args.keys - @spec.keys
        end

        def count
          @inputs.count
        end

        def to_hash
          @inputs.inject({}) do |hash, argument|
            raise ArgumentError unless argument.valid?

            hash.merge(argument.to_hash)
          end
        end

        private

        def validate!
          @errors.push("unknown input arguments: #{unknown.inspect}") if unknown.any?
        end

        def fabricate!
          @spec.each do |key, spec|
            argument = ARGUMENTS.find { |klass| klass.matches?(spec) }

            raise UnknownSpecArgumentError if argument.nil?

            @inputs.push(argument.new(key, spec, @args[key]))
          end
        end
      end
    end
  end
end