2016-02-15 08:41:40 -05:00
|
|
|
module Issues
|
|
|
|
class MoveService < Issues::BaseService
|
2016-02-24 05:52:02 -05:00
|
|
|
def initialize(project, current_user, params, issue, new_project_id)
|
2016-02-17 09:59:25 -05:00
|
|
|
super(project, current_user, params)
|
|
|
|
|
|
|
|
@issue_old = issue
|
2016-03-15 05:14:39 -04:00
|
|
|
@issue_new = nil
|
2016-02-16 05:47:00 -05:00
|
|
|
@project_old = @project
|
2016-03-15 05:14:39 -04:00
|
|
|
|
|
|
|
if new_project_id
|
|
|
|
@project_new = Project.find(new_project_id)
|
|
|
|
end
|
2016-03-15 06:35:40 -04:00
|
|
|
|
|
|
|
if @project_new == @project_old
|
|
|
|
raise StandardError, 'Cannot move issue to project it originates from!'
|
|
|
|
end
|
2016-02-17 09:59:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return unless move?
|
|
|
|
|
2016-03-15 06:35:40 -04:00
|
|
|
# Using trasaction because of a high resources footprint
|
|
|
|
# on rewriting notes (unfolding references)
|
2016-03-15 05:14:39 -04:00
|
|
|
#
|
2016-03-14 03:25:37 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
# New issue tasks
|
|
|
|
#
|
2016-03-14 06:25:04 -04:00
|
|
|
create_new_issue
|
2016-03-14 03:25:37 -04:00
|
|
|
rewrite_notes
|
|
|
|
add_moved_from_note
|
2016-02-22 05:00:40 -05:00
|
|
|
|
2016-03-14 03:25:37 -04:00
|
|
|
# Old issue tasks
|
|
|
|
#
|
|
|
|
add_moved_to_note
|
|
|
|
close_old_issue
|
|
|
|
end
|
2016-02-22 05:00:40 -05:00
|
|
|
|
2016-03-15 05:14:39 -04:00
|
|
|
notify_participants
|
2016-02-15 08:41:40 -05:00
|
|
|
|
|
|
|
@issue_new
|
|
|
|
end
|
|
|
|
|
2016-02-17 09:59:25 -05:00
|
|
|
def move?
|
2016-02-22 06:41:33 -05:00
|
|
|
@project_new && can_move?
|
2016-02-17 09:59:25 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 08:41:40 -05:00
|
|
|
private
|
|
|
|
|
2016-02-17 09:59:25 -05:00
|
|
|
def can_move?
|
2016-03-14 03:30:37 -04:00
|
|
|
can?(@current_user, :admin_issue, @project_old) &&
|
|
|
|
can?(@current_user, :admin_issue, @project_new)
|
2016-02-17 09:59:25 -05:00
|
|
|
end
|
|
|
|
|
2016-03-14 06:25:04 -04:00
|
|
|
def create_new_issue
|
2016-03-15 05:14:39 -04:00
|
|
|
new_params = { id: nil, iid: nil, milestone: nil, label_ids: [],
|
|
|
|
project: @project_new, author: @issue_old.author,
|
|
|
|
description: rewrite_references(@issue_old) }
|
2016-03-14 08:24:56 -04:00
|
|
|
|
2016-03-15 05:14:39 -04:00
|
|
|
create_service = CreateService.new(@project_new, @current_user,
|
|
|
|
params.merge(new_params))
|
2016-03-14 08:24:56 -04:00
|
|
|
|
2016-03-15 05:14:39 -04:00
|
|
|
@issue_new = create_service.execute(set_author: false)
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_notes
|
2016-02-22 05:00:40 -05:00
|
|
|
@issue_old.notes.find_each do |note|
|
2016-02-22 09:18:39 -05:00
|
|
|
new_note = note.dup
|
2016-02-24 09:38:52 -05:00
|
|
|
new_params = { project: @project_new, noteable: @issue_new,
|
|
|
|
note: rewrite_references(new_note) }
|
|
|
|
|
|
|
|
new_note.update(new_params)
|
2016-02-22 05:00:40 -05:00
|
|
|
end
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close_old_issue
|
2016-03-15 05:14:39 -04:00
|
|
|
close_service = CloseService.new(@project_new, @current_user)
|
|
|
|
close_service.execute(@issue_old, notifications: false, system_note: false)
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
|
|
|
|
2016-02-22 05:00:40 -05:00
|
|
|
def add_moved_from_note
|
2016-02-29 03:49:22 -05:00
|
|
|
SystemNoteService.noteable_moved(:from, @issue_new, @project_new,
|
|
|
|
@issue_old, @current_user)
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
|
|
|
|
2016-02-22 05:00:40 -05:00
|
|
|
def add_moved_to_note
|
2016-02-29 03:49:22 -05:00
|
|
|
SystemNoteService.noteable_moved(:to, @issue_old, @project_old,
|
|
|
|
@issue_new, @current_user)
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
2016-02-23 08:18:54 -05:00
|
|
|
|
2016-02-24 10:27:06 -05:00
|
|
|
def rewrite_references(noteable)
|
2016-02-29 03:49:22 -05:00
|
|
|
content = noteable_content(noteable).dup
|
2016-03-07 07:09:53 -05:00
|
|
|
unfolder = Gitlab::Gfm::ReferenceUnfolder.new(content, @project_old)
|
|
|
|
unfolder.unfold(@project_new)
|
2016-02-23 08:18:54 -05:00
|
|
|
end
|
2016-02-24 06:18:46 -05:00
|
|
|
|
2016-02-24 10:27:06 -05:00
|
|
|
def noteable_content(noteable)
|
|
|
|
case noteable
|
2016-03-15 05:14:39 -04:00
|
|
|
when Issue then noteable.description
|
|
|
|
when Note then noteable.note
|
2016-02-24 06:18:46 -05:00
|
|
|
else
|
2016-03-15 05:14:39 -04:00
|
|
|
raise 'Unexpected noteable while moving an issue!'
|
2016-02-24 06:18:46 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-14 08:24:56 -04:00
|
|
|
|
|
|
|
def notify_participants
|
2016-03-15 06:35:40 -04:00
|
|
|
notification_service.issue_moved(@issue_old, @issue_new, @current_user)
|
2016-03-14 08:24:56 -04:00
|
|
|
end
|
2016-02-15 08:41:40 -05:00
|
|
|
end
|
|
|
|
end
|