Commit ab0cd2a8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Introduce pipeline build seeds

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