diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-03-21 23:22:21 +0100 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-03-21 23:22:21 +0100 |
commit | ae7b2ef62cdf61c990f914d776a6fdfc2bc49fa2 (patch) | |
tree | 6f0022bf04b1b566fa79b979cc9cc373cd0ebaa1 /app/services/issues/move_service.rb | |
parent | 8d544645f0ef114586212835cf011a3e268c9ec1 (diff) | |
parent | 0305dd98b32b5a989f2b84e0810cf5ddc14abd7f (diff) | |
download | gitlab-ce-ae7b2ef62cdf61c990f914d776a6fdfc2bc49fa2.tar.gz |
Merge branch 'master' into issue_12658
# Conflicts:
# app/models/issue.rb
# app/views/projects/_home_panel.html.haml
# app/views/shared/projects/_project.html.haml
# db/schema.rb
# spec/models/project_spec.rb
Diffstat (limited to 'app/services/issues/move_service.rb')
-rw-r--r-- | app/services/issues/move_service.rb | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/app/services/issues/move_service.rb b/app/services/issues/move_service.rb new file mode 100644 index 00000000000..3cfbafe1576 --- /dev/null +++ b/app/services/issues/move_service.rb @@ -0,0 +1,94 @@ +module Issues + class MoveService < Issues::BaseService + class MoveError < StandardError; end + + def execute(issue, new_project) + @old_issue = issue + @old_project = @project + @new_project = new_project + + unless issue.can_move?(current_user, new_project) + raise MoveError, 'Cannot move issue due to insufficient permissions!' + end + + if @project == new_project + raise MoveError, 'Cannot move issue to project it originates from!' + end + + # Using transaction because of a high resources footprint + # on rewriting notes (unfolding references) + # + ActiveRecord::Base.transaction do + # New issue tasks + # + @new_issue = create_new_issue + + rewrite_notes + add_note_moved_from + + # Old issue tasks + # + add_note_moved_to + close_issue + mark_as_moved + end + + notify_participants + + @new_issue + end + + private + + def create_new_issue + new_params = { id: nil, iid: nil, label_ids: [], milestone: nil, + project: @new_project, author: @old_issue.author, + description: unfold_references(@old_issue.description) } + + new_params = @old_issue.serializable_hash.merge(new_params) + CreateService.new(@new_project, @current_user, new_params).execute + end + + def rewrite_notes + @old_issue.notes.find_each do |note| + new_note = note.dup + new_params = { project: @new_project, noteable: @new_issue, + note: unfold_references(new_note.note), + created_at: note.created_at } + + new_note.update(new_params) + end + end + + def close_issue + close_service = CloseService.new(@old_project, @current_user) + close_service.execute(@old_issue, notifications: false, system_note: false) + end + + def add_note_moved_from + SystemNoteService.noteable_moved(@new_issue, @new_project, + @old_issue, @current_user, + direction: :from) + end + + def add_note_moved_to + SystemNoteService.noteable_moved(@old_issue, @old_project, + @new_issue, @current_user, + direction: :to) + end + + def unfold_references(content) + rewriter = Gitlab::Gfm::ReferenceRewriter.new(content, @old_project, + @current_user) + rewriter.rewrite(@new_project) + end + + def notify_participants + notification_service.issue_moved(@old_issue, @new_issue, @current_user) + end + + def mark_as_moved + @old_issue.update(moved_to: @new_issue) + end + end +end |