2014-04-03 03:36:10 -04:00
|
|
|
module MergeRequests
|
2014-04-02 14:51:17 -04:00
|
|
|
class CreateService < MergeRequests::BaseService
|
|
|
|
def execute
|
2015-08-21 12:47:39 -04:00
|
|
|
# @project is used to determine whether the user can set the merge request's
|
2016-02-16 18:01:14 -05:00
|
|
|
# assignee, milestone and labels. Whether they can depends on their
|
2015-08-21 12:47:39 -04:00
|
|
|
# permissions on the target project.
|
|
|
|
source_project = @project
|
|
|
|
@project = Project.find(params[:target_project_id]) if params[:target_project_id]
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
merge_request = MergeRequest.new
|
2017-07-12 04:45:40 -04:00
|
|
|
merge_request.target_project = @project
|
2015-08-21 12:47:39 -04:00
|
|
|
merge_request.source_project = source_project
|
2017-05-25 17:14:40 -04:00
|
|
|
merge_request.source_branch = params[:source_branch]
|
2016-06-30 11:34:19 -04:00
|
|
|
merge_request.merge_params['force_remove_source_branch'] = params.delete(:force_remove_source_branch)
|
2014-04-02 14:51:17 -04:00
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
create(merge_request)
|
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
|
2017-08-01 08:38:45 -04:00
|
|
|
def before_create(merge_request)
|
2017-08-08 03:41:27 -04:00
|
|
|
# current_user (defined in BaseService) is not available within run_after_commit block
|
2017-08-01 08:38:45 -04:00
|
|
|
user = current_user
|
|
|
|
merge_request.run_after_commit do
|
|
|
|
NewMergeRequestWorker.perform_async(merge_request.id, user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-16 20:59:55 -04:00
|
|
|
def after_create(issuable)
|
2016-06-30 11:34:19 -04:00
|
|
|
todo_service.new_merge_request(issuable, current_user)
|
2016-09-20 15:52:20 -04:00
|
|
|
issuable.cache_merge_request_closes_issues!(current_user)
|
2017-08-02 09:11:32 -04:00
|
|
|
update_merge_requests_head_pipeline(issuable)
|
2017-08-17 11:21:25 -04:00
|
|
|
|
|
|
|
super
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
2017-05-25 17:14:40 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-02 09:11:32 -04:00
|
|
|
def update_merge_requests_head_pipeline(merge_request)
|
|
|
|
pipeline = head_pipeline_for(merge_request)
|
|
|
|
merge_request.update(head_pipeline_id: pipeline.id) if pipeline
|
|
|
|
end
|
|
|
|
|
2017-05-25 17:14:40 -04:00
|
|
|
def head_pipeline_for(merge_request)
|
|
|
|
return unless merge_request.source_project
|
|
|
|
|
2017-06-01 14:31:52 -04:00
|
|
|
sha = merge_request.source_branch_sha
|
2017-05-25 17:14:40 -04:00
|
|
|
return unless sha
|
|
|
|
|
2017-06-01 14:31:52 -04:00
|
|
|
pipelines = merge_request.source_project.pipelines.where(ref: merge_request.source_branch, sha: sha)
|
2017-05-25 17:14:40 -04:00
|
|
|
|
2017-06-01 14:31:52 -04:00
|
|
|
pipelines.order(id: :desc).first
|
2017-05-25 17:14:40 -04:00
|
|
|
end
|
2014-04-02 14:51:17 -04:00
|
|
|
end
|
|
|
|
end
|