gitlab-org--gitlab-foss/app/services/merge_requests/create_from_issue_service.rb
2017-08-21 14:30:37 +01:00

57 lines
1.4 KiB
Ruby

module MergeRequests
class CreateFromIssueService < MergeRequests::CreateService
def execute
return error('Invalid issue iid') unless issue_iid.present? && issue.present?
params[:label_ids] = issue.label_ids if issue.label_ids.any?
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
def issue_iid
@isssue_iid ||= params.delete(:issue_iid)
end
def issue
@issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: issue_iid)
end
def branch_name
@branch_name ||= issue.to_branch_name
end
def ref
project.default_branch || 'master'
end
def merge_request
MergeRequests::BuildService.new(project, current_user, merge_request_params).execute
end
def merge_request_params
{
source_project_id: project.id,
source_branch: branch_name,
target_project_id: project.id,
milestone_id: issue.milestone_id
}
end
def success(merge_request)
super().merge(merge_request: merge_request)
end
end
end