debian-mirror-gitlab/app/services/issuable/clone/content_rewriter.rb

75 lines
2.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# 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-09-30 21:07:59 +05:30
new_discussion_ids = {}
2019-02-15 15:39:39 +05:30
original_entity.notes_with_associations.find_each do |note|
new_note = note.dup
2019-09-30 21:07:59 +05:30
new_discussion_ids[note.discussion_id] ||= Discussion.discussion_id(new_note)
2019-02-15 15:39:39 +05:30
new_params = {
2019-09-30 21:07:59 +05:30
project: new_entity.project,
noteable: new_entity,
discussion_id: new_discussion_ids[note.discussion_id],
2019-02-15 15:39:39 +05:30
note: rewrite_content(new_note.note),
2019-09-04 21:01:54 +05:30
note_html: nil,
2019-02-15 15:39:39 +05:30
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-12-21 20:55:43 +05:30
# 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
2019-02-15 15:39:39 +05:30
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