Commit 1c2530de authored by Grzegorz Bizon's avatar Grzegorz Bizon

Expose evaluable variables method from build object

parent bf59ed3c
...@@ -244,13 +244,24 @@ module Ci ...@@ -244,13 +244,24 @@ module Ci
Gitlab::Utils.slugify(ref.to_s) Gitlab::Utils.slugify(ref.to_s)
end end
##
# Variables whose value does not depend on environment # Variables whose value does not depend on environment
#
def simple_variables def simple_variables
variables(environment: nil) variables(environment: nil)
end end
# All variables, including those dependent on environment, which could ##
# Variables that are available for evaluation using variables policy.
#
def evaluable_variables
Gitlab::Ci::Variables::Collection.new
.concat(simple_variables)
end
## All variables, including those dependent on environment, which could
# contain unexpanded variables. # contain unexpanded variables.
#
def variables(environment: persisted_environment) def variables(environment: persisted_environment)
collection = Gitlab::Ci::Variables::Collection.new.tap do |variables| collection = Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.concat(predefined_variables) variables.concat(predefined_variables)
......
...@@ -8,9 +8,8 @@ module Gitlab ...@@ -8,9 +8,8 @@ module Gitlab
end end
def satisfied_by?(pipeline, seed) def satisfied_by?(pipeline, seed)
variables = Gitlab::Ci::Variables::Collection variables = seed.to_resource
.new(seed.to_resource.simple_variables) .evaluable_variables.to_hash
.to_hash
statements = @expressions.map do |statement| statements = @expressions.map do |statement|
::Gitlab::Ci::Pipeline::Expression::Statement ::Gitlab::Ci::Pipeline::Expression::Statement
......
...@@ -6,7 +6,8 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do ...@@ -6,7 +6,8 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do
let(:pipeline) do let(:pipeline) do
build(:ci_pipeline_with_one_job, project: project, build(:ci_pipeline_with_one_job, project: project,
ref: 'master') ref: 'master',
user: user)
end end
let(:command) do let(:command) do
...@@ -42,6 +43,10 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do ...@@ -42,6 +43,10 @@ describe Gitlab::Ci::Pipeline::Chain::Populate do
expect(pipeline.stages.first.builds).to be_one expect(pipeline.stages.first.builds).to be_one
expect(pipeline.stages.first.builds.first).not_to be_persisted expect(pipeline.stages.first.builds.first).not_to be_persisted
end end
it 'correctly assigns user' do
expect(pipeline.builds).to all(have_attributes(user: user))
end
end end
context 'when pipeline is empty' do context 'when pipeline is empty' do
......
...@@ -1978,6 +1978,27 @@ describe Ci::Build do ...@@ -1978,6 +1978,27 @@ describe Ci::Build do
end end
end end
context 'when build has not been persisted yet' do
let(:build) do
described_class.new(
name: 'rspec',
stage: 'test',
ref: 'feature',
project: project,
pipeline: pipeline
)
end
it 'returns static predefined variables' do
expect(build.variables.size).to be >= 28
expect(build.variables)
.to include(key: 'CI_COMMIT_REF_NAME', value: 'feature', public: true)
expect(build).not_to be_persisted
end
end
end
describe '#evaluable_variables' do
context 'when build has not been persisted yet' do context 'when build has not been persisted yet' do
let(:build) do let(:build) do
described_class.new( described_class.new(
...@@ -1993,9 +2014,8 @@ describe Ci::Build do ...@@ -1993,9 +2014,8 @@ describe Ci::Build do
expect(build).to be_valid expect(build).to be_valid
expect(build).not_to be_persisted expect(build).not_to be_persisted
variables = build.variables variables = build.evaluable_variables
expect(variables.size).to be >= 28
expect(build).not_to be_persisted expect(build).not_to be_persisted
end end
...@@ -2006,13 +2026,14 @@ describe Ci::Build do ...@@ -2006,13 +2026,14 @@ describe Ci::Build do
CI_COMMIT_REF_SLUG CI_COMMIT_REF_SLUG
CI_JOB_STAGE] CI_JOB_STAGE]
build.variables.map { |var| var.fetch(:key) }.tap do |names| variables = build.evaluable_variables
variables.map { |env| env[:key] }.tap do |names|
expect(names).to include(*keys) expect(names).to include(*keys)
end end
expect(build.variables).to include(key: 'CI_COMMIT_REF_NAME', expect(variables)
value: 'feature', .to include(key: 'CI_COMMIT_REF_NAME', value: 'feature', public: true)
public: true)
end end
it 'does not return prohibited variables' do it 'does not return prohibited variables' do
...@@ -2025,7 +2046,7 @@ describe Ci::Build do ...@@ -2025,7 +2046,7 @@ describe Ci::Build do
CI_REPOSITORY_URL CI_REPOSITORY_URL
CI_ENVIRONMENT_URL] CI_ENVIRONMENT_URL]
build.variables.map { |var| var.fetch(:key) }.tap do |names| build.evaluable_variables.map { |env| env[:key] }.tap do |names|
expect(names).not_to include(*keys) expect(names).not_to include(*keys)
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