promote_service.rb 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
module Labels
  class PromoteService < BaseService
    BATCH_SIZE = 1000

    def execute(label)
      return unless project.group &&
          label.is_a?(ProjectLabel)

      Label.transaction do
        new_label = clone_label_to_group_label(label)

        label_ids_for_merge(new_label).find_in_batches(batch_size: BATCH_SIZE) do |batched_ids|
          update_issuables(new_label, batched_ids)
          update_issue_board_lists(new_label, batched_ids)
          update_priorities(new_label, batched_ids)
16
          subscribe_users(new_label, batched_ids)
17 18 19 20 21 22
          # Order is important, project labels need to be last
          update_project_labels(batched_ids)
        end

        # We skipped validations during creation. Let's run them now, after deleting conflicting labels
        raise ActiveRecord::RecordInvalid.new(new_label) unless new_label.valid?
23

24 25 26 27 28 29
        new_label
      end
    end

    private

30 31 32 33 34 35 36 37 38
    def subscribe_users(new_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: new_label.id)
    end

39
    def label_ids_for_merge(new_label)
40 41 42 43 44
      LabelsFinder
        .new(current_user, title: new_label.title, group_id: project.group.id)
        .execute(skip_authorization: true)
        .where.not(id: new_label)
        .select(:id)  # Can't use pluck() to avoid object-creation because of the batching
45 46 47
    end

    def update_issuables(new_label, label_ids)
48 49 50
      LabelLink
        .where(label: label_ids)
        .update_all(label_id: new_label)
51 52 53
    end

    def update_issue_board_lists(new_label, label_ids)
54 55 56
      List
        .where(label: label_ids)
        .update_all(label_id: new_label)
57 58 59
    end

    def update_priorities(new_label, label_ids)
60 61 62
      LabelPriority
        .where(label: label_ids)
        .update_all(label_id: new_label)
63 64 65
    end

    def update_project_labels(label_ids)
66
      Label.where(id: label_ids).destroy_all
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    end

    def clone_label_to_group_label(label)
      params = label.attributes.slice('title', 'description', 'color')
      # Since the title of the new label has to be the same as the previous labels
      # and we're merging old labels in batches we'll skip validation to omit 2-step
      # merge process and do it in one batch
      # We'll be forcing validation at the end of the transaction to ensure everything
      # was merged correctly
      new_label = GroupLabel.new(params.merge(group: project.group))
      new_label.save(validate: false)

      new_label
    end
  end
end