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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/ci/config/interpolation/inputs.rb')
-rw-r--r--lib/gitlab/ci/config/interpolation/inputs.rb78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/gitlab/ci/config/interpolation/inputs.rb b/lib/gitlab/ci/config/interpolation/inputs.rb
new file mode 100644
index 00000000000..0fd3238d503
--- /dev/null
+++ b/lib/gitlab/ci/config/interpolation/inputs.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ class Config
+ module Interpolation
+ # Interpolation inputs provided by the user.
+ class Inputs
+ UnknownInputTypeError = Class.new(StandardError)
+
+ TYPES = [
+ BooleanInput,
+ NumberInput,
+ StringInput
+ ].freeze
+
+ def self.input_types
+ TYPES.map(&:type_name)
+ end
+
+ def initialize(specs, args)
+ @specs = specs.to_h
+ @args = args.to_h
+ @inputs = []
+ @errors = []
+
+ validate!
+ fabricate!
+ end
+
+ def errors
+ @errors + @inputs.flat_map(&:errors)
+ end
+
+ def valid?
+ errors.none?
+ end
+
+ def to_hash
+ @inputs.inject({}) do |hash, input|
+ hash.merge(input.to_hash)
+ end
+ end
+
+ private
+
+ def validate!
+ unknown_inputs = @args.keys - @specs.keys
+ return if unknown_inputs.empty?
+
+ @errors.push("unknown input arguments: #{unknown_inputs.join(', ')}")
+ end
+
+ def fabricate!
+ @specs.each do |input_name, spec|
+ input_type = TYPES.find { |klass| klass.matches?(spec) }
+
+ unless input_type
+ @errors.push(
+ "unknown input specification for `#{input_name}` (valid types: #{valid_type_names.join(', ')})")
+ next
+ end
+
+ @inputs.push(input_type.new(
+ name: input_name,
+ spec: spec,
+ value: @args[input_name]))
+ end
+ end
+
+ def valid_type_names
+ TYPES.map(&:type_name)
+ end
+ end
+ end
+ end
+ end
+end