summaryrefslogtreecommitdiff
path: root/app/services/labels/transfer_service.rb
blob: 19d419609a56d40c98ce8a27e86ed800fb21a66e (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
105
106
107
# frozen_string_literal: true

# Labels::TransferService class
#
# User for recreate the missing group labels at project level
#
module Labels
  class TransferService
    def initialize(current_user, old_group, project)
      @current_user = current_user
      @old_group = old_group
      @project = project
    end

    def execute
      return unless old_group.present?

      # rubocop: disable CodeReuse/ActiveRecord
      link_ids = group_labels_applied_to_issues.pluck("label_links.id") +
                 group_labels_applied_to_merge_requests.pluck("label_links.id")
      # rubocop: disable CodeReuse/ActiveRecord

      Label.transaction do
        labels_to_transfer.find_each do |label|
          new_label_id = find_or_create_label!(label)

          next if new_label_id == label.id

          update_label_links(link_ids, old_label_id: label.id, new_label_id: new_label_id)
          update_label_priorities(old_label_id: label.id, new_label_id: new_label_id)
        end
      end
    end

    private

    attr_reader :current_user, :old_group, :project

    # rubocop: disable CodeReuse/ActiveRecord
    def labels_to_transfer
      Label
        .from_union([
          group_labels_applied_to_issues,
          group_labels_applied_to_merge_requests
        ])
        .reorder(nil)
        .distinct
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def group_labels_applied_to_issues
      @labels_applied_to_issues ||= if use_optimized_group_labels_query?
                                      Label.joins(:issues)
                                        .joins("INNER JOIN namespaces on namespaces.id = labels.group_id AND namespaces.type = 'Group'" )
                                        .where(issues: { project_id: project.id }).reorder(nil)
                                    else
                                      Label.joins(:issues).where(
                                        issues: { project_id: project.id },
                                        labels: { group_id: old_group.self_and_ancestors }
                                      )
                                    end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def group_labels_applied_to_merge_requests
      @labels_applied_to_mrs ||= if use_optimized_group_labels_query?
                                   Label.joins(:merge_requests)
                                     .joins("INNER JOIN namespaces on namespaces.id = labels.group_id AND namespaces.type = 'Group'" )
                                     .where(merge_requests: { target_project_id: project.id }).reorder(nil)
                                 else
                                   Label.joins(:merge_requests)
                                     .where(
                                       merge_requests: { target_project_id: project.id },
                                       labels: { group_id: old_group.self_and_ancestors }
                                     )
                                 end
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def find_or_create_label!(label)
      params    = label.attributes.slice('title', 'description', 'color')
      new_label = FindOrCreateService.new(current_user, project, params.merge(include_ancestor_groups: true)).execute

      new_label.id
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def update_label_links(link_ids, old_label_id:, new_label_id:)
      LabelLink.where(id: link_ids, label_id: old_label_id)
        .update_all(label_id: new_label_id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    # rubocop: disable CodeReuse/ActiveRecord
    def update_label_priorities(old_label_id:, new_label_id:)
      LabelPriority.where(project_id: project.id, label_id: old_label_id)
        .update_all(label_id: new_label_id)
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def use_optimized_group_labels_query?
      Feature.enabled?(:use_optimized_group_labels_query, project.root_namespace, default_enabled: :yaml)
    end
  end
end