diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-05-31 15:13:40 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-05-31 15:13:40 +0200 |
commit | c72e21fd9764845a107005562ff8ce1c06cac431 (patch) | |
tree | 1acd707d049036bd532046b91e5f25d55633744e | |
parent | c881425b665b9c0b022dc2e213486aecc320ec7e (diff) | |
download | gitlab-ce-c72e21fd9764845a107005562ff8ce1c06cac431.tar.gz |
Return stage seeds object from YAML processor
-rw-r--r-- | app/models/ci/pipeline.rb | 8 | ||||
-rw-r--r-- | lib/ci/gitlab_ci_yaml_processor.rb | 12 | ||||
-rw-r--r-- | lib/gitlab/ci/stage/seeds.rb | 6 | ||||
-rw-r--r-- | spec/lib/ci/gitlab_ci_yaml_processor_spec.rb | 30 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/stage/seeds_spec.rb | 4 |
5 files changed, 34 insertions, 26 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 7298f676a0e..d5b6da4eee6 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -295,14 +295,16 @@ module Ci sort_by { |build| build[:stage_idx] } end - def config_stages_attributes + def stage_seeds 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 def has_stages? - config_stages_attributes.any? + stage_seeds.has_stages? end def has_warnings? diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb index 5e6c3d029c6..4b0e87a945b 100644 --- a/lib/ci/gitlab_ci_yaml_processor.rb +++ b/lib/ci/gitlab_ci_yaml_processor.rb @@ -50,14 +50,14 @@ module Ci end end - def stages_for_ref(ref, tag = false, trigger_request = nil) - stages = @stages.uniq.map do |stage| - builds = builds_for_stage_and_ref(stage, ref, tag, trigger_request) + def stage_seeds(ref:, tag: false, trigger: nil) + Gitlab::Ci::Stage::Seeds.new.tap do |seeds| + @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 - - stages.compact.sort_by { |stage| @stages.index(stage[:name]) } end def build_attributes(name) diff --git a/lib/gitlab/ci/stage/seeds.rb b/lib/gitlab/ci/stage/seeds.rb index 16c436b5d4f..cb5174a166b 100644 --- a/lib/gitlab/ci/stage/seeds.rb +++ b/lib/gitlab/ci/stage/seeds.rb @@ -8,6 +8,10 @@ module Gitlab @stages = [] end + def has_stages? + @stages.any? + end + def stages @stages.map(&:stage) end @@ -48,7 +52,7 @@ module Gitlab end def to_attributes - @stages.map.with_index do |seed| + @stages.map do |seed| seed.stage.merge(builds_attributes: seed.jobs) end end diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb index f98da1916b4..7f652c17ed5 100644 --- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb +++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb @@ -83,7 +83,7 @@ module Ci end end - describe '#stages_for_ref' do + describe '#stage_seeds' do context 'when no refs policy is specified' do let(:config) do YAML.dump(production: { stage: 'deploy', script: 'cap prod' }, @@ -91,15 +91,15 @@ module Ci spinach: { stage: 'test', script: 'spinach' }) end - it 'returns model attributes for stages with nested jobs' do - attributes = subject.stages_for_ref('master') + it 'returns correctly fabricated stage seeds object' do + seeds = subject.stage_seeds(ref: 'master') - expect(attributes.size).to eq 2 - expect(attributes.dig(0, :name)).to eq 'test' - expect(attributes.dig(1, :name)).to eq 'deploy' - expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'rspec' - expect(attributes.dig(0, :builds_attributes, 1, :name)).to eq 'spinach' - expect(attributes.dig(1, :builds_attributes, 0, :name)).to eq 'production' + expect(seeds.stages.size).to eq 2 + expect(seeds.stages.dig(0, :name)).to eq 'test' + expect(seeds.stages.dig(1, :name)).to eq 'deploy' + expect(seeds.jobs.dig(0, :name)).to eq 'rspec' + expect(seeds.jobs.dig(1, :name)).to eq 'spinach' + expect(seeds.jobs.dig(2, :name)).to eq 'production' end end @@ -109,14 +109,12 @@ module Ci spinach: { stage: 'test', script: 'spinach', only: ['tags'] }) end - it 'returns stage attributes except of jobs assigned to master' do - # true flag argument means matching jobs for tags - # - attributes = subject.stages_for_ref('feature', true) + it 'returns stage seeds only assigned to master to master' do + seeds = subject.stage_seeds(ref: 'feature', tag: true) - expect(attributes.size).to eq 1 - expect(attributes.dig(0, :name)).to eq 'test' - expect(attributes.dig(0, :builds_attributes, 0, :name)).to eq 'spinach' + expect(seeds.stages.size).to eq 1 + expect(seeds.stages.dig(0, :name)).to eq 'test' + expect(seeds.jobs.dig(0, :name)).to eq 'spinach' end end end diff --git a/spec/lib/gitlab/ci/stage/seeds_spec.rb b/spec/lib/gitlab/ci/stage/seeds_spec.rb index cc4f37d236e..3824a868fb2 100644 --- a/spec/lib/gitlab/ci/stage/seeds_spec.rb +++ b/spec/lib/gitlab/ci/stage/seeds_spec.rb @@ -6,6 +6,10 @@ describe Gitlab::Ci::Stage::Seeds do subject.append_stage('deploy', [{ name: 'prod', script: 'cap deploy' }]) end + describe '#has_stages?' do + it { is_expected.to have_stages } + end + describe '#stages' do it 'returns hashes of all stages' do expect(subject.stages.size).to eq 2 |