Commit 83367ed2 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Instantiate pipeline stages and builds before saving

parent edd95e13
...@@ -7,28 +7,28 @@ module Gitlab ...@@ -7,28 +7,28 @@ module Gitlab
attr_reader :pipeline attr_reader :pipeline
delegate :project, to: :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 @pipeline = pipeline
@stage = { name: stage } @stage = stage # stage name
@jobs = jobs.to_a.dup @builds = builds.to_a.dup # builds array of hashes
end end
def user=(current_user) def user=(current_user)
@jobs.map! do |attributes| @builds.map! do |attributes|
attributes.merge(user: current_user) attributes.merge(user: current_user)
end end
end end
def stage def stage_attributes
@stage.merge(project: project) { name: @stage, project: project }
end end
def builds def builds_attributes
trigger = pipeline.trigger_requests.first trigger = pipeline.trigger_requests.first
@jobs.map do |attributes| @builds.map do |attributes|
attributes.merge(project: project, attributes.merge(project: project,
ref: pipeline.ref, ref: pipeline.ref,
tag: pipeline.tag, tag: pipeline.tag,
...@@ -38,12 +38,16 @@ module Gitlab ...@@ -38,12 +38,16 @@ module Gitlab
end end
def create! def create!
pipeline.stages.create!(stage).tap do |stage| pipeline.stages.build(stage_attributes).tap do |stage|
builds_attributes = builds.map do |attributes| builds_attributes.each do |build_attributes|
attributes.merge(stage_id: stage.id) stage.builds.build(build_attributes).tap do |build|
build.pipeline = pipeline
end end
end
stage.save!
pipeline.builds.create!(builds_attributes).each do |build| stage.builds.each do |build|
yield build if block_given? yield build if block_given?
end end
end end
......
...@@ -17,20 +17,20 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -17,20 +17,20 @@ describe Gitlab::Ci::Stage::Seed do
end end
end end
describe '#stage' do describe '#stage_attributes' do
it 'returns hash attributes of a stage' do it 'returns hash attributes of a stage' do
expect(subject.stage).to be_a Hash expect(subject.stage_attributes).to be_a Hash
expect(subject.stage).to include(:name, :project) expect(subject.stage_attributes).to include(:name, :project)
end end
end end
describe '#builds' do describe '#builds_attributes' do
it 'returns hash attributes of all builds' do it 'returns hash attributes of all builds' do
expect(subject.builds.size).to eq 2 expect(subject.builds_attributes.size).to eq 2
expect(subject.builds).to all(include(ref: 'master')) expect(subject.builds_attributes).to all(include(ref: 'master'))
expect(subject.builds).to all(include(tag: false)) expect(subject.builds_attributes).to all(include(tag: false))
expect(subject.builds).to all(include(project: pipeline.project)) expect(subject.builds_attributes).to all(include(project: pipeline.project))
expect(subject.builds) expect(subject.builds_attributes)
.to all(include(trigger_request: pipeline.trigger_requests.first)) .to all(include(trigger_request: pipeline.trigger_requests.first))
end end
...@@ -40,7 +40,7 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -40,7 +40,7 @@ describe Gitlab::Ci::Stage::Seed do
end end
it 'returns protected builds' do it 'returns protected builds' do
expect(subject.builds).to all(include(protected: true)) expect(subject.builds_attributes).to all(include(protected: true))
end end
end end
...@@ -50,7 +50,7 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -50,7 +50,7 @@ describe Gitlab::Ci::Stage::Seed do
end end
it 'returns unprotected builds' do 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 end
end end
...@@ -61,7 +61,7 @@ describe Gitlab::Ci::Stage::Seed do ...@@ -61,7 +61,7 @@ describe Gitlab::Ci::Stage::Seed do
it 'assignes relevant pipeline attributes' do it 'assignes relevant pipeline attributes' do
subject.user = user subject.user = user
expect(subject.builds).to all(include(user: user)) expect(subject.builds_attributes).to all(include(user: user))
end end
end end
......
...@@ -119,11 +119,11 @@ module Gitlab ...@@ -119,11 +119,11 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 2 expect(seeds.size).to eq 2
expect(seeds.first.stage[:name]).to eq 'test' expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.second.stage[:name]).to eq 'deploy' expect(seeds.second.stage_attributes[:name]).to eq 'deploy'
expect(seeds.first.builds.dig(0, :name)).to eq 'rspec' expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'rspec'
expect(seeds.first.builds.dig(1, :name)).to eq 'spinach' expect(seeds.first.builds_attributes.dig(1, :name)).to eq 'spinach'
expect(seeds.second.builds.dig(0, :name)).to eq 'production' expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production'
end end
end end
...@@ -141,8 +141,8 @@ module Gitlab ...@@ -141,8 +141,8 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1 expect(seeds.size).to eq 1
expect(seeds.first.stage[:name]).to eq 'test' expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach'
end end
end end
...@@ -160,8 +160,8 @@ module Gitlab ...@@ -160,8 +160,8 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1 expect(seeds.size).to eq 1
expect(seeds.first.stage[:name]).to eq 'test' expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach'
end end
end end
...@@ -183,8 +183,8 @@ module Gitlab ...@@ -183,8 +183,8 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 2 expect(seeds.size).to eq 2
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach'
expect(seeds.second.builds.dig(0, :name)).to eq 'production' expect(seeds.second.builds_attributes.dig(0, :name)).to eq 'production'
end end
end end
...@@ -209,7 +209,7 @@ module Gitlab ...@@ -209,7 +209,7 @@ module Gitlab
seeds = subject.stage_seeds(pipeline) seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1 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 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