2018-10-29 05:05:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Issuable
|
|
|
|
module Clone
|
|
|
|
class ContentRewriter < ::Issuable::Clone::BaseService
|
|
|
|
def initialize(current_user, original_entity, new_entity)
|
|
|
|
@current_user = current_user
|
|
|
|
@original_entity = original_entity
|
|
|
|
@new_entity = new_entity
|
|
|
|
@project = original_entity.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
rewrite_description
|
|
|
|
rewrite_award_emoji(original_entity, new_entity)
|
|
|
|
rewrite_notes
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def rewrite_description
|
|
|
|
new_entity.update(description: rewrite_content(original_entity.description))
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_notes
|
2019-07-05 11:45:07 -04:00
|
|
|
new_discussion_ids = {}
|
2018-10-29 05:05:47 -04:00
|
|
|
original_entity.notes_with_associations.find_each do |note|
|
|
|
|
new_note = note.dup
|
2019-07-05 11:45:07 -04:00
|
|
|
new_discussion_ids[note.discussion_id] ||= Discussion.discussion_id(new_note)
|
2018-10-29 05:05:47 -04:00
|
|
|
new_params = {
|
2019-07-05 11:45:07 -04:00
|
|
|
project: new_entity.project,
|
|
|
|
noteable: new_entity,
|
|
|
|
discussion_id: new_discussion_ids[note.discussion_id],
|
2018-10-29 05:05:47 -04:00
|
|
|
note: rewrite_content(new_note.note),
|
2019-05-23 09:11:29 -04:00
|
|
|
note_html: nil,
|
2018-10-29 05:05:47 -04:00
|
|
|
created_at: note.created_at,
|
|
|
|
updated_at: note.updated_at
|
|
|
|
}
|
|
|
|
|
|
|
|
if note.system_note_metadata
|
|
|
|
new_params[:system_note_metadata] = note.system_note_metadata.dup
|
2019-10-18 07:11:44 -04:00
|
|
|
|
|
|
|
# TODO: Implement copying of description versions when an issue is moved
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/32300
|
|
|
|
new_params[:system_note_metadata].description_version = nil
|
2018-10-29 05:05:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
new_note.update(new_params)
|
|
|
|
|
|
|
|
rewrite_award_emoji(note, new_note)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_content(content)
|
|
|
|
return unless content
|
|
|
|
|
|
|
|
rewriters = [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter]
|
|
|
|
|
|
|
|
rewriters.inject(content) do |text, klass|
|
|
|
|
rewriter = klass.new(text, old_project, current_user)
|
|
|
|
rewriter.rewrite(new_parent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_award_emoji(old_awardable, new_awardable)
|
|
|
|
old_awardable.award_emoji.each do |award|
|
|
|
|
new_award = award.dup
|
|
|
|
new_award.awardable = new_awardable
|
|
|
|
new_award.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|