Commit c72e21fd authored by Grzegorz Bizon's avatar Grzegorz Bizon

Return stage seeds object from YAML processor

parent c881425b
...@@ -295,14 +295,16 @@ module Ci ...@@ -295,14 +295,16 @@ module Ci
sort_by { |build| build[:stage_idx] } sort_by { |build| build[:stage_idx] }
end end
def config_stages_attributes def stage_seeds
return [] unless config_processor return [] unless config_processor
config_processor.stages_for_ref(ref, tag?, trigger_requests.first) config_processor.stage_seeds(ref: ref,
tag: tag?,
trigger: trigger_requests.first)
end end
def has_stages? def has_stages?
config_stages_attributes.any? stage_seeds.has_stages?
end end
def has_warnings? def has_warnings?
......
...@@ -50,14 +50,14 @@ module Ci ...@@ -50,14 +50,14 @@ module Ci
end end
end end
def stages_for_ref(ref, tag = false, trigger_request = nil) def stage_seeds(ref:, tag: false, trigger: nil)
stages = @stages.uniq.map do |stage| Gitlab::Ci::Stage::Seeds.new.tap do |seeds|
builds = builds_for_stage_and_ref(stage, ref, tag, trigger_request) @stages.uniq.each do |stage|
builds = builds_for_stage_and_ref(stage, ref, tag, trigger)
{ name: stage, builds_attributes: builds.to_a } if builds.any? seeds.append_stage(stage, builds) if builds.any?
end
end end
stages.compact.sort_by { |stage| @stages.index(stage[:name]) }
end end
def build_attributes(name) def build_attributes(name)
......
...@@ -8,6 +8,10 @@ module Gitlab ...@@ -8,6 +8,10 @@ module Gitlab
@stages = [] @stages = []
end end
def has_stages?
@stages.any?
end
def stages def stages
@stages.map(&:stage) @stages.map(&:stage)
end end
...@@ -48,7 +52,7 @@ module Gitlab ...@@ -48,7 +52,7 @@ module Gitlab
end end
def to_attributes def to_attributes
@stages.map.with_index do |seed| @stages.map do |seed|
seed.stage.merge(builds_attributes: seed.jobs) seed.stage.merge(builds_attributes: seed.jobs)
end end
end end
......
...@@ -83,7 +83,7 @@ module Ci ...@@ -83,7 +83,7 @@ module Ci
end end
end end
describe '#stages_for_ref' do describe '#stage_seeds' do
context 'when no refs policy is specified' do context 'when no refs policy is specified' do
let(:config) do let(:config) do
YAML.dump(production: { stage: 'deploy', script: 'cap prod' }, YAML.dump(production: { stage: 'deploy', script: 'cap prod' },
...@@ -91,15 +91,15 @@ module Ci ...@@ -91,15 +91,15 @@ module Ci
spinach: { stage: 'test', script: 'spinach' }) spinach: { stage: 'test', script: 'spinach' })
end end
it 'returns model attributes for stages with nested jobs' do it 'returns correctly fabricated stage seeds object' do
attributes = subject.stages_for_ref('master') seeds = subject.stage_seeds(ref: 'master')
expect(attributes.size).to eq 2 expect(seeds.stages.size).to eq 2
expect(attributes.dig(0, :name)).to eq 'test' expect(seeds.stages.dig(0, :name)).to eq 'test'
expect(attributes.dig(1, :name)).to eq 'deploy' expect(seeds.stages.dig(1, :name)).to eq 'deploy'
expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'rspec' expect(seeds.jobs.dig(0, :name)).to eq 'rspec'
expect(attributes.dig(0, :builds_attributes, 1, :name)).to eq 'spinach' expect(seeds.jobs.dig(1, :name)).to eq 'spinach'
expect(attributes.dig(1, :builds_attributes, 0, :name)).to eq 'production' expect(seeds.jobs.dig(2, :name)).to eq 'production'
end end
end end
...@@ -109,14 +109,12 @@ module Ci ...@@ -109,14 +109,12 @@ module Ci
spinach: { stage: 'test', script: 'spinach', only: ['tags'] }) spinach: { stage: 'test', script: 'spinach', only: ['tags'] })
end end
it 'returns stage attributes except of jobs assigned to master' do it 'returns stage seeds only assigned to master to master' do
# true flag argument means matching jobs for tags seeds = subject.stage_seeds(ref: 'feature', tag: true)
#
attributes = subject.stages_for_ref('feature', true)
expect(attributes.size).to eq 1 expect(seeds.stages.size).to eq 1
expect(attributes.dig(0, :name)).to eq 'test' expect(seeds.stages.dig(0, :name)).to eq 'test'
expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'spinach' expect(seeds.jobs.dig(0, :name)).to eq 'spinach'
end end
end end
end end
......
...@@ -6,6 +6,10 @@ describe Gitlab::Ci::Stage::Seeds do ...@@ -6,6 +6,10 @@ describe Gitlab::Ci::Stage::Seeds do
subject.append_stage('deploy', [{ name: 'prod', script: 'cap deploy' }]) subject.append_stage('deploy', [{ name: 'prod', script: 'cap deploy' }])
end end
describe '#has_stages?' do
it { is_expected.to have_stages }
end
describe '#stages' do describe '#stages' do
it 'returns hashes of all stages' do it 'returns hashes of all stages' do
expect(subject.stages.size).to eq 2 expect(subject.stages.size).to eq 2
......
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