diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /app/services/notes/copy_service.rb | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'app/services/notes/copy_service.rb')
-rw-r--r-- | app/services/notes/copy_service.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/app/services/notes/copy_service.rb b/app/services/notes/copy_service.rb new file mode 100644 index 00000000000..6e5b4596602 --- /dev/null +++ b/app/services/notes/copy_service.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +# This service copies Notes from one Noteable to another. +# +# It expects the calling code to have performed the necessary authorization +# checks in order to allow the copy to happen. +module Notes + class CopyService + def initialize(current_user, from_noteable, to_noteable) + raise ArgumentError, 'Noteables must be different' if from_noteable == to_noteable + + @current_user = current_user + @from_noteable = from_noteable + @to_noteable = to_noteable + @from_project = from_noteable.project + @new_discussion_ids = {} + end + + def execute + from_noteable.notes_with_associations.find_each do |note| + copy_note(note) + end + + ServiceResponse.success + end + + private + + attr_reader :from_noteable, :to_noteable, :from_project, :current_user, :new_discussion_ids + + def copy_note(note) + new_note = note.dup + new_params = params_from_note(note, new_note) + new_note.update!(new_params) + + copy_award_emoji(note, new_note) + end + + def params_from_note(note, new_note) + new_discussion_ids[note.discussion_id] ||= Discussion.discussion_id(new_note) + rewritten_note = MarkdownContentRewriterService.new(current_user, note.note, from_project, to_noteable.resource_parent).execute + + new_params = { + project: to_noteable.project, + noteable: to_noteable, + discussion_id: new_discussion_ids[note.discussion_id], + note: rewritten_note, + note_html: nil, + 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 + + # 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 + end + + new_params + end + + def copy_award_emoji(from_note, to_note) + AwardEmojis::CopyService.new(from_note, to_note).execute + end + end +end |