summaryrefslogtreecommitdiff
path: root/app/services/issues/move_service.rb
blob: 60e0d1eec3d32ccae5b3fd8ee4bd2fd1151a01e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Issues
  class MoveService < Issuable::Clone::BaseService
    MoveError = Class.new(StandardError)

    def execute(issue, target_project)
      @target_project = target_project

      unless issue.can_move?(current_user, @target_project)
        raise MoveError, s_('MoveIssue|Cannot move issue due to insufficient permissions!')
      end

      if @project == @target_project
        raise MoveError, s_('MoveIssue|Cannot move issue to project it originates from!')
      end

      super

      notify_participants

      # Updates old issue sent notifications allowing
      # to receive service desk emails on the new moved issue.
      update_service_desk_sent_notifications

      new_entity
    end

    private

    def update_service_desk_sent_notifications
      return unless original_entity.from_service_desk?

      original_entity
        .sent_notifications.update_all(project_id: new_entity.project_id, noteable_id: new_entity.id)
    end

    def update_old_entity
      super

      rewrite_related_issues
      mark_as_moved
    end

    def create_new_entity
      new_params = {
                     id: nil,
                     iid: nil,
                     project: @target_project,
                     author: original_entity.author,
                     assignee_ids: original_entity.assignee_ids
                   }

      new_params = original_entity.serializable_hash.symbolize_keys.merge(new_params)

      # Skip creation of system notes for existing attributes of the issue. The system notes of the old
      # issue are copied over so we don't want to end up with duplicate notes.
      CreateService.new(@target_project, @current_user, new_params).execute(skip_system_notes: true)
    end

    def mark_as_moved
      original_entity.update(moved_to: new_entity)
    end

    def rewrite_related_issues
      source_issue_links = IssueLink.for_source_issue(original_entity)
      source_issue_links.update_all(source_id: new_entity.id)

      target_issue_links = IssueLink.for_target_issue(original_entity)
      target_issue_links.update_all(target_id: new_entity.id)
    end

    def notify_participants
      notification_service.async.issue_moved(original_entity, new_entity, @current_user)
    end

    def add_note_from
      SystemNoteService.noteable_moved(new_entity, @target_project,
                                       original_entity, current_user,
                                       direction: :from)
    end

    def add_note_to
      SystemNoteService.noteable_moved(original_entity, old_project,
                                       new_entity, current_user,
                                       direction: :to)
    end
  end
end

Issues::MoveService.prepend_if_ee('EE::Issues::MoveService')