2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-26 16:10:54 -04:00
|
|
|
# Labels::TransferService class
|
|
|
|
#
|
|
|
|
# User for recreate the missing group labels at project level
|
|
|
|
#
|
|
|
|
module Labels
|
|
|
|
class TransferService
|
2016-10-19 12:47:17 -04:00
|
|
|
def initialize(current_user, old_group, project)
|
2016-09-26 16:10:54 -04:00
|
|
|
@current_user = current_user
|
2016-10-19 12:47:17 -04:00
|
|
|
@old_group = old_group
|
2016-09-26 16:10:54 -04:00
|
|
|
@project = project
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-10-19 12:47:17 -04:00
|
|
|
return unless old_group.present?
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
# 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
|
|
|
|
|
2016-09-26 16:10:54 -04:00
|
|
|
Label.transaction do
|
|
|
|
labels_to_transfer.find_each do |label|
|
|
|
|
new_label_id = find_or_create_label!(label)
|
|
|
|
|
2016-10-13 16:25:25 -04:00
|
|
|
next if new_label_id == label.id
|
|
|
|
|
2020-07-13 08:09:18 -04:00
|
|
|
update_label_links(link_ids, old_label_id: label.id, new_label_id: new_label_id)
|
2016-10-19 12:47:17 -04:00
|
|
|
update_label_priorities(old_label_id: label.id, new_label_id: new_label_id)
|
2016-09-26 16:10:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-19 12:47:17 -04:00
|
|
|
attr_reader :current_user, :old_group, :project
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
def labels_to_transfer
|
2018-09-11 11:31:34 -04:00
|
|
|
Label
|
|
|
|
.from_union([
|
|
|
|
group_labels_applied_to_issues,
|
|
|
|
group_labels_applied_to_merge_requests
|
|
|
|
])
|
|
|
|
.reorder(nil)
|
2018-10-26 12:19:28 -04:00
|
|
|
.distinct
|
2016-10-19 12:47:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
def group_labels_applied_to_issues
|
2020-07-13 08:09:18 -04:00
|
|
|
@group_labels_applied_to_issues ||= Label.joins(:issues)
|
2017-06-21 09:48:12 -04:00
|
|
|
.where(
|
2016-10-19 12:47:17 -04:00
|
|
|
issues: { project_id: project.id },
|
2020-07-17 02:09:11 -04:00
|
|
|
labels: { group_id: old_group.self_and_ancestors }
|
2016-10-19 12:47:17 -04:00
|
|
|
)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-26 16:10:54 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
def group_labels_applied_to_merge_requests
|
2020-07-13 08:09:18 -04:00
|
|
|
@group_labels_applied_to_merge_requests ||= Label.joins(:merge_requests)
|
2017-06-21 09:48:12 -04:00
|
|
|
.where(
|
2016-10-19 12:47:17 -04:00
|
|
|
merge_requests: { target_project_id: project.id },
|
2020-07-17 02:09:11 -04:00
|
|
|
labels: { group_id: old_group.self_and_ancestors }
|
2016-10-19 12:47:17 -04:00
|
|
|
)
|
2016-09-26 16:10:54 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-26 16:10:54 -04:00
|
|
|
|
|
|
|
def find_or_create_label!(label)
|
2016-10-19 12:47:17 -04:00
|
|
|
params = label.attributes.slice('title', 'description', 'color')
|
2020-03-17 20:09:16 -04:00
|
|
|
new_label = FindOrCreateService.new(current_user, project, params.merge(include_ancestor_groups: true)).execute
|
2016-09-26 16:10:54 -04:00
|
|
|
|
|
|
|
new_label.id
|
|
|
|
end
|
2016-10-19 12:47:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-07-13 08:09:18 -04:00
|
|
|
def update_label_links(link_ids, old_label_id:, new_label_id:)
|
2018-04-19 04:17:12 -04:00
|
|
|
LabelLink.where(id: link_ids, label_id: old_label_id)
|
2017-06-21 09:48:12 -04:00
|
|
|
.update_all(label_id: new_label_id)
|
2016-10-19 12:47:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-19 12:47:17 -04:00
|
|
|
def update_label_priorities(old_label_id:, new_label_id:)
|
2017-06-21 09:48:12 -04:00
|
|
|
LabelPriority.where(project_id: project.id, label_id: old_label_id)
|
|
|
|
.update_all(label_id: new_label_id)
|
2016-10-19 12:47:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-26 16:10:54 -04:00
|
|
|
end
|
|
|
|
end
|