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
|
2020-10-29 02:08:45 -04:00
|
|
|
# use the existing group label if it exists
|
|
|
|
group_label = find_or_create_group_label(label)
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
label_ids_for_merge(group_label).find_in_batches(batch_size: BATCH_SIZE) do |batched_ids|
|
|
|
|
update_old_label_relations(group_label, batched_ids)
|
2020-03-23 05:09:42 -04:00
|
|
|
destroy_project_labels(batched_ids)
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
group_label
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-11-02 07:58:59 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
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)
|
2020-03-23 05:09:42 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-10-29 02:08:45 -04:00
|
|
|
def subscribe_users(group_label, label_ids)
|
2018-01-10 02:29:58 -05:00
|
|
|
# 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)')
|
2020-10-29 02:08:45 -04:00
|
|
|
Subscription.where(id: ids_to_update).update_all(subscribable_id: group_label.id)
|
2018-01-10 02:29:58 -05:00
|
|
|
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
|
2020-10-29 02:08:45 -04:00
|
|
|
def label_ids_for_merge(group_label)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelsFinder
|
2020-10-29 02:08:45 -04:00
|
|
|
.new(current_user, title: group_label.title, group_id: project.group.id)
|
2017-06-21 09:48:12 -04:00
|
|
|
.execute(skip_authorization: true)
|
2020-10-29 02:08:45 -04:00
|
|
|
.where.not(id: group_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
|
2020-10-29 02:08:45 -04:00
|
|
|
def update_issuables(group_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelLink
|
|
|
|
.where(label: label_ids)
|
2020-10-29 02:08:45 -04:00
|
|
|
.update_all(label_id: group_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
|
2020-10-29 02:08:45 -04:00
|
|
|
def update_resource_label_events(group_label, label_ids)
|
2018-09-07 09:39:20 -04:00
|
|
|
ResourceLabelEvent
|
|
|
|
.where(label: label_ids)
|
2020-10-29 02:08:45 -04:00
|
|
|
.update_all(label_id: group_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-10-29 02:08:45 -04:00
|
|
|
def update_issue_board_lists(group_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
List
|
|
|
|
.where(label: label_ids)
|
2020-10-29 02:08:45 -04:00
|
|
|
.update_all(label_id: group_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-10-29 02:08:45 -04:00
|
|
|
def update_priorities(group_label, label_ids)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelPriority
|
|
|
|
.where(label: label_ids)
|
2020-10-29 02:08:45 -04:00
|
|
|
.update_all(label_id: group_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
|
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
def find_or_create_group_label(label)
|
2016-11-02 07:58:59 -04:00
|
|
|
params = label.attributes.slice('title', 'description', 'color')
|
2020-10-29 02:08:45 -04:00
|
|
|
new_label = GroupLabel.create_with(params).find_or_initialize_by(group_id: project.group.id, title: label.title)
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
new_label.save! unless new_label.persisted?
|
2016-11-02 07:58:59 -04:00
|
|
|
new_label
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-23 05:09:42 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Labels::PromoteService.prepend_mod_with('Labels::PromoteService')
|