diff --git a/app/services/issues/move_service.rb b/app/services/issues/move_service.rb index 37aaeb2885760d06b67ae7fee9609d4b0476a438..782b8b6a430281d94b46995ed2fee08b3234be2e 100644 --- a/app/services/issues/move_service.rb +++ b/app/services/issues/move_service.rb @@ -57,7 +57,10 @@ module Issues def rewrite_notes @issue_old.notes.find_each do |note| new_note = note.dup - new_note.update(project: @project_new, noteable: @issue_new) + new_params = { project: @project_new, noteable: @issue_new, + note: rewrite_references(new_note) } + + new_note.update(new_params) end end @@ -79,8 +82,10 @@ module Issues def rewrite_references(mentionable) references = mentionable.all_references new_content = mentionable_content(mentionable).dup + cross_project_refs = [:issues, :merge_requests, :milestones, + :snippets, :commits, :commit_ranges] - [:issues, :merge_requests, :milestones].each do |type| + cross_project_refs.each do |type| references.public_send(type).each do |mentioned| new_content.gsub!(mentioned.to_reference, mentioned.to_reference(@project_new)) diff --git a/spec/services/issues/move_service_spec.rb b/spec/services/issues/move_service_spec.rb index 282dfd9187d0f11f986cbdd2cd9dede2055a8c3f..596da74b572fef1f2e44474d5ab13b95c00b7429 100644 --- a/spec/services/issues/move_service_spec.rb +++ b/spec/services/issues/move_service_spec.rb @@ -94,27 +94,54 @@ describe Issues::MoveService, services: true do end context 'issue with notes' do - let(:note_contents) do - ['Some system note 1', 'Some comment', 'Some system note 2'] - end + context 'notes without references' do + let(:notes_params) do + [{ system: false, note: 'Some comment 1' }, + { system: true, note: 'Some system note' }, + { system: false, note: 'Some comment 2' }] + end - before do - note_params = { noteable: old_issue, project: old_project, author: user } - create(:system_note, note_params.merge(note: note_contents.first)) - create(:note, note_params.merge(note: note_contents.second)) - create(:system_note, note_params.merge(note: note_contents.third)) - end + before do + note_params = { noteable: old_issue, project: old_project, author: author } + notes_params.each do |note| + create(:note, note_params.merge(note)) + end + end - include_context 'issue move executed' + include_context 'issue move executed' - let(:new_notes) { new_issue.notes.order('id ASC').pluck(:note) } + let(:all_notes) { new_issue.notes.order('id ASC') } + let(:system_notes) { all_notes.system } + let(:user_notes) { all_notes.user } + + it 'rewrites existing notes in valid order' do + expect(all_notes.pluck(:note).first(3)) + .to eq notes_params.map { |n| n[:note] } + end - it 'rewrites existing system notes in valid order' do - expect(new_notes.first(3)).to eq note_contents + it 'adds a system note about move after rewritten notes' do + expect(system_notes.last.note).to match /^Moved from/ + end + + it 'preserves orignal author of comment' do + expect(user_notes.pluck(:author_id)).to all(eq(author.id)) + end end - it 'adds a system note about move after rewritten notes' do - expect(new_notes.last).to match /^Moved from/ + context 'notes with references' do + before do + create(:merge_request, source_project: old_project) + create(:note, noteable: old_issue, project: old_project, author: author, + note: 'Note with reference to merge request !1') + end + + include_context 'issue move executed' + let(:new_note) { new_issue.notes.first } + + it 'rewrites references using a cross reference to old project' do + expect(new_note.note) + .to eq "Note with reference to merge request #{old_project.to_reference}!1" + end end end