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
|
2021-05-11 23:10:21 -04:00
|
|
|
# TODO: This constructor does not use the "params:" argument from the superclass,
|
|
|
|
# but instead has a custom "mr_params:" argument. This is because historically,
|
|
|
|
# prior to named arguments being introduced to the constructor, it never passed
|
|
|
|
# along the third positional argument when calling `super`.
|
|
|
|
# This should be changed, in order to be consistent (all subclasses should pass
|
|
|
|
# along all of the arguments to the superclass, otherwise it is probably not an
|
|
|
|
# "is a" relationship). However, we need to be sure that passing the params
|
|
|
|
# argument to `super` (especially target_project_id) will not cause any unexpected
|
|
|
|
# behavior in the superclass. Since the addition of the named arguments is
|
|
|
|
# intended to be a low-risk pure refactor, we will defer this fix
|
|
|
|
# to this follow-on issue:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/328726
|
|
|
|
def initialize(project:, current_user:, mr_params: {})
|
2017-11-25 06:33:05 -05:00
|
|
|
# branch - the name of new branch
|
|
|
|
# ref - the source of new branch.
|
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
@branch_name = mr_params[:branch_name]
|
|
|
|
@issue_iid = mr_params[:issue_iid]
|
|
|
|
@ref = mr_params[:ref]
|
|
|
|
@target_project_id = mr_params[:target_project_id]
|
2017-11-25 06:33:05 -05:00
|
|
|
|
2021-05-11 23:10:21 -04:00
|
|
|
super(project: project, current_user: current_user)
|
2017-11-25 06:33:05 -05:00
|
|
|
end
|
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
def execute
|
2019-06-19 16:41:21 -04:00
|
|
|
return error('Project not found') if target_project.blank?
|
|
|
|
return error('Not allowed to create merge request') unless can_create_merge_request?
|
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
|
|
|
|
2019-12-03 13:06:49 -05:00
|
|
|
result = ::Branches::CreateService.new(target_project, current_user).execute(branch_name, ref)
|
2017-05-04 04:09:21 -04:00
|
|
|
return result if result[:status] == :error
|
|
|
|
|
|
|
|
new_merge_request = create(merge_request)
|
|
|
|
|
|
|
|
if new_merge_request.valid?
|
2021-02-16 01:09:07 -05:00
|
|
|
merge_request_activity_counter.track_mr_create_from_issue(user: current_user)
|
2019-06-28 18:08:26 -04:00
|
|
|
SystemNoteService.new_merge_request(issue, project, current_user, new_merge_request)
|
2018-10-19 03:16:58 -04:00
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
success(new_merge_request)
|
|
|
|
else
|
2019-06-28 18:08:26 -04:00
|
|
|
SystemNoteService.new_issue_branch(issue, project, current_user, branch_name, branch_project: target_project)
|
2018-10-19 03:16:58 -04:00
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
error(new_merge_request.errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-19 16:41:21 -04:00
|
|
|
def can_create_merge_request?
|
|
|
|
can?(current_user, :create_merge_request_from, target_project)
|
|
|
|
end
|
|
|
|
|
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
|
2019-09-12 06:23:24 -04:00
|
|
|
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-06-08 08:28:43 -04:00
|
|
|
|
2019-09-12 06:23:24 -04:00
|
|
|
def ref_is_tag?
|
|
|
|
target_project.repository.tag_exists?(@ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
2021-05-04 08:10:04 -04:00
|
|
|
target_project.default_branch_or_main
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
2021-05-11 23:10:21 -04:00
|
|
|
MergeRequests::BuildService.new(project: target_project, current_user: current_user, params: merge_request_params).execute
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params
|
|
|
|
{
|
2017-11-25 06:33:05 -05:00
|
|
|
issue_iid: @issue_iid,
|
2019-06-19 16:41:21 -04:00
|
|
|
source_project_id: target_project.id,
|
2017-05-04 04:09:21 -04:00
|
|
|
source_branch: branch_name,
|
2019-06-19 16:41:21 -04:00
|
|
|
target_project_id: target_project.id,
|
2021-01-04 04:10:04 -05:00
|
|
|
target_branch: target_branch,
|
|
|
|
assignee_ids: [current_user.id]
|
2017-05-04 04:09:21 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-12 06:23:24 -04:00
|
|
|
def target_branch
|
|
|
|
if ref_is_branch?
|
|
|
|
@ref
|
|
|
|
else
|
|
|
|
default_branch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-04 04:09:21 -04:00
|
|
|
def success(merge_request)
|
|
|
|
super().merge(merge_request: merge_request)
|
|
|
|
end
|
2019-06-19 16:41:21 -04:00
|
|
|
|
|
|
|
def target_project
|
|
|
|
@target_project ||=
|
|
|
|
if @target_project_id.present?
|
|
|
|
project.forks.find_by_id(@target_project_id)
|
|
|
|
else
|
|
|
|
project
|
|
|
|
end
|
|
|
|
end
|
2017-05-04 04:09:21 -04:00
|
|
|
end
|
|
|
|
end
|