diff options
Diffstat (limited to 'app/services/issuable')
-rw-r--r-- | app/services/issuable/clone/attributes_rewriter.rb | 55 | ||||
-rw-r--r-- | app/services/issuable/clone/base_service.rb | 2 | ||||
-rw-r--r-- | app/services/issuable/common_system_notes_service.rb | 18 |
3 files changed, 52 insertions, 23 deletions
diff --git a/app/services/issuable/clone/attributes_rewriter.rb b/app/services/issuable/clone/attributes_rewriter.rb index 55f5629baac..a78e191c85f 100644 --- a/app/services/issuable/clone/attributes_rewriter.rb +++ b/app/services/issuable/clone/attributes_rewriter.rb @@ -20,6 +20,7 @@ module Issuable copy_resource_label_events copy_resource_weight_events copy_resource_milestone_events + copy_resource_state_events end private @@ -47,8 +48,6 @@ module Issuable end def copy_resource_label_events - entity_key = new_entity.class.name.underscore.foreign_key - copy_events(ResourceLabelEvent.table_name, original_entity.resource_label_events) do |event| event.attributes .except('id', 'reference', 'reference_html') @@ -67,22 +66,39 @@ module Issuable end def copy_resource_milestone_events - entity_key = new_entity.class.name.underscore.foreign_key + return unless milestone_events_supported? copy_events(ResourceMilestoneEvent.table_name, original_entity.resource_milestone_events) do |event| - matching_destination_milestone = matching_milestone(event.milestone.title) - - if matching_destination_milestone.present? - event.attributes - .except('id') - .merge(entity_key => new_entity.id, - 'milestone_id' => matching_destination_milestone.id, - 'action' => ResourceMilestoneEvent.actions[event.action], - 'state' => ResourceMilestoneEvent.states[event.state]) + if event.remove? + event_attributes_with_milestone(event, nil) + else + matching_destination_milestone = matching_milestone(event.milestone_title) + + event_attributes_with_milestone(event, matching_destination_milestone) if matching_destination_milestone.present? end end end + def copy_resource_state_events + return unless state_events_supported? + + copy_events(ResourceStateEvent.table_name, original_entity.resource_state_events) do |event| + event.attributes + .except('id') + .merge(entity_key => new_entity.id, + 'state' => ResourceStateEvent.states[event.state]) + end + end + + def event_attributes_with_milestone(event, milestone) + event.attributes + .except('id') + .merge(entity_key => new_entity.id, + 'milestone_id' => milestone&.id, + 'action' => ResourceMilestoneEvent.actions[event.action], + 'state' => ResourceMilestoneEvent.states[event.state]) + end + def copy_events(table_name, events_to_copy) events_to_copy.find_in_batches do |batch| events = batch.map do |event| @@ -94,7 +110,20 @@ module Issuable end def entity_key - new_entity.class.name.parameterize('_').foreign_key + new_entity.class.name.underscore.foreign_key + end + + def milestone_events_supported? + both_respond_to?(:resource_milestone_events) + end + + def state_events_supported? + both_respond_to?(:resource_state_events) + end + + def both_respond_to?(method) + original_entity.respond_to?(method) && + new_entity.respond_to?(method) end end end diff --git a/app/services/issuable/clone/base_service.rb b/app/services/issuable/clone/base_service.rb index 54576e82030..0d1640924e5 100644 --- a/app/services/issuable/clone/base_service.rb +++ b/app/services/issuable/clone/base_service.rb @@ -47,7 +47,7 @@ module Issuable end def new_parent - new_entity.project ? new_entity.project : new_entity.group + new_entity.project || new_entity.group end def group diff --git a/app/services/issuable/common_system_notes_service.rb b/app/services/issuable/common_system_notes_service.rb index 67cf212691f..195616857dc 100644 --- a/app/services/issuable/common_system_notes_service.rb +++ b/app/services/issuable/common_system_notes_service.rb @@ -4,7 +4,7 @@ module Issuable class CommonSystemNotesService < ::BaseService attr_reader :issuable - def execute(issuable, old_labels: [], is_update: true) + def execute(issuable, old_labels: [], old_milestone: nil, is_update: true) @issuable = issuable # We disable touch so that created system notes do not update @@ -22,17 +22,13 @@ module Issuable end create_due_date_note if issuable.previous_changes.include?('due_date') - create_milestone_note if has_milestone_changes? + create_milestone_note(old_milestone) if issuable.previous_changes.include?('milestone_id') create_labels_note(old_labels) if old_labels && issuable.labels != old_labels end end private - def has_milestone_changes? - issuable.previous_changes.include?('milestone_id') - end - def handle_time_tracking_note if issuable.previous_changes.include?('time_estimate') create_time_estimate_note @@ -98,15 +94,19 @@ module Issuable SystemNoteService.change_time_spent(issuable, issuable.project, issuable.time_spent_user) end - def create_milestone_note + def create_milestone_note(old_milestone) if milestone_changes_tracking_enabled? - # Creates a synthetic note - ResourceEvents::ChangeMilestoneService.new(issuable, current_user).execute + create_milestone_change_event(old_milestone) else SystemNoteService.change_milestone(issuable, issuable.project, current_user, issuable.milestone) end end + def create_milestone_change_event(old_milestone) + ResourceEvents::ChangeMilestoneService.new(issuable, current_user, old_milestone: old_milestone) + .execute + end + def milestone_changes_tracking_enabled? ::Feature.enabled?(:track_resource_milestone_change_events, issuable.project) end |