Commit e4cfe814 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it possible to fabricate CI/CD bridge jobs

parent 62e92031
......@@ -4,8 +4,6 @@ module Gitlab
module Ci
module Pipeline
module Seed
## TODO this should become Seed::Job now
#
class Build < Seed::Base
include Gitlab::Utils::StrongMemoize
......@@ -15,9 +13,6 @@ module Gitlab
@pipeline = pipeline
@attributes = attributes
# TODO we should extract that
@type = attributes.dig(:options, :trigger) ? ::Ci::Bridge : ::Ci::Build
@only = Gitlab::Ci::Build::Policy
.fabricate(attributes.delete(:only))
@except = Gitlab::Ci::Build::Policy
......@@ -40,11 +35,21 @@ module Gitlab
tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref?
).compact
)
end
def bridge?
@attributes.to_h.dig(:options, :trigger).present?
end
def to_resource
strong_memoize(:resource) { @type.new(attributes) }
strong_memoize(:resource) do
if bridge?
::Ci::Bridge.new(attributes)
else
::Ci::Build.new(attributes)
end
end
end
end
end
......
......@@ -39,7 +39,13 @@ module Gitlab
def to_resource
strong_memoize(:stage) do
::Ci::Stage.new(attributes).tap do |stage|
seeds.each { |seed| stage.statuses << seed.to_resource }
seeds.each do |seed|
if seed.bridge?
stage.statuses << seed.to_resource
else
stage.builds << seed.to_resource
end
end
end
end
end
......
......@@ -5,8 +5,7 @@ describe Gitlab::Ci::Pipeline::Seed::Build do
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
let(:attributes) do
{ name: 'rspec',
ref: 'master' }
{ name: 'rspec', ref: 'master' }
end
subject do
......@@ -21,10 +20,45 @@ describe Gitlab::Ci::Pipeline::Seed::Build do
end
end
describe '#bridge?' do
context 'when job is a bridge' do
let(:attributes) do
{ name: 'rspec', ref: 'master', options: { trigger: 'my/project' } }
end
it { is_expected.to be_bridge }
end
context 'when trigger definition is empty' do
let(:attributes) do
{ name: 'rspec', ref: 'master', options: { trigger: '' } }
end
it { is_expected.not_to be_bridge }
end
context 'when job is not a bridge' do
it { is_expected.not_to be_bridge }
end
end
describe '#to_resource' do
it 'returns a valid build resource' do
expect(subject.to_resource).to be_a(::Ci::Build)
expect(subject.to_resource).to be_valid
context 'when job is not a bridge' do
it 'returns a valid build resource' do
expect(subject.to_resource).to be_a(::Ci::Build)
expect(subject.to_resource).to be_valid
end
end
context 'when job is a bridge' do
let(:attributes) do
{ name: 'rspec', ref: 'master', options: { trigger: 'my/project' } }
end
it 'returns a valid bridge resource' do
expect(subject.to_resource).to be_a(::Ci::Bridge)
expect(subject.to_resource).to be_valid
end
end
it 'memoizes a resource object' do
......
......@@ -62,8 +62,18 @@ describe Gitlab::Ci::Pipeline::Seed::Stage do
expect(subject.seeds.map(&:attributes)).to all(include(ref: 'master'))
expect(subject.seeds.map(&:attributes)).to all(include(tag: false))
expect(subject.seeds.map(&:attributes)).to all(include(project: pipeline.project))
expect(subject.seeds.map(&:attributes))
.to all(include(trigger_request: pipeline.trigger_requests.first))
end
context 'when a legacy trigger exists' do
before do
create(:ci_trigger_request, pipeline: pipeline)
end
it 'returns build seeds including legacy trigger' do
expect(pipeline.legacy_trigger).not_to be_nil
expect(subject.seeds.map(&:attributes))
.to all(include(trigger_request: pipeline.legacy_trigger))
end
end
context 'when a ref is protected' do
......
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