blob: d064a520921fbdb6012a59bda00afb84405da974 (
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
|
# frozen_string_literal: true
module Gitlab
module Ci
module Interpolation
##
# This class represents an interpolation block. The format supported is:
# $[[ <access> | <function1> | <function2> | ... <functionN> ]]
#
# <access> specifies the value to retrieve (e.g. `inputs.key`).
# <function> can be optionally provided with or without arguments to
# manipulate the access value. Functions are evaluated in the order
# they are presented.
class Block
PREFIX = '$[['
PATTERN = /(?<block>\$\[\[\s*(?<data>.*?)\s*\]\])/.freeze
MAX_FUNCTIONS = 3
attr_reader :block, :data, :ctx, :errors
def initialize(block, data, ctx)
@block = block
@data = data
@ctx = ctx
@errors = []
@value = nil
evaluate!
end
def valid?
errors.none?
end
def content
data
end
def value
raise ArgumentError, 'block invalid' unless valid?
@value
end
def self.match(data)
return data unless data.is_a?(String) && data.include?(PREFIX)
data.gsub(PATTERN) do
yield ::Regexp.last_match(1), ::Regexp.last_match(2)
end
end
private
# We expect the block data to be a string with one or more entities delimited by pipes:
# <access> | <function1> | <function2> | ... <functionN>
def evaluate!
data_access, *functions = data.split('|').map(&:strip)
access = Interpolation::Access.new(data_access, ctx)
return @errors.concat(access.errors) unless access.valid?
return @errors.push('too many functions in interpolation block') if functions.count > MAX_FUNCTIONS
result = Interpolation::FunctionsStack.new(functions).evaluate(access.value)
if result.success?
@value = result.value
else
@errors.concat(result.errors)
end
end
end
end
end
end
|