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