2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-02 07:58:59 -04:00
|
|
|
module Labels
|
|
|
|
class PromoteService < BaseService
|
|
|
|
BATCH_SIZE = 1000
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
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|
|
2020-03-23 05:09:42 -04:00
|
|
|
update_old_label_relations(new_label, batched_ids)
|
|
|
|
destroy_project_labels(batched_ids)
|
2016-11-02 07:58:59 -04:00
|
|
|
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?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-11-02 07:58:59 -04:00
|
|
|
new_label
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-23 05:09:42 -04:00
|
|
|
def update_old_label_relations(new_label, old_label_ids)
|
|
|
|
update_issuables(new_label, old_label_ids)
|
|
|
|
update_resource_label_events(new_label, old_label_ids)
|
|
|
|
update_issue_board_lists(new_label, old_label_ids)
|
|
|
|
update_priorities(new_label, old_label_ids)
|
|
|
|
subscribe_users(new_label, old_label_ids)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-10 02:29:58 -05:00
|
|
|
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
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-10 02:29:58 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
def label_ids_for_merge(new_label)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelsFinder
|
|
|
|
.new(current_user, title: new_label.title, group_id: project.group.id)
|
|
|
|
.execute(skip_authorization: true)
|
|
|
|
.where.not(id: new_label)
|
2019-01-16 07:09:29 -05:00
|
|
|
.select(:id) # Can't use pluck() to avoid object-creation because of the batching
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
def update_issuables(new_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelLink
|
|
|
|
.where(label: label_ids)
|
2018-12-13 15:45:40 -05:00
|
|
|
.update_all(label_id: new_label.id)
|
2018-09-07 09:39:20 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-09-07 09:39:20 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-09-07 09:39:20 -04:00
|
|
|
def update_resource_label_events(new_label, label_ids)
|
|
|
|
ResourceLabelEvent
|
|
|
|
.where(label: label_ids)
|
2018-12-13 15:45:40 -05:00
|
|
|
.update_all(label_id: new_label.id)
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
def update_issue_board_lists(new_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
List
|
|
|
|
.where(label: label_ids)
|
2018-12-13 15:45:40 -05:00
|
|
|
.update_all(label_id: new_label.id)
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
def update_priorities(new_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelPriority
|
|
|
|
.where(label: label_ids)
|
2018-12-13 15:45:40 -05:00
|
|
|
.update_all(label_id: new_label.id)
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-03-23 05:09:42 -04:00
|
|
|
def destroy_project_labels(label_ids)
|
2020-05-28 17:08:22 -04:00
|
|
|
Label.where(id: label_ids).destroy_all # rubocop: disable Cop/DestroyAll
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
|
|
|
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
|
2020-03-23 05:09:42 -04:00
|
|
|
|
|
|
|
Labels::PromoteService.prepend_if_ee('EE::Labels::PromoteService')
|