Preserve original author when moving issue

This also wrapps entire process into transation, as rewriting references
may have large memory footprint.
This commit is contained in:
Grzegorz Bizon 2016-03-14 08:25:37 +01:00
parent 4354bfaba5
commit 310b417b0f
2 changed files with 22 additions and 24 deletions

View file

@ -4,7 +4,7 @@ module Issues
super(project, current_user, params) super(project, current_user, params)
@issue_old = issue @issue_old = issue
@issue_new = nil @issue_new = issue.dup
@project_old = @project @project_old = @project
@project_new = Project.find(new_project_id) if new_project_id @project_new = Project.find(new_project_id) if new_project_id
end end
@ -12,16 +12,18 @@ module Issues
def execute def execute
return unless move? return unless move?
# New issue tasks ActiveRecord::Base.transaction do
# # New issue tasks
open_new_issue #
rewrite_notes open_new_issue
add_moved_from_note rewrite_notes
add_moved_from_note
# Old issue tasks # Old issue tasks
# #
add_moved_to_note add_moved_to_note
close_old_issue close_old_issue
end
# Notifications # Notifications
# #
@ -42,16 +44,12 @@ module Issues
end end
def open_new_issue def open_new_issue
create_service = CreateService.new(@project_new, current_user, new_issue_params) @issue_new.iid = nil
@issue_new = create_service.execute @issue_new.project = @project_new
end @issue_new.labels = []
@issue_new.milestone = nil
def new_issue_params @issue_new.description = rewrite_references(@issue_old)
new_params = { id: nil, iid: nil, milestone_id: nil, label_ids: [], @issue_new.save!
project_id: @project_new.id,
description: rewrite_references(@issue_old) }
params.merge(new_params)
end end
def rewrite_notes def rewrite_notes

View file

@ -74,13 +74,13 @@ describe Issues::MoveService, services: true do
expect(new_issue.persisted?).to be true expect(new_issue.persisted?).to be true
end end
it 'persist all changes' do it 'persists all changes' do
expect(old_issue.changed?).to be false expect(old_issue.changed?).to be false
expect(new_issue.changed?).to be false expect(new_issue.persisted?).to be true
end end
it 'changes author' do it 'preserves author' do
expect(new_issue.author).to eq user expect(new_issue.author).to eq author
end end
it 'removes data that is invalid in new context' do it 'removes data that is invalid in new context' do