2019-09-04 21:01:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class CreatePipelineService < MergeRequests::BaseService
|
|
|
|
def execute(merge_request)
|
2021-10-27 15:23:28 +05:30
|
|
|
return cannot_create_pipeline_error unless can_create_pipeline_for?(merge_request)
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
create_detached_merge_request_pipeline(merge_request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_detached_merge_request_pipeline(merge_request)
|
2022-07-23 23:45:48 +05:30
|
|
|
Ci::CreatePipelineService
|
|
|
|
.new(pipeline_project(merge_request),
|
|
|
|
current_user,
|
|
|
|
ref: pipeline_ref_for_detached_merge_request_pipeline(merge_request),
|
|
|
|
push_options: params[:push_options])
|
2020-04-08 14:13:33 +05:30
|
|
|
.execute(:merge_request_event, merge_request: merge_request)
|
2019-09-04 21:01:54 +05:30
|
|
|
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-03-13 15:44:24 +05:30
|
|
|
return false if !allow_duplicate && merge_request.find_actual_head_pipeline&.merge_request?
|
2019-09-04 21:01:54 +05:30
|
|
|
return false if merge_request.has_no_commits?
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_duplicate
|
|
|
|
params[:allow_duplicate]
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
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-04-08 14:13:33 +05:30
|
|
|
def pipeline_ref_for_detached_merge_request_pipeline(merge_request)
|
2020-07-28 23:09:34 +05:30
|
|
|
if can_create_pipeline_in_target_project?(merge_request)
|
2020-04-08 14:13:33 +05:30
|
|
|
merge_request.ref_path
|
|
|
|
else
|
|
|
|
merge_request.source_branch
|
|
|
|
end
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
def can_create_pipeline_in_target_project?(merge_request)
|
2022-08-27 11:52:29 +05:30
|
|
|
merge_request.target_project.ci_allow_fork_pipelines_to_run_in_parent_project? &&
|
|
|
|
can?(current_user, :create_pipeline, merge_request.target_project) &&
|
2022-08-13 15:12:31 +05:30
|
|
|
can_update_source_branch_in_target_project?(merge_request)
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def can_update_source_branch_in_target_project?(merge_request)
|
|
|
|
::Gitlab::UserAccess.new(current_user, container: merge_request.target_project)
|
|
|
|
.can_update_branch?(merge_request.source_branch_ref)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
def cannot_create_pipeline_error
|
|
|
|
ServiceResponse.error(message: 'Cannot create a pipeline for this merge request.', payload: nil)
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
MergeRequests::CreatePipelineService.prepend_mod_with('MergeRequests::CreatePipelineService')
|