Commit a1c1f4d0 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '38096-splitmr-refactor-attributes-rewriter-pd' into 'master'

Generalize copying of events within AttributesRewriter

Closes #38096

See merge request gitlab-org/gitlab!24047
parents e332eee3 765ec848
...@@ -12,7 +12,7 @@ module Issuable ...@@ -12,7 +12,7 @@ module Issuable
def execute def execute
update_attributes = { labels: cloneable_labels } update_attributes = { labels: cloneable_labels }
milestone = cloneable_milestone milestone = matching_milestone(original_entity.milestone&.title)
update_attributes[:milestone] = milestone if milestone.present? update_attributes[:milestone] = milestone if milestone.present?
new_entity.update(update_attributes) new_entity.update(update_attributes)
...@@ -23,11 +23,8 @@ module Issuable ...@@ -23,11 +23,8 @@ module Issuable
private private
def cloneable_milestone def matching_milestone(title)
return unless new_entity.supports_milestone? return if title.blank? || !new_entity.supports_milestone?
title = original_entity.milestone&.title
return unless title
params = { title: title, project_ids: new_entity.project&.id, group_ids: group&.id } params = { title: title, project_ids: new_entity.project&.id, group_ids: group&.id }
...@@ -49,29 +46,32 @@ module Issuable ...@@ -49,29 +46,32 @@ module Issuable
end end
def copy_resource_label_events def copy_resource_label_events
original_entity.resource_label_events.find_in_batches do |batch| entity_key = new_entity.class.name.underscore.foreign_key
events = batch.map do |event|
entity_key = new_entity.is_a?(Issue) ? 'issue_id' : 'epic_id' copy_events(ResourceLabelEvent.table_name, original_entity.resource_label_events) do |event|
event.attributes event.attributes
.except('id', 'reference', 'reference_html') .except('id', 'reference', 'reference_html')
.merge(entity_key => new_entity.id, 'action' => ResourceLabelEvent.actions[event.action]) .merge(entity_key => new_entity.id, 'action' => ResourceLabelEvent.actions[event.action])
end end
Gitlab::Database.bulk_insert(ResourceLabelEvent.table_name, events)
end
end end
def copy_resource_weight_events def copy_resource_weight_events
return unless original_entity.respond_to?(:resource_weight_events) return unless original_entity.respond_to?(:resource_weight_events)
original_entity.resource_weight_events.find_in_batches do |batch| copy_events(ResourceWeightEvent.table_name, original_entity.resource_weight_events) do |event|
events = batch.map do |event|
event.attributes event.attributes
.except('id', 'reference', 'reference_html') .except('id', 'reference', 'reference_html')
.merge('issue_id' => new_entity.id) .merge('issue_id' => new_entity.id)
end end
end
def copy_events(table_name, events_to_copy)
events_to_copy.find_in_batches do |batch|
events = batch.map do |event|
yield(event)
end.compact
Gitlab::Database.bulk_insert(ResourceWeightEvent.table_name, events) Gitlab::Database.bulk_insert(table_name, events)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment