Commit 46d910ed authored by Grzegorz Bizon's avatar Grzegorz Bizon

Introduce pipeline build seeds

parent 7b437396
......@@ -6,6 +6,7 @@ module Ci
include AfterCommitQueue
include Presentable
include Gitlab::OptimisticLocking
include Gitlab::Utils::StrongMemoize
prepend ::EE::Ci::Pipeline
......@@ -490,6 +491,22 @@ module Ci
end
end
# TODO specs
#
def protected_ref?
strong_memoize(:protected_ref) do
project.protected_for?(ref)
end
end
# TODO specs
#
def legacy_trigger
strong_memoize(:legacy_trigger) do
trigger_requests.first
end
end
def predefined_variables
Gitlab::Ci::Variables::Collection.new
.append(key: 'CI_PIPELINE_ID', value: id.to_s)
......
module Gitlab
module Ci
module Pipeline
module Seed
class Build < Seed::Base
def initialize(pipeline, attributes)
@pipeline = pipeline
@attributes = attributes
end
# TODO find a different solution
#
def user=(current_user)
@attributes.merge!(user: current_user)
end
def attributes
@attributes.merge(project: @pipeline.project,
ref: @pipeline.ref,
tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref?)
end
end
end
end
end
end
......@@ -3,43 +3,35 @@ module Gitlab
module Pipeline
module Seed
class Stage < Seed::Base
include ::Gitlab::Utils::StrongMemoize
attr_reader :pipeline
delegate :project, to: :pipeline
delegate :size, to: :@builds
def initialize(pipeline, stage, builds)
def initialize(pipeline, name, builds)
@pipeline = pipeline
@stage = stage # stage name
@builds = builds.to_a.dup # builds array of hashes
@name = name
@builds = builds.map do |attributes|
Seed::Build.new(pipeline, attributes)
end
end
def user=(current_user)
@builds.map! do |attributes|
attributes.merge(user: current_user)
end
@builds.each { |seed| seed.user = current_user }
end
def stage_attributes
{ name: @stage, project: project }
def attributes
{ name: @name, project: project }
end
# TODO decouple from Seed::Build
def builds_attributes
trigger = pipeline.trigger_requests.first
@builds.map do |attributes|
attributes.merge(project: project,
ref: pipeline.ref,
tag: pipeline.tag,
trigger_request: trigger,
protected: protected_ref?)
end
@builds.map(&:attributes)
end
def create!
pipeline.stages.build(stage_attributes).tap do |stage|
pipeline.stages.build(attributes).tap do |stage|
builds_attributes.each do |build_attributes|
stage.builds.build(build_attributes).tap do |build|
build.pipeline = pipeline
......@@ -57,9 +49,6 @@ module Gitlab
private
def protected_ref?
strong_memoize(:protected_ref) do
project.protected_for?(pipeline.ref)
end
end
end
end
......
......@@ -19,8 +19,8 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
describe '#stage_attributes' do
it 'returns hash attributes of a stage' do
expect(subject.stage_attributes).to be_a Hash
expect(subject.stage_attributes).to include(:name, :project)
expect(subject.attributes).to be_a Hash
expect(subject.attributes).to include(:name, :project)
end
end
......
......@@ -119,8 +119,8 @@ module Gitlab
seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 2
expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.second.stage_attributes[:name]).to eq 'deploy'
expect(seeds.first.attributes[:name]).to eq 'test'
expect(seeds.second.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'
......@@ -141,7 +141,7 @@ module Gitlab
seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1
expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.first.attributes[:name]).to eq 'test'
expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach'
end
end
......@@ -160,7 +160,7 @@ module Gitlab
seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1
expect(seeds.first.stage_attributes[:name]).to eq 'test'
expect(seeds.first.attributes[:name]).to eq 'test'
expect(seeds.first.builds_attributes.dig(0, :name)).to eq 'spinach'
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