Commit da42ee6f authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'pedropombeiro/26345/5-add-raw-attribute-to-item' into 'master'

Add :raw attribute to Collection::Item

See merge request gitlab-org/gitlab!54506
parents 20042768 d38b8b80
......@@ -7,11 +7,14 @@ module Gitlab
class Item
include Gitlab::Utils::StrongMemoize
def initialize(key:, value:, public: true, file: false, masked: false)
attr_reader :raw
def initialize(key:, value:, public: true, file: false, masked: false, raw: false)
raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless
value.is_a?(String) || value.nil?
@variable = { key: key, value: value, public: public, file: file, masked: masked }
@raw = raw
end
def value
......@@ -28,6 +31,8 @@ module Gitlab
def depends_on
strong_memoize(:depends_on) do
next if raw
next unless ExpandVariables.possible_var_reference?(value)
value.scan(ExpandVariables::VARIABLES_REGEXP).map(&:first)
......
......@@ -92,6 +92,10 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do
variable: { key: 'VAR', value: 'something_${VAR2}_$VAR3' },
expected_depends_on: %w(VAR2 VAR3)
},
"complex expansion in raw variable": {
variable: { key: 'VAR', value: 'something_${VAR2}_$VAR3', raw: true },
expected_depends_on: nil
},
"complex expansions for Windows": {
variable: { key: 'variable3', value: 'key%variable%%variable2%' },
expected_depends_on: %w(variable variable2)
......@@ -156,6 +160,26 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do
end
end
describe '#raw' do
it 'returns false when :raw is not specified' do
item = described_class.new(**variable)
expect(item.raw).to eq false
end
context 'when :raw is specified as true' do
let(:variable) do
{ key: variable_key, value: variable_value, public: true, masked: false, raw: true }
end
it 'returns true' do
item = described_class.new(**variable)
expect(item.raw).to eq true
end
end
end
describe '#to_runner_variable' do
context 'when variable is not a file-related' do
it 'returns a runner-compatible hash representation' do
......@@ -185,5 +209,19 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Item do
expect(runner_variable.depends_on).to eq(%w(CI_VAR_2 CI_VAR_3))
end
end
context 'when assigned the raw attribute' do
it 'retains a true raw attribute' do
runner_variable = described_class.new(key: 'CI_VAR', value: '123', raw: true)
expect(runner_variable).to eq(key: 'CI_VAR', value: '123', public: true, masked: false, raw: true)
end
it 'does not retain a false raw attribute' do
runner_variable = described_class.new(key: 'CI_VAR', value: '123', raw: false)
expect(runner_variable).to eq(key: 'CI_VAR', value: '123', public: true, masked: false)
end
end
end
end
......@@ -76,6 +76,13 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2' }
]
},
"array with raw variable": {
variables: [
{ key: 'variable', value: '$variable2' },
{ key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2', raw: true }
]
}
}
end
......@@ -128,6 +135,14 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable3', value: 'key$variable$variable2' }
],
validation_result: 'circular variable reference detected: ["variable", "variable2", "variable3"]'
},
"array with raw variable": {
variables: [
{ key: 'variable', value: '$variable2' },
{ key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2', raw: true }
],
validation_result: nil
}
}
end
......@@ -271,6 +286,22 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable', value: '$variable2' }
],
result: %w[variable2 variable3 variable]
},
"raw variable does not get resolved": {
variables: [
{ key: 'variable', value: '$variable2' },
{ key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2', raw: true }
],
result: %w[variable3 variable2 variable]
},
"variable containing escaped variable reference": {
variables: [
{ key: 'variable_c', value: '$variable_b' },
{ key: 'variable_b', value: '$$variable_a' },
{ key: 'variable_a', value: 'value' }
],
result: %w[variable_a variable_b variable_c]
}
}
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment