debian-mirror-gitlab/app/services/labels/promote_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Labels
class PromoteService < BaseService
BATCH_SIZE = 1000
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def execute(label)
return unless project.group &&
label.is_a?(ProjectLabel)
Label.transaction do
2021-01-29 00:20:46 +05:30
# use the existing group label if it exists
group_label = find_or_create_group_label(label)
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
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-04-22 19:07:51 +05:30
destroy_project_labels(batched_ids)
2017-08-17 22:00:37 +05:30
end
2021-01-29 00:20:46 +05:30
group_label
2017-08-17 22:00:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
private
2021-01-29 00:20:46 +05:30
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-04-22 19:07:51 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def subscribe_users(group_label, label_ids)
2018-03-17 18:26:18 +05:30
# 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)')
2021-01-29 00:20:46 +05:30
Subscription.where(id: ids_to_update).update_all(subscribable_id: group_label.id)
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def label_ids_for_merge(group_label)
2017-09-10 17:25:29 +05:30
LabelsFinder
2021-01-29 00:20:46 +05:30
.new(current_user, title: group_label.title, group_id: project.group.id)
2017-09-10 17:25:29 +05:30
.execute(skip_authorization: true)
2021-01-29 00:20:46 +05:30
.where.not(id: group_label)
2019-03-02 22:35:43 +05:30
.select(:id) # Can't use pluck() to avoid object-creation because of the batching
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def update_issuables(group_label, label_ids)
2017-09-10 17:25:29 +05:30
LabelLink
.where(label: label_ids)
2021-01-29 00:20:46 +05:30
.update_all(label_id: group_label.id)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def update_resource_label_events(group_label, label_ids)
2018-11-20 20:47:30 +05:30
ResourceLabelEvent
.where(label: label_ids)
2021-01-29 00:20:46 +05:30
.update_all(label_id: group_label.id)
2018-11-20 20:47:30 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def update_issue_board_lists(group_label, label_ids)
2017-09-10 17:25:29 +05:30
List
.where(label: label_ids)
2021-01-29 00:20:46 +05:30
.update_all(label_id: group_label.id)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2021-01-29 00:20:46 +05:30
def update_priorities(group_label, label_ids)
2017-09-10 17:25:29 +05:30
LabelPriority
.where(label: label_ids)
2021-01-29 00:20:46 +05:30
.update_all(label_id: group_label.id)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2020-04-22 19:07:51 +05:30
def destroy_project_labels(label_ids)
2020-06-23 00:09:42 +05:30
Label.where(id: label_ids).destroy_all # rubocop: disable Cop/DestroyAll
2017-08-17 22:00:37 +05:30
end
2021-01-29 00:20:46 +05:30
def find_or_create_group_label(label)
2017-08-17 22:00:37 +05:30
params = label.attributes.slice('title', 'description', 'color')
2021-01-29 00:20:46 +05:30
new_label = GroupLabel.create_with(params).find_or_initialize_by(group_id: project.group.id, title: label.title)
2017-08-17 22:00:37 +05:30
2021-01-29 00:20:46 +05:30
new_label.save! unless new_label.persisted?
2017-08-17 22:00:37 +05:30
new_label
end
end
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
Labels::PromoteService.prepend_mod_with('Labels::PromoteService')