Commit 62e92031 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add specs for creating a bridge jobs from triggers

parent c86015bf
...@@ -13,7 +13,16 @@ module EE ...@@ -13,7 +13,16 @@ module EE
include ::Gitlab::Config::Entry::Configurable include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Attributable include ::Gitlab::Config::Entry::Attributable
ALLOWED_KEYS = %i[trigger].freeze ALLOWED_KEYS = %i[trigger stage allow_failure only except].freeze
# TODO we probably need a common ancestor for a status / job
#
DEFAULT_ONLY_POLICY = {
refs: %w(branches tags)
}.freeze
DEFAULT_EXCEPT_POLICY = {
}.freeze
validations do validations do
validates :config, allowed_keys: ALLOWED_KEYS validates :config, allowed_keys: ALLOWED_KEYS
...@@ -26,15 +35,29 @@ module EE ...@@ -26,15 +35,29 @@ module EE
entry :trigger, ::EE::Gitlab::Ci::Config::Entry::Trigger, entry :trigger, ::EE::Gitlab::Ci::Config::Entry::Trigger,
description: 'CI/CD Bridge downstream trigger definition.' description: 'CI/CD Bridge downstream trigger definition.'
helpers :trigger entry :stage, ::Gitlab::Ci::Config::Entry::Stage,
attributes ALLOWED_KEYS description: 'Pipeline stage this job will be executed into.'
entry :only, ::Gitlab::Ci::Config::Entry::Policy,
description: 'Refs policy this job will be executed for.'
entry :except, ::Gitlab::Ci::Config::Entry::Policy,
description: 'Refs policy this job will be executed for.'
helpers *ALLOWED_KEYS
attributes *ALLOWED_KEYS
def name def name
@metadata[:name] @metadata[:name]
end end
def value def value
{ name: name, trigger: trigger_value } { name: name,
trigger: trigger_value,
ignore: !!allow_failure,
stage: stage_value,
only: DEFAULT_ONLY_POLICY.deep_merge(only_value.to_h),
except: DEFAULT_EXCEPT_POLICY.deep_merge(except_value.to_h) }
end end
end end
end end
......
...@@ -68,6 +68,35 @@ describe Ci::CreatePipelineService, '#execute' do ...@@ -68,6 +68,35 @@ describe Ci::CreatePipelineService, '#execute' do
end end
end end
describe 'cross-project pipeline triggers' do
before do
stub_feature_flags(cross_project_pipeline_triggers: true)
stub_ci_pipeline_yaml_file <<~YAML
test:
script: rspec
deploy:
stage: deploy
trigger: my/project
YAML
end
it 'creates bridge jobs correctly' do
pipeline = create_pipeline!
test = pipeline.statuses.find_by(name: 'test')
bridge = pipeline.statuses.find_by(name: 'deploy')
expect(pipeline).to be_persisted
expect(test).to be_a Ci::Build
expect(bridge).to be_a Ci::Bridge
expect(bridge.stage).to eq 'deploy'
expect(pipeline.statuses).to match_array [test, bridge]
expect(bridge.options).to eq(trigger: { project: 'my/project' })
end
end
def create_pipeline! def create_pipeline!
service.execute(:push) service.execute(:push)
end end
......
...@@ -4,6 +4,8 @@ module Gitlab ...@@ -4,6 +4,8 @@ module Gitlab
module Ci module Ci
module Pipeline module Pipeline
module Seed module Seed
## TODO this should become Seed::Job now
#
class Build < Seed::Base class Build < Seed::Base
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
...@@ -13,6 +15,9 @@ module Gitlab ...@@ -13,6 +15,9 @@ module Gitlab
@pipeline = pipeline @pipeline = pipeline
@attributes = attributes @attributes = attributes
# TODO we should extract that
@type = attributes.dig(:options, :trigger) ? ::Ci::Bridge : ::Ci::Build
@only = Gitlab::Ci::Build::Policy @only = Gitlab::Ci::Build::Policy
.fabricate(attributes.delete(:only)) .fabricate(attributes.delete(:only))
@except = Gitlab::Ci::Build::Policy @except = Gitlab::Ci::Build::Policy
...@@ -35,13 +40,11 @@ module Gitlab ...@@ -35,13 +40,11 @@ module Gitlab
tag: @pipeline.tag, tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger, trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref? protected: @pipeline.protected_ref?
) ).compact
end end
def to_resource def to_resource
strong_memoize(:resource) do strong_memoize(:resource) { @type.new(attributes) }
::Ci::Build.new(attributes)
end
end end
end end
end end
......
...@@ -39,7 +39,7 @@ module Gitlab ...@@ -39,7 +39,7 @@ module Gitlab
def to_resource def to_resource
strong_memoize(:stage) do strong_memoize(:stage) do
::Ci::Stage.new(attributes).tap do |stage| ::Ci::Stage.new(attributes).tap do |stage|
seeds.each { |seed| stage.builds << seed.to_resource } seeds.each { |seed| stage.statuses << seed.to_resource }
end end
end end
end end
......
...@@ -33,7 +33,7 @@ module Gitlab ...@@ -33,7 +33,7 @@ module Gitlab
{ stage_idx: @stages.index(job[:stage]), { stage_idx: @stages.index(job[:stage]),
stage: job[:stage], stage: job[:stage],
tag_list: job[:tags] || [], tag_list: job[:tags],
name: job[:name].to_s, name: job[:name].to_s,
allow_failure: job[:ignore], allow_failure: job[:ignore],
when: job[:when] || 'on_success', when: job[:when] || 'on_success',
...@@ -53,7 +53,8 @@ module Gitlab ...@@ -53,7 +53,8 @@ module Gitlab
retry: job[:retry], retry: job[:retry],
parallel: job[:parallel], parallel: job[:parallel],
instance: job[:instance], instance: job[:instance],
start_in: job[:start_in] start_in: job[:start_in],
trigger: job[:trigger]
}.compact } }.compact }
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