2019-05-27 08:26:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class CreatePipelineService < MergeRequests::BaseService
|
|
|
|
def execute(merge_request)
|
|
|
|
return unless can_create_pipeline_for?(merge_request)
|
|
|
|
|
|
|
|
create_detached_merge_request_pipeline(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_detached_merge_request_pipeline(merge_request)
|
2020-07-15 17:09:26 -04:00
|
|
|
Ci::CreatePipelineService.new(pipeline_project(merge_request),
|
2020-02-27 13:09:21 -05:00
|
|
|
current_user,
|
|
|
|
ref: pipeline_ref_for_detached_merge_request_pipeline(merge_request))
|
|
|
|
.execute(:merge_request_event, merge_request: merge_request)
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_pipeline_for?(merge_request)
|
|
|
|
##
|
|
|
|
# UpdateMergeRequestsWorker could be retried by an exception.
|
|
|
|
# pipelines for merge request should not be recreated in such case.
|
2020-02-13 07:08:49 -05:00
|
|
|
return false if !allow_duplicate && merge_request.find_actual_head_pipeline&.merge_request?
|
2019-05-27 08:26:39 -04:00
|
|
|
return false if merge_request.has_no_commits?
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_duplicate
|
|
|
|
params[:allow_duplicate]
|
|
|
|
end
|
2020-02-27 13:09:21 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-15 17:09:26 -04:00
|
|
|
def pipeline_project(merge_request)
|
|
|
|
if can_create_pipeline_in_target_project?(merge_request)
|
|
|
|
merge_request.target_project
|
|
|
|
else
|
|
|
|
merge_request.source_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 13:09:21 -05:00
|
|
|
def pipeline_ref_for_detached_merge_request_pipeline(merge_request)
|
2020-07-15 17:09:26 -04:00
|
|
|
if can_create_pipeline_in_target_project?(merge_request)
|
2020-02-27 13:09:21 -05:00
|
|
|
merge_request.ref_path
|
|
|
|
else
|
|
|
|
merge_request.source_branch
|
|
|
|
end
|
|
|
|
end
|
2020-07-15 17:09:26 -04:00
|
|
|
|
|
|
|
def can_create_pipeline_in_target_project?(merge_request)
|
|
|
|
if Gitlab::Ci::Features.allow_to_create_merge_request_pipelines_in_target_project?(merge_request.target_project)
|
|
|
|
can?(current_user, :create_pipeline, merge_request.target_project)
|
|
|
|
else
|
|
|
|
merge_request.for_same_project?
|
|
|
|
end
|
|
|
|
end
|
2019-05-27 08:26:39 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
MergeRequests::CreatePipelineService.prepend_if_ee('EE::MergeRequests::CreatePipelineService')
|