File: move_service.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (224 lines) | stat: -rw-r--r-- 6,406 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# frozen_string_literal: true

module Issues
  class MoveService < Issuable::Clone::BaseService
    extend ::Gitlab::Utils::Override

    BATCH_SIZE = 100

    MoveError = Class.new(StandardError)

    def execute(issue, target_project, move_any_issue_type = false)
      @move_any_issue_type = move_any_issue_type
      @target_project = target_project

      verify_can_move_issue!(issue, target_project)

      super(issue, target_project)

      notify_participants

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

      copy_email_participants
      queue_copy_designs
      copy_timelogs

      new_entity
    end

    private

    attr_reader :target_project, :move_any_issue_type

    override :after_clone_actions
    def after_clone_actions
      move_children
    end

    def move_children
      WorkItems::ParentLink.for_parents(original_entity).each do |link|
        new_child = self.class.new(
          container: container,
          current_user: current_user
        ).execute(
          ::Issue.find(link.work_item_id),
          target_project,
          true
        )

        WorkItems::ParentLink.create!(work_item_id: new_child.id, work_item_parent_id: new_entity.id)
      end
    end

    def verify_can_move_issue!(issue, target_project)
      unless issue.supports_move_and_clone? || move_any_issue_type
        raise MoveError, s_('MoveIssue|Cannot move issues of \'%{issue_type}\' type.') % { issue_type: issue.issue_type }
      end

      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
    end

    def update_service_desk_sent_notifications
      context = { project_id: new_entity.project_id, noteable_id: new_entity.id }

      original_entity.run_after_commit_or_now do
        next unless from_service_desk?

        sent_notifications.update_all(**context)
      end
    end

    def copy_email_participants
      new_attributes = { id: nil, issue_id: new_entity.id }

      new_participants = original_entity.issue_email_participants.dup

      new_participants.each do |participant|
        participant.assign_attributes(new_attributes)
      end

      IssueEmailParticipant.bulk_insert!(new_participants)
    end

    override :update_old_entity
    def update_old_entity
      super

      recreate_related_issues
      mark_as_moved
    end

    override :update_new_entity
    def update_new_entity
      super

      copy_contacts
    end

    def create_new_entity
      new_params = {
        id: nil,
        iid: nil,
        relative_position: relative_position,
        project: target_project,
        author: original_entity.author,
        assignee_ids: original_entity.assignee_ids,
        moved_issue: true,
        imported_from: :none
      }

      new_params = original_entity.serializable_hash.symbolize_keys.merge(new_params)
      new_params = new_params.merge(rewritten_old_entity_attributes)
      # spam checking is not necessary, as no new content is being created.

      # 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.
      create_result = CreateService.new(
        container: @target_project,
        current_user: @current_user,
        params: new_params,
        perform_spam_check: false
      ).execute(skip_system_notes: true)

      raise MoveError, create_result.errors.join(', ') if create_result.error? && create_result[:issue].blank?

      create_result[:issue]
    end

    def queue_copy_designs
      return unless original_entity.designs.present?

      response = DesignManagement::CopyDesignCollection::QueueService.new(
        current_user,
        original_entity,
        new_entity
      ).execute

      log_error(response.message) if response.error?
    end

    def copy_timelogs
      return if original_entity.timelogs.empty?

      WorkItems::CopyTimelogsWorker.perform_async(original_entity.id, new_entity.id)
    end

    def mark_as_moved
      original_entity.update(moved_to: new_entity)
    end

    def recreate_related_issues
      source_issue_links = IssueLink.for_source(original_entity)
      target_issue_links = IssueLink.for_target(original_entity)

      source_issue_links.each_batch(of: BATCH_SIZE) do |links_batch|
        new_links = new_links(links_batch, reference_attribute: 'source_id')
        ::IssueLink.insert_all!(new_links) if new_links.any?
      end

      target_issue_links.each_batch(of: BATCH_SIZE) do |links_batch|
        new_links = new_links(links_batch, reference_attribute: 'target_id')
        ::IssueLink.insert_all!(new_links) if new_links.any?
      end

      source_issue_links.each_batch(of: BATCH_SIZE) do |links_batch|
        links_batch.delete_all
      end

      target_issue_links.each_batch(of: BATCH_SIZE) do |links_batch|
        links_batch.delete_all
      end
    end

    def new_links(links_batch, reference_attribute:)
      links_batch.map do |link|
        link.attributes.except('id', 'namespace_id').merge(reference_attribute => new_entity.id)
      end
    end

    def copy_contacts
      return unless original_entity.project.root_ancestor == new_entity.project.root_ancestor

      new_entity.customer_relations_contacts = original_entity.customer_relations_contacts
    end

    def notify_participants
      context = { original: original_entity, new: new_entity, user: @current_user, service: notification_service }

      original_entity.run_after_commit_or_now do
        context[:service].async.issue_moved(context[:original], context[:new], context[:user])
      end
    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_mod_with('Issues::MoveService')