Commit b24b45be authored by Grzegorz Bizon's avatar Grzegorz Bizon

Instantiate pipeline stages and builds before saving

parent 6cfea81e
......@@ -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
stage.save!
pipeline.builds.create!(builds_attributes).each do |build|
stage.builds.each do |build|
yield build if block_given?
end
end
......
......@@ -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
......
......@@ -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
......
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