2016-02-15 13:41:40 +00:00
|
|
|
module Issues
|
|
|
|
class MoveService < Issues::BaseService
|
2016-02-24 10:52:02 +00:00
|
|
|
def initialize(project, current_user, params, issue, new_project_id)
|
2016-02-17 14:59:25 +00:00
|
|
|
super(project, current_user, params)
|
|
|
|
|
|
|
|
@issue_old = issue
|
2016-02-24 11:18:46 +00:00
|
|
|
@issue_new = nil
|
2016-02-16 10:47:00 +00:00
|
|
|
@project_old = @project
|
2016-02-24 10:52:02 +00:00
|
|
|
@project_new = Project.find(new_project_id) if new_project_id
|
2016-02-17 14:59:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return unless move?
|
|
|
|
|
2016-02-22 10:00:40 +00:00
|
|
|
# New issue tasks
|
|
|
|
#
|
2016-02-15 13:41:40 +00:00
|
|
|
open_new_issue
|
|
|
|
rewrite_notes
|
2016-02-22 10:00:40 +00:00
|
|
|
add_moved_from_note
|
|
|
|
|
|
|
|
# Old issue tasks
|
|
|
|
#
|
|
|
|
add_moved_to_note
|
2016-02-22 14:18:39 +00:00
|
|
|
close_old_issue
|
2016-02-22 10:00:40 +00:00
|
|
|
|
|
|
|
# Notifications
|
|
|
|
#
|
2016-02-15 13:41:40 +00:00
|
|
|
notify_participants
|
|
|
|
|
|
|
|
@issue_new
|
|
|
|
end
|
|
|
|
|
2016-02-17 14:59:25 +00:00
|
|
|
def move?
|
2016-02-22 11:41:33 +00:00
|
|
|
@project_new && can_move?
|
2016-02-17 14:59:25 +00:00
|
|
|
end
|
|
|
|
|
2016-02-15 13:41:40 +00:00
|
|
|
private
|
|
|
|
|
2016-02-17 14:59:25 +00:00
|
|
|
def can_move?
|
2016-02-22 11:41:33 +00:00
|
|
|
can?(@current_user, :move_issue, @project_old) &&
|
|
|
|
can?(@current_user, :move_issue, @project_new)
|
2016-02-17 14:59:25 +00:00
|
|
|
end
|
|
|
|
|
2016-02-15 13:41:40 +00:00
|
|
|
def open_new_issue
|
2016-02-24 11:18:46 +00:00
|
|
|
create_service = CreateService.new(@project_new, current_user, new_issue_params)
|
|
|
|
@issue_new = create_service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_issue_params
|
|
|
|
new_params = { id: nil, iid: nil, milestone_id: nil, label_ids: [],
|
|
|
|
project_id: @project_new.id,
|
2016-02-24 12:20:08 +00:00
|
|
|
description: rewrite_references(@issue_old) }
|
2016-02-24 11:18:46 +00:00
|
|
|
|
|
|
|
params.merge(new_params)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_notes
|
2016-02-22 10:00:40 +00:00
|
|
|
@issue_old.notes.find_each do |note|
|
2016-02-22 14:18:39 +00:00
|
|
|
new_note = note.dup
|
|
|
|
new_note.update(project: @project_new, noteable: @issue_new)
|
2016-02-22 10:00:40 +00:00
|
|
|
end
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def close_old_issue
|
2016-02-22 14:18:39 +00:00
|
|
|
@issue_old.update(state: :closed)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_participants
|
|
|
|
end
|
|
|
|
|
2016-02-22 10:00:40 +00:00
|
|
|
def add_moved_from_note
|
2016-02-16 10:47:00 +00:00
|
|
|
SystemNoteService.noteable_moved(:from, @issue_new, @project_new, @issue_old, @current_user)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
|
2016-02-22 10:00:40 +00:00
|
|
|
def add_moved_to_note
|
2016-02-16 10:47:00 +00:00
|
|
|
SystemNoteService.noteable_moved(:to, @issue_old, @project_old, @issue_new, @current_user)
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
2016-02-23 13:18:54 +00:00
|
|
|
|
2016-02-24 11:18:46 +00:00
|
|
|
def rewrite_references(mentionable)
|
2016-02-23 13:18:54 +00:00
|
|
|
references = mentionable.all_references
|
2016-02-24 11:18:46 +00:00
|
|
|
new_content = mentionable_content(mentionable).dup
|
2016-02-23 13:18:54 +00:00
|
|
|
|
|
|
|
[:issues, :merge_requests, :milestones].each do |type|
|
|
|
|
references.public_send(type).each do |mentioned|
|
|
|
|
new_content.gsub!(mentioned.to_reference,
|
|
|
|
mentioned.to_reference(@project_new))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
new_content
|
|
|
|
end
|
2016-02-24 11:18:46 +00:00
|
|
|
|
|
|
|
def mentionable_content(mentionable)
|
|
|
|
case mentionable
|
|
|
|
when Issue then mentionable.description
|
|
|
|
when Note then mentionable.note
|
|
|
|
else
|
|
|
|
raise 'Unexpected mentionable while moving an issue'
|
|
|
|
end
|
|
|
|
end
|
2016-02-15 13:41:40 +00:00
|
|
|
end
|
|
|
|
end
|