2018-10-29 05:05:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Issuable
|
|
|
|
module Clone
|
|
|
|
class BaseService < IssuableBaseService
|
2021-03-04 07:11:11 -05:00
|
|
|
attr_reader :original_entity, :new_entity, :target_project
|
2018-10-29 05:05:47 -04:00
|
|
|
|
|
|
|
alias_method :old_project, :project
|
|
|
|
|
2021-03-04 07:11:11 -05:00
|
|
|
def execute(original_entity, target_project = nil)
|
2018-10-29 05:05:47 -04:00
|
|
|
@original_entity = original_entity
|
2021-03-04 07:11:11 -05:00
|
|
|
@target_project = target_project
|
2018-10-29 05:05:47 -04:00
|
|
|
|
|
|
|
# Using transaction because of a high resources footprint
|
|
|
|
# on rewriting notes (unfolding references)
|
|
|
|
#
|
2021-07-23 08:09:05 -04:00
|
|
|
ApplicationRecord.transaction do
|
2018-10-29 05:05:47 -04:00
|
|
|
@new_entity = create_new_entity
|
|
|
|
|
|
|
|
update_new_entity
|
|
|
|
update_old_entity
|
|
|
|
create_notes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-11 11:10:08 -04:00
|
|
|
def copy_award_emoji
|
|
|
|
AwardEmojis::CopyService.new(original_entity, new_entity).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_notes
|
|
|
|
Notes::CopyService.new(current_user, original_entity, new_entity).execute
|
|
|
|
end
|
|
|
|
|
2018-10-29 05:05:47 -04:00
|
|
|
def update_new_entity
|
2020-08-11 11:10:08 -04:00
|
|
|
update_new_entity_description
|
|
|
|
update_new_entity_attributes
|
|
|
|
copy_award_emoji
|
|
|
|
copy_notes
|
|
|
|
end
|
2018-10-29 05:05:47 -04:00
|
|
|
|
2020-08-11 11:10:08 -04:00
|
|
|
def update_new_entity_description
|
|
|
|
rewritten_description = MarkdownContentRewriterService.new(
|
|
|
|
current_user,
|
|
|
|
original_entity.description,
|
|
|
|
original_entity.project,
|
2020-08-19 23:10:04 -04:00
|
|
|
new_parent
|
2020-08-11 11:10:08 -04:00
|
|
|
).execute
|
|
|
|
|
|
|
|
new_entity.update!(description: rewritten_description)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_new_entity_attributes
|
|
|
|
AttributesRewriter.new(current_user, original_entity, new_entity).execute
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_old_entity
|
|
|
|
close_issue
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notes
|
|
|
|
add_note_from
|
|
|
|
add_note_to
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_issue
|
2021-05-11 23:10:21 -04:00
|
|
|
close_service = Issues::CloseService.new(project: old_project, current_user: current_user)
|
2021-06-20 20:10:50 -04:00
|
|
|
close_service.execute(original_entity, notifications: false, system_note: true)
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_parent
|
2020-08-19 23:10:04 -04:00
|
|
|
new_entity.resource_parent
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def group
|
|
|
|
if new_entity.project&.group && current_user.can?(:read_group, new_entity.project.group)
|
|
|
|
new_entity.project.group
|
|
|
|
end
|
|
|
|
end
|
2021-03-04 07:11:11 -05:00
|
|
|
|
|
|
|
def relative_position
|
|
|
|
return if original_entity.project.root_ancestor.id != target_project.root_ancestor.id
|
|
|
|
|
|
|
|
original_entity.relative_position
|
|
|
|
end
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Issuable::Clone::BaseService.prepend_mod_with('Issuable::Clone::BaseService')
|