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
include ::Gitlab::Config::Entry::Configurable
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
validates :config, allowed_keys: ALLOWED_KEYS
......@@ -26,15 +35,29 @@ module EE
entry :trigger, ::EE::Gitlab::Ci::Config::Entry::Trigger,
description: 'CI/CD Bridge downstream trigger definition.'
helpers :trigger
attributes ALLOWED_KEYS
entry :stage, ::Gitlab::Ci::Config::Entry::Stage,
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
@metadata[:name]
end
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
......
......@@ -68,6 +68,35 @@ describe Ci::CreatePipelineService, '#execute' do
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!
service.execute(:push)
end
......
......@@ -4,6 +4,8 @@ module Gitlab
module Ci
module Pipeline
module Seed
## TODO this should become Seed::Job now
#
class Build < Seed::Base
include Gitlab::Utils::StrongMemoize
......@@ -13,6 +15,9 @@ 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
......@@ -35,13 +40,11 @@ module Gitlab
tag: @pipeline.tag,
trigger_request: @pipeline.legacy_trigger,
protected: @pipeline.protected_ref?
)
).compact
end
def to_resource
strong_memoize(:resource) do
::Ci::Build.new(attributes)
end
strong_memoize(:resource) { @type.new(attributes) }
end
end
end
......
......@@ -39,7 +39,7 @@ module Gitlab
def to_resource
strong_memoize(:stage) do
::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
......
......@@ -33,7 +33,7 @@ module Gitlab
{ stage_idx: @stages.index(job[:stage]),
stage: job[:stage],
tag_list: job[:tags] || [],
tag_list: job[:tags],
name: job[:name].to_s,
allow_failure: job[:ignore],
when: job[:when] || 'on_success',
......@@ -53,7 +53,8 @@ module Gitlab
retry: job[:retry],
parallel: job[:parallel],
instance: job[:instance],
start_in: job[:start_in]
start_in: job[:start_in],
trigger: job[:trigger]
}.compact }
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