2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
module MergeRequests
|
|
|
|
class CreateFromIssueService < MergeRequests::CreateService
|
2017-11-25 06:33:05 -05:00
|
|
|
def initialize(project, user, params)
|
|
|
|
# branch - the name of new branch
|
|
|
|
# ref - the source of new branch.
|
|
|
|
|
|
|
|
@branch_name = params[:branch_name]
|
|
|
|
@issue_iid = params[:issue_iid]
|
|
|
|
@ref = params[:ref]
|
|
|
|
|
|
|
|
super(project, user)
|
|
|
|
end
|
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
def execute
|
2017-11-25 06:33:05 -05:00
|
|
|
return error('Invalid issue iid') unless @issue_iid.present? && issue.present?
|
2017-05-04 04:09:21 -04:00
|
|
|
|
|
|
|
result = CreateBranchService.new(project, current_user).execute(branch_name, ref)
|
|
|
|
return result if result[:status] == :error
|
|
|
|
|
|
|
|
SystemNoteService.new_issue_branch(issue, project, current_user, branch_name)
|
|
|
|
|
|
|
|
new_merge_request = create(merge_request)
|
|
|
|
|
|
|
|
if new_merge_request.valid?
|
|
|
|
success(new_merge_request)
|
|
|
|
else
|
|
|
|
error(new_merge_request.errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-04 04:09:21 -04:00
|
|
|
def issue
|
2017-11-25 06:33:05 -05:00
|
|
|
@issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid)
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-04 04:09:21 -04:00
|
|
|
|
|
|
|
def branch_name
|
2017-11-25 06:33:05 -05:00
|
|
|
@branch ||= @branch_name || issue.to_branch_name
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
2018-06-08 08:28:43 -04:00
|
|
|
return @ref if project.repository.branch_exists?(@ref)
|
|
|
|
|
|
|
|
project.default_branch || 'master'
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params
|
|
|
|
{
|
2017-11-25 06:33:05 -05:00
|
|
|
issue_iid: @issue_iid,
|
2017-05-04 04:09:21 -04:00
|
|
|
source_project_id: project.id,
|
|
|
|
source_branch: branch_name,
|
2017-08-17 10:44:37 -04:00
|
|
|
target_project_id: project.id,
|
2018-09-06 13:02:51 -04:00
|
|
|
target_branch: ref
|
2017-05-04 04:09:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def success(merge_request)
|
|
|
|
super().merge(merge_request: merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|