summaryrefslogtreecommitdiff
path: root/app/services/issuable/clone/base_service.rb
blob: 574fe85b4662f8e1cc43c0d020c8fe0018cf5061 (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 Issuable
  module Clone
    class BaseService < IssuableBaseService
      attr_reader :original_entity, :new_entity, :target_project

      alias_method :old_project, :project

      def execute(original_entity, target_project = nil)
        @original_entity = original_entity
        @target_project = target_project

        # Using transaction because of a high resources footprint
        # on rewriting notes (unfolding references)
        #
        ActiveRecord::Base.transaction do
          @new_entity = create_new_entity

          update_new_entity
          update_old_entity
          create_notes
        end
      end

      private

      def copy_award_emoji
        AwardEmojis::CopyService.new(original_entity, new_entity).execute
      end

      def copy_notes
        Notes::CopyService.new(current_user, original_entity, new_entity).execute
      end

      def update_new_entity
        update_new_entity_description
        update_new_entity_attributes
        copy_award_emoji
        copy_notes
      end

      def update_new_entity_description
        rewritten_description = MarkdownContentRewriterService.new(
          current_user,
          original_entity.description,
          original_entity.project,
          new_parent
        ).execute

        new_entity.update!(description: rewritten_description)
      end

      def update_new_entity_attributes
        AttributesRewriter.new(current_user, original_entity, new_entity).execute
      end

      def update_old_entity
        close_issue
      end

      def create_notes
        add_note_from
        add_note_to
      end

      def close_issue
        close_service = Issues::CloseService.new(project: old_project, current_user: current_user)
        close_service.execute(original_entity, notifications: false, system_note: true)
      end

      def new_parent
        new_entity.resource_parent
      end

      def group
        if new_entity.project&.group && current_user.can?(:read_group, new_entity.project.group)
          new_entity.project.group
        end
      end

      def relative_position
        return if original_entity.project.root_ancestor.id != target_project.root_ancestor.id

        original_entity.relative_position
      end
    end
  end
end

Issuable::Clone::BaseService.prepend_mod_with('Issuable::Clone::BaseService')