From b24b45be628440e4a3523a0b89ecf9c83f80e8ea Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 19 Mar 2018 09:54:30 +0100 Subject: Instantiate pipeline stages and builds before saving --- lib/gitlab/ci/stage/seed.rb | 30 +++++++++++++++++------------- spec/lib/gitlab/ci/stage/seed_spec.rb | 24 ++++++++++++------------ spec/lib/gitlab/ci/yaml_processor_spec.rb | 24 ++++++++++++------------ 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/gitlab/ci/stage/seed.rb b/lib/gitlab/ci/stage/seed.rb index f33c87f554d..493af6b0868 100644 --- a/lib/gitlab/ci/stage/seed.rb +++ b/lib/gitlab/ci/stage/seed.rb @@ -7,28 +7,28 @@ module Gitlab attr_reader :pipeline delegate :project, to: :pipeline - delegate :size, to: :@jobs + delegate :size, to: :@builds - def initialize(pipeline, stage, jobs) + def initialize(pipeline, stage, builds) @pipeline = pipeline - @stage = { name: stage } - @jobs = jobs.to_a.dup + @stage = stage # stage name + @builds = builds.to_a.dup # builds array of hashes end def user=(current_user) - @jobs.map! do |attributes| + @builds.map! do |attributes| attributes.merge(user: current_user) end end - def stage - @stage.merge(project: project) + def stage_attributes + { name: @stage, project: project } end - def builds + def builds_attributes trigger = pipeline.trigger_requests.first - @jobs.map do |attributes| + @builds.map do |attributes| attributes.merge(project: project, ref: pipeline.ref, tag: pipeline.tag, @@ -38,12 +38,16 @@ module Gitlab end def create! - pipeline.stages.create!(stage).tap do |stage| - builds_attributes = builds.map do |attributes| - attributes.merge(stage_id: stage.id) + pipeline.stages.build(stage_attributes).tap do |stage| + builds_attributes.each do |build_attributes| + stage.builds.build(build_attributes).tap do |build| + build.pipeline = pipeline + end end - pipeline.builds.create!(builds_attributes).each do |build| + stage.save! + + stage.builds.each do |build| yield build if block_given? end end diff --git a/spec/lib/gitlab/ci/stage/seed_spec.rb b/spec/lib/gitlab/ci/stage/seed_spec.rb index 3fe8d50c49a..71e0bc0dbab 100644 --- a/spec/lib/gitlab/ci/stage/seed_spec.rb +++ b/spec/lib/gitlab/ci/stage/seed_spec.rb @@ -17,20 +17,20 @@ describe Gitlab::Ci::Stage::Seed do end end - describe '#stage' do + describe '#stage_attributes' do it 'returns hash attributes of a stage' do - expect(subject.stage).to be_a Hash - expect(subject.stage).to include(:name, :project) + expect(subject.stage_attributes).to be_a Hash + expect(subject.stage_attributes).to include(:name, :project) end end - describe '#builds' do + describe '#builds_attributes' do it 'returns hash attributes of all builds' do - expect(subject.builds.size).to eq 2 - expect(subject.builds).to all(include(ref: 'master')) - expect(subject.builds).to all(include(tag: false)) - expect(subject.builds).to all(include(project: pipeline.project)) - expect(subject.builds) + expect(subject.builds_attributes.size).to eq 2 + expect(subject.builds_attributes).to all(include(ref: 'master')) + expect(subject.builds_attributes).to all(include(tag: false)) + expect(subject.builds_attributes).to all(include(project: pipeline.project)) + expect(subject.builds_attributes) .to all(include(trigger_request: pipeline.trigger_requests.first)) end @@ -40,7 +40,7 @@ describe Gitlab::Ci::Stage::Seed do end it 'returns protected builds' do - expect(subject.builds).to all(include(protected: true)) + expect(subject.builds_attributes).to all(include(protected: true)) end end @@ -50,7 +50,7 @@ describe Gitlab::Ci::Stage::Seed do end it 'returns unprotected builds' do - expect(subject.builds).to all(include(protected: false)) + expect(subject.builds_attributes).to all(include(protected: false)) end end end @@ -61,7 +61,7 @@ describe Gitlab::Ci::Stage::Seed do it 'assignes relevant pipeline attributes' do subject.user = user - expect(subject.builds).to all(include(user: user)) + expect(subject.builds_attributes).to all(include(user: user)) end end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index f83f932e61e..1d852e2b3b2 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -119,11 +119,11 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 2 - expect(seeds.first.stage[:name]).to eq 'test' - expect(seeds.second.stage[:name]).to eq 'deploy' - expect(seeds.first.builds.dig(0, :name)).to eq 'rspec' - expect(seeds.first.builds.dig(1, :name)).to eq 'spinach' - expect(seeds.second.builds.dig(0, :name)).to eq 'production' + expect(seeds.first.stage_attributes[:name]).to eq 'test' + expect(seeds.second.stage_attributes[:name]).to eq 'deploy' + expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'rspec' + expect(seeds.first.builds_attributes.dig(1, :name)).to eq 'spinach' + expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' end end @@ -141,8 +141,8 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 1 - expect(seeds.first.stage[:name]).to eq 'test' - expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' + expect(seeds.first.stage_attributes[:name]).to eq 'test' + expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' end end @@ -160,8 +160,8 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 1 - expect(seeds.first.stage[:name]).to eq 'test' - expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' + expect(seeds.first.stage_attributes[:name]).to eq 'test' + expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' end end @@ -183,8 +183,8 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 2 - expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' - expect(seeds.second.builds.dig(0, :name)).to eq 'production' + expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' + expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production' end end @@ -209,7 +209,7 @@ module Gitlab seeds = subject.stage_seeds(pipeline) expect(seeds.size).to eq 1 - expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' + expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach' end end end -- cgit v1.2.1