Commit bf9c09d7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add basic implementation of cross project pipeline service

parent d426d249
......@@ -6,6 +6,7 @@ module EE
extend ActiveSupport::Concern
prepended do
serialize :options # rubocop:disable Cop/ActiveRecordSerialize
has_many :sourced_pipelines, class_name: ::Ci::Sources::Pipeline,
foreign_key: :source_job_id
......@@ -35,7 +36,19 @@ module EE
end
def schedule_downstream_pipeline!
raise NotImplementedError
CreateCrossProjectPipelineWorker.perform_async(self.id)
end
def target_user
self.user
end
def target_project_name
options.dig(:trigger, :project)
end
def target_ref
options.dig(:trigger, :branch)
end
end
end
......
# frozen_string_literal: true
module Ci
class CreateCrossProjectPipelineService < ::BaseService
include Gitlab::Utils::StrongMemoize
def execute(bridge)
@bridge = bridge
unless can_create_cross_pipeline?
return bridge.drop!(:insufficient_permissions)
end
create_pipeline do |pipeline|
source = bridge.sourced_pipelines.build(
source_pipeline: bridge.pipeline,
source_project: bridge.project,
project: target_project,
pipeline: pipeline)
pipeline.source_pipeline = source
end
end
private
def can_create_cross_pipeline?
can?(current_user, :create_pipeline, project) &&
can?(current_user, :create_pipeline, target_project) &&
can?(@bridge.target_user, :create_pipeline, target_project)
end
def create_pipeline
::Ci::CreatePipelineService
.new(target_project, @bridge.target_user, ref: @bridge.target_ref)
.execute(:pipeline, ignore_skip_ci: true) do |pipeline|
yield pipeline
end
end
def target_project
strong_memoize(:target_project) do
Project.find_by_full_path(@bridge.target_project_name)
end
end
end
end
# frozen_string_literal: true
class CreateCrossProjectPipelineWorker
include ::ApplicationWorker
include ::PipelineQueue
def perform(bridge_id)
::Ci::Bridge.find(bridge_id).try do |bridge|
::Ci::CreateCrossProjectPipelineService
.new(bridge.project, bridge.user)
.execute(bridge)
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