2016-02-15 13:41:40 +00:00
|
|
|
module Issues
|
|
|
|
class MoveService < Issues::BaseService
|
2016-03-18 13:48:55 +00:00
|
|
|
class MoveError < StandardError; end
|
2016-02-17 14:59:25 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
def execute(issue, new_project)
|
|
|
|
@old_issue = issue
|
|
|
|
@old_project = @project
|
|
|
|
@new_project = new_project
|
2016-03-15 09:14:39 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
unless issue.can_move?(current_user, new_project)
|
|
|
|
raise MoveError, 'Cannot move issue due to insufficient permissions!'
|
2016-03-15 09:14:39 +00:00
|
|
|
end
|
2016-03-15 10:35:40 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
if @project == new_project
|
|
|
|
raise MoveError, 'Cannot move issue to project it originates from!'
|
2016-03-15 10:35:40 +00:00
|
|
|
end
|
2016-02-17 14:59:25 +00:00
|
|
|
|
2016-03-16 09:24:44 +00:00
|
|
|
# Using transaction because of a high resources footprint
|
2016-03-15 10:35:40 +00:00
|
|
|
# on rewriting notes (unfolding references)
|
2016-03-15 09:14:39 +00:00
|
|
|
#
|
2016-03-14 07:25:37 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
# New issue tasks
|
|
|
|
#
|
2016-03-18 13:48:55 +00:00
|
|
|
@new_issue = create_new_issue
|
|
|
|
|
2016-03-14 07:25:37 +00:00
|
|
|
rewrite_notes
|
2016-03-18 13:48:55 +00:00
|
|
|
add_note_moved_from
|
2016-02-22 10:00:40 +00:00
|
|
|
|
2016-03-14 07:25:37 +00:00
|
|
|
# Old issue tasks
|
|
|
|
#
|
2016-03-18 13:48:55 +00:00
|
|
|
add_note_moved_to
|
|
|
|
close_issue
|
2016-03-17 10:11:22 +00:00
|
|
|
mark_as_moved
|
2016-03-14 07:25:37 +00:00
|
|
|
end
|
2016-02-22 10:00:40 +00:00
|
|
|
|
2016-03-15 09:14:39 +00:00
|
|
|
notify_participants
|
2016-02-15 13:41:40 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
@new_issue
|
2016-02-17 14:59:25 +00:00
|
|
|
end
|
|
|
|
|
2016-02-15 13:41:40 +00:00
|
|
|
private
|
|
|
|
|
2016-03-14 10:25:04 +00:00
|
|
|
def create_new_issue
|
2016-03-18 13:48:55 +00:00
|
|
|
new_params = { id: nil, iid: nil, label_ids: [], milestone: nil,
|
|
|
|
project: @new_project, author: @old_issue.author,
|
2016-03-24 11:28:43 +00:00
|
|
|
description: rewrite_content(@old_issue.description) }
|
2016-03-14 12:24:56 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
new_params = @old_issue.serializable_hash.merge(new_params)
|
|
|
|
CreateService.new(@new_project, @current_user, new_params).execute
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_notes
|
2016-03-18 13:48:55 +00:00
|
|
|
@old_issue.notes.find_each do |note|
|
2016-02-22 14:18:39 +00:00
|
|
|
new_note = note.dup
|
2016-03-18 13:48:55 +00:00
|
|
|
new_params = { project: @new_project, noteable: @new_issue,
|
2016-03-24 11:28:43 +00:00
|
|
|
note: rewrite_content(new_note.note),
|
2016-03-23 11:41:53 +00:00
|
|
|
created_at: note.created_at,
|
|
|
|
updated_at: note.updated_at }
|
2016-02-24 14:38:52 +00:00
|
|
|
|
|
|
|
new_note.update(new_params)
|
2016-02-22 10:00:40 +00:00
|
|
|
end
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
2016-03-24 11:28:43 +00:00
|
|
|
def rewrite_content(content)
|
|
|
|
return unless content
|
|
|
|
|
2016-03-29 11:21:57 +00:00
|
|
|
rewriters = [Gitlab::Gfm::ReferenceRewriter,
|
|
|
|
Gitlab::Gfm::UploadsRewriter]
|
2016-03-24 11:28:43 +00:00
|
|
|
|
2016-03-29 11:21:57 +00:00
|
|
|
rewriters.inject(content) do |text, klass|
|
|
|
|
rewriter = klass.new(text, @old_project, @current_user)
|
2016-03-30 08:42:39 +00:00
|
|
|
rewriter.rewrite(@new_project)
|
2016-03-29 11:21:57 +00:00
|
|
|
end
|
2016-03-24 11:28:43 +00:00
|
|
|
end
|
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
def close_issue
|
|
|
|
close_service = CloseService.new(@old_project, @current_user)
|
|
|
|
close_service.execute(@old_issue, notifications: false, system_note: false)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
def add_note_moved_from
|
|
|
|
SystemNoteService.noteable_moved(@new_issue, @new_project,
|
|
|
|
@old_issue, @current_user,
|
|
|
|
direction: :from)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
def add_note_moved_to
|
|
|
|
SystemNoteService.noteable_moved(@old_issue, @old_project,
|
|
|
|
@new_issue, @current_user,
|
|
|
|
direction: :to)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
2016-02-23 13:18:54 +00:00
|
|
|
|
2016-03-24 11:28:43 +00:00
|
|
|
def mark_as_moved
|
|
|
|
@old_issue.update(moved_to: @new_issue)
|
2016-02-23 13:18:54 +00:00
|
|
|
end
|
2016-02-24 11:18:46 +00:00
|
|
|
|
2016-03-14 12:24:56 +00:00
|
|
|
def notify_participants
|
2016-03-18 13:48:55 +00:00
|
|
|
notification_service.issue_moved(@old_issue, @new_issue, @current_user)
|
2016-03-14 12:24:56 +00:00
|
|
|
end
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
end
|