Commit 7b437396 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Start building abstraction over pipeline seeds

parent 83367ed2
module Gitlab
module Ci
module Pipeline
module Seed
class Base
def attributes
raise NotImplementedError
end
def excluded?
raise NotImplementedError
end
end
end
end
end
end
module Gitlab
module Ci
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)
@pipeline = pipeline
@stage = stage # stage name
@builds = builds.to_a.dup # builds array of hashes
end
def user=(current_user)
@builds.map! do |attributes|
attributes.merge(user: current_user)
end
end
def stage_attributes
{ name: @stage, project: project }
end
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
end
def create!
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!
stage.builds.each do |build|
yield build if block_given?
end
end
end
private
def protected_ref?
strong_memoize(:protected_ref) do
project.protected_for?(pipeline.ref)
end
end
end
end
end
end
end
module Gitlab
module Ci
module Stage
class Seed
include ::Gitlab::Utils::StrongMemoize
attr_reader :pipeline
delegate :project, to: :pipeline
delegate :size, to: :@builds
def initialize(pipeline, stage, builds)
@pipeline = pipeline
@stage = stage # stage name
@builds = builds.to_a.dup # builds array of hashes
end
def user=(current_user)
@builds.map! do |attributes|
attributes.merge(user: current_user)
end
end
def stage_attributes
{ name: @stage, project: project }
end
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
end
def create!
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!
stage.builds.each do |build|
yield build if block_given?
end
end
end
private
def protected_ref?
strong_memoize(:protected_ref) do
project.protected_for?(pipeline.ref)
end
end
end
end
end
end
...@@ -73,7 +73,8 @@ module Gitlab ...@@ -73,7 +73,8 @@ module Gitlab
seeds = @stages.uniq.map do |stage| seeds = @stages.uniq.map do |stage|
builds = pipeline_stage_builds(stage, pipeline) builds = pipeline_stage_builds(stage, pipeline)
Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any? Gitlab::Ci::Pipeline::Seed::Stage
.new(pipeline, stage, builds) if builds.any?
end end
seeds.compact seeds.compact
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Stage::Seed do describe Gitlab::Ci::Pipeline::Seed::Stage do
let(:pipeline) { create(:ci_empty_pipeline) } let(:pipeline) { create(:ci_empty_pipeline) }
let(:builds) do let(:builds) do
......
...@@ -252,7 +252,8 @@ describe Ci::Pipeline, :mailer do ...@@ -252,7 +252,8 @@ describe Ci::Pipeline, :mailer do
end end
it 'returns preseeded stage seeds object' do it 'returns preseeded stage seeds object' do
expect(pipeline.stage_seeds).to all(be_a Gitlab::Ci::Stage::Seed) expect(pipeline.stage_seeds)
.to all(be_a Gitlab::Ci::Pipeline::Seed::Base)
expect(pipeline.stage_seeds.count).to eq 1 expect(pipeline.stage_seeds.count).to eq 1
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