2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module MergeRequests
|
|
|
|
class CreateFromIssueService < MergeRequests::CreateService
|
2018-03-17 18:26:18 +05:30
|
|
|
def initialize(project, user, params)
|
|
|
|
# branch - the name of new branch
|
|
|
|
# ref - the source of new branch.
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
@branch_name = params[:branch_name]
|
|
|
|
@issue_iid = params[:issue_iid]
|
|
|
|
@ref = params[:ref]
|
|
|
|
@target_project_id = params[:target_project_id]
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
super(project, user)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def execute
|
2019-09-30 21:07:59 +05:30
|
|
|
return error('Project not found') if target_project.blank?
|
|
|
|
return error('Not allowed to create merge request') unless can_create_merge_request?
|
2018-03-17 18:26:18 +05:30
|
|
|
return error('Invalid issue iid') unless @issue_iid.present? && issue.present?
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
result = ::Branches::CreateService.new(target_project, current_user).execute(branch_name, ref)
|
2017-08-17 22:00:37 +05:30
|
|
|
return result if result[:status] == :error
|
|
|
|
|
|
|
|
new_merge_request = create(merge_request)
|
|
|
|
|
|
|
|
if new_merge_request.valid?
|
2019-02-15 15:39:39 +05:30
|
|
|
SystemNoteService.new_merge_request(issue, project, current_user, new_merge_request)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
success(new_merge_request)
|
|
|
|
else
|
2019-09-30 21:07:59 +05:30
|
|
|
SystemNoteService.new_issue_branch(issue, project, current_user, branch_name, branch_project: target_project)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
error(new_merge_request.errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def can_create_merge_request?
|
|
|
|
can?(current_user, :create_merge_request_from, target_project)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def issue
|
2018-03-17 18:26:18 +05:30
|
|
|
@issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def branch_name
|
2018-03-17 18:26:18 +05:30
|
|
|
@branch ||= @branch_name || issue.to_branch_name
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
2019-12-04 20:38:33 +05:30
|
|
|
if valid_ref?
|
|
|
|
@ref
|
|
|
|
else
|
|
|
|
default_branch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_ref?
|
|
|
|
ref_is_branch? || ref_is_tag?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref_is_branch?
|
|
|
|
target_project.repository.branch_exists?(@ref)
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def ref_is_tag?
|
|
|
|
target_project.repository.tag_exists?(@ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
2019-09-30 21:07:59 +05:30
|
|
|
target_project.default_branch || 'master'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
2019-09-30 21:07:59 +05:30
|
|
|
MergeRequests::BuildService.new(target_project, current_user, merge_request_params).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params
|
|
|
|
{
|
2018-03-17 18:26:18 +05:30
|
|
|
issue_iid: @issue_iid,
|
2019-09-30 21:07:59 +05:30
|
|
|
source_project_id: target_project.id,
|
2017-08-17 22:00:37 +05:30
|
|
|
source_branch: branch_name,
|
2019-09-30 21:07:59 +05:30
|
|
|
target_project_id: target_project.id,
|
2019-12-04 20:38:33 +05:30
|
|
|
target_branch: target_branch
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def target_branch
|
|
|
|
if ref_is_branch?
|
|
|
|
@ref
|
|
|
|
else
|
|
|
|
default_branch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def success(merge_request)
|
|
|
|
super().merge(merge_request: merge_request)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
def target_project
|
|
|
|
@target_project ||=
|
|
|
|
if @target_project_id.present?
|
|
|
|
project.forks.find_by_id(@target_project_id)
|
|
|
|
else
|
|
|
|
project
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|