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/interpolation/context.rb')
-rw-r--r--lib/gitlab/ci/interpolation/context.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/gitlab/ci/interpolation/context.rb b/lib/gitlab/ci/interpolation/context.rb
new file mode 100644
index 00000000000..ce7a86a3c9b
--- /dev/null
+++ b/lib/gitlab/ci/interpolation/context.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Ci
+ module Interpolation
+ ##
+ # Interpolation::Context is a class that represents the data that can be used when performing string interpolation
+ # on a CI configuration.
+ #
+ class Context
+ ContextTooComplexError = Class.new(StandardError)
+ NotSymbolizedContextError = Class.new(StandardError)
+
+ MAX_DEPTH = 3
+
+ def initialize(hash)
+ @context = hash
+
+ raise ContextTooComplexError if depth > MAX_DEPTH
+ end
+
+ def valid?
+ errors.none?
+ end
+
+ ##
+ # This method is here because `Context` will be responsible for validating specs, inputs and defaults.
+ #
+ def errors
+ []
+ end
+
+ def depth
+ deep_depth(@context)
+ end
+
+ def fetch(field)
+ @context.fetch(field)
+ end
+
+ def to_h
+ @context.to_h
+ end
+
+ private
+
+ def deep_depth(context, depth = 0)
+ values = context.values.map do |value|
+ if value.is_a?(Hash)
+ deep_depth(value, depth + 1)
+ else
+ depth + 1
+ end
+ end
+
+ values.max
+ end
+
+ def self.fabricate(context)
+ case context
+ when Hash
+ new(context)
+ when Interpolation::Context
+ context
+ else
+ raise ArgumentError, 'unknown interpolation context'
+ end
+ end
+ end
+ end
+ end
+end