summaryrefslogtreecommitdiff
path: root/app/services/labels/promote_service.rb
blob: fdf2cf13f92ae41a7d6fd48757ce5468ddcaf4e9 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

module Labels
  class PromoteService < BaseService
    BATCH_SIZE = 1000

    # rubocop: disable CodeReuse/ActiveRecord
    def execute(label)
      return unless project.group &&
          label.is_a?(ProjectLabel)

      Label.transaction do
        # use the existing group label if it exists
        group_label = find_or_create_group_label(label)

        label_ids_for_merge(group_label).find_in_batches(batch_size: BATCH_SIZE) do |batched_ids|
          update_old_label_relations(group_label, batched_ids)
          destroy_project_labels(batched_ids)
        end

        group_label
      end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    private

    def update_old_label_relations(group_label, old_label_ids)
      update_issuables(group_label, old_label_ids)
      update_resource_label_events(group_label, old_label_ids)
      update_issue_board_lists(group_label, old_label_ids)
      update_priorities(group_label, old_label_ids)
      subscribe_users(group_label, old_label_ids)
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def subscribe_users(group_label, label_ids)
      # users can be subscribed to multiple labels that will be merged into the group one
      # we want to keep only one subscription / user
      ids_to_update = Subscription.where(subscribable_id: label_ids, subscribable_type: 'Label')
        .group(:user_id)
        .pluck('MAX(id)')
      Subscription.where(id: ids_to_update).update_all(subscribable_id: group_label.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def label_ids_for_merge(group_label)
      LabelsFinder
        .new(current_user, title: group_label.title, group_id: project.group.id)
        .execute(skip_authorization: true)
        .where.not(id: group_label)
        .select(:id) # Can't use pluck() to avoid object-creation because of the batching
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_issuables(group_label, label_ids)
      LabelLink
        .where(label: label_ids)
        .update_all(label_id: group_label.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_resource_label_events(group_label, label_ids)
      ResourceLabelEvent
        .where(label: label_ids)
        .update_all(label_id: group_label.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_issue_board_lists(group_label, label_ids)
      List
        .where(label: label_ids)
        .update_all(label_id: group_label.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_priorities(group_label, label_ids)
      LabelPriority
        .where(label: label_ids)
        .update_all(label_id: group_label.id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def destroy_project_labels(label_ids)
      Label.where(id: label_ids).destroy_all # rubocop: disable Cop/DestroyAll
    end

    def find_or_create_group_label(label)
      params = label.attributes.slice('title', 'description', 'color')
      new_label = GroupLabel.create_with(params).find_or_initialize_by(group_id: project.group.id, title: label.title)

      new_label.save! unless new_label.persisted?
      new_label
    end
  end
end

Labels::PromoteService.prepend_if_ee('EE::Labels::PromoteService')