Commit b5bd74f8 authored by Pedro Pombeiro's avatar Pedro Pombeiro

Address MR review comments

parent 250fede4
...@@ -34,11 +34,7 @@ module ExpandVariables ...@@ -34,11 +34,7 @@ module ExpandVariables
end end
def match_or_blank_value(variables, last_match) def match_or_blank_value(variables, last_match)
ref_var_name = last_match[1] || last_match[2] variables[last_match[1] || last_match[2]]
ref_var = variables[ref_var_name]
return ref_var if ref_var.is_a?(String) # if entry is a simple "key" => "value" hash
ref_var[:value] if ref_var
end end
def match_or_original_value(variables, last_match) def match_or_original_value(variables, last_match)
...@@ -52,10 +48,10 @@ module ExpandVariables ...@@ -52,10 +48,10 @@ module ExpandVariables
# Convert Collection to variables # Convert Collection to variables
variables = variables.to_hash if variables.is_a?(Gitlab::Ci::Variables::Collection) variables = variables.to_hash if variables.is_a?(Gitlab::Ci::Variables::Collection)
# Convert hash array to hash of variables # Convert hash array to variables
if variables.is_a?(Array) if variables.is_a?(Array)
variables = variables.reduce({}) do |hash, variable| variables = variables.reduce({}) do |hash, variable|
hash[variable[:key]] = variable hash[variable[:key]] = variable[:value]
hash hash
end end
end end
......
...@@ -162,7 +162,7 @@ module Gitlab ...@@ -162,7 +162,7 @@ module Gitlab
end end
def variable_expansion_errors def variable_expansion_errors
expanded_collection = evaluate_context.variables.expand_all(@pipeline.project) expanded_collection = evaluate_context.variables.sort_and_expand_all(@pipeline.project)
errors = expanded_collection.errors errors = expanded_collection.errors
["#{name}: #{errors}"] if errors ["#{name}: #{errors}"] if errors
end end
......
...@@ -72,7 +72,7 @@ module Gitlab ...@@ -72,7 +72,7 @@ module Gitlab
end end
end end
def expand_all(project, keep_undefined: false) def sort_and_expand_all(project, keep_undefined: false)
return self if Feature.disabled?(:variable_inside_variable, project) return self if Feature.disabled?(:variable_inside_variable, project)
sorted = Sort.new(self) sorted = Sort.new(self)
......
...@@ -29,7 +29,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -29,7 +29,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ {
"empty array": { "empty array": {
variables: [], variables: [],
validation_result: nil expected_errors: nil
}, },
"simple expansions": { "simple expansions": {
variables: [ variables: [
...@@ -37,7 +37,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -37,7 +37,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable2', value: 'result' }, { key: 'variable2', value: 'result' },
{ key: 'variable3', value: 'key$variable$variable2' } { key: 'variable3', value: 'key$variable$variable2' }
], ],
validation_result: nil expected_errors: nil
}, },
"cyclic dependency": { "cyclic dependency": {
variables: [ variables: [
...@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable2', value: '$variable3' }, { key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2' } { key: 'variable3', value: 'key$variable$variable2' }
], ],
validation_result: 'circular variable reference detected: ["variable", "variable2", "variable3"]' expected_errors: 'circular variable reference detected: ["variable", "variable2", "variable3"]'
}, },
"array with raw variable": { "array with raw variable": {
variables: [ variables: [
...@@ -53,7 +53,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -53,7 +53,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable2', value: '$variable3' }, { key: 'variable2', value: '$variable3' },
{ key: 'variable3', value: 'key$variable$variable2', raw: true } { key: 'variable3', value: 'key$variable$variable2', raw: true }
], ],
validation_result: nil expected_errors: nil
}, },
"variable containing escaped variable reference": { "variable containing escaped variable reference": {
variables: [ variables: [
...@@ -61,7 +61,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -61,7 +61,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
{ key: 'variable_b', value: '$$variable_a' }, { key: 'variable_b', value: '$$variable_a' },
{ key: 'variable_c', value: '$variable_b' } { key: 'variable_c', value: '$variable_b' }
], ],
validation_result: nil expected_errors: nil
} }
} }
end end
...@@ -71,12 +71,12 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -71,12 +71,12 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
subject { Gitlab::Ci::Variables::Collection::Sort.new(collection) } subject { Gitlab::Ci::Variables::Collection::Sort.new(collection) }
it 'errors matches expected validation result' do it 'errors matches expected errors' do
expect(subject.errors).to eq(validation_result) expect(subject.errors).to eq(expected_errors)
end end
it 'valid? matches expected validation result' do it 'valid? matches expected errors' do
expect(subject.valid?).to eq(validation_result.nil?) expect(subject.valid?).to eq(expected_errors.nil?)
end end
it 'does not raise' do it 'does not raise' do
...@@ -159,7 +159,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do ...@@ -159,7 +159,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection::Sort do
subject { Gitlab::Ci::Variables::Collection::Sort.new(collection).tsort } subject { Gitlab::Ci::Variables::Collection::Sort.new(collection).tsort }
it 'returns correctly sorted variables' do it 'returns correctly sorted variables' do
expect(subject.map { |var| var[:key] }).to eq(result) expect(subject.pluck(:key)).to eq(result)
end end
end end
end end
......
...@@ -243,7 +243,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ...@@ -243,7 +243,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
end end
end end
describe '#expand_all' do describe '#sort_and_expand_all' do
context 'when FF :variable_inside_variable is disabled' do context 'when FF :variable_inside_variable is disabled' do
let_it_be(:project_with_flag_disabled) { create(:project) } let_it_be(:project_with_flag_disabled) { create(:project) }
let_it_be(:project_with_flag_enabled) { create(:project) } let_it_be(:project_with_flag_enabled) { create(:project) }
...@@ -305,15 +305,14 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ...@@ -305,15 +305,14 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
with_them do with_them do
let(:collection) { Gitlab::Ci::Variables::Collection.new(variables, keep_undefined: keep_undefined) } let(:collection) { Gitlab::Ci::Variables::Collection.new(variables, keep_undefined: keep_undefined) }
subject { collection.expand_all(project_with_flag_disabled) } subject { collection.sort_and_expand_all(project_with_flag_disabled) }
it 'returns Collection' do it 'returns Collection' do
is_expected.to be_an_instance_of(Gitlab::Ci::Variables::Collection) is_expected.to be_an_instance_of(Gitlab::Ci::Variables::Collection)
end end
it 'does not expand variables' do it 'does not expand variables' do
var_hash = variables.to_h { |env| [env.fetch(:key), env.fetch(:value)] } var_hash = variables.pluck(:key, :value).to_h
.with_indifferent_access
expect(subject.to_hash).to eq(var_hash) expect(subject.to_hash).to eq(var_hash)
end end
end end
...@@ -481,7 +480,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ...@@ -481,7 +480,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
with_them do with_them do
let(:collection) { Gitlab::Ci::Variables::Collection.new(variables) } let(:collection) { Gitlab::Ci::Variables::Collection.new(variables) }
subject { collection.expand_all(project_with_flag_enabled, keep_undefined: keep_undefined) } subject { collection.sort_and_expand_all(project_with_flag_enabled, keep_undefined: keep_undefined) }
it 'returns Collection' do it 'returns Collection' do
is_expected.to be_an_instance_of(Gitlab::Ci::Variables::Collection) is_expected.to be_an_instance_of(Gitlab::Ci::Variables::Collection)
...@@ -494,11 +493,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do ...@@ -494,11 +493,7 @@ RSpec.describe Gitlab::Ci::Variables::Collection do
end end
it 'preserves raw attribute' do it 'preserves raw attribute' do
collection.each do |v| expect(subject.pluck(:key, :raw).to_h).to eq(collection.pluck(:key, :raw).to_h)
k = v[:key]
subject_item = subject.find { |sv| sv[:key] == k }
expect(subject_item.raw).to eq(v.raw)
end
end end
end end
end 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