Commit 15411bad authored by Patrick Derichs's avatar Patrick Derichs

Fix AttributesRewriter for ResourceMilestoneEvents without milestone

Update specs as well
parent 87d63f10
......@@ -25,4 +25,8 @@ class ResourceMilestoneEvent < ResourceEvent
def self.issuable_attrs
%i(issue merge_request).freeze
end
def milestone_title
milestone&.title
end
end
......@@ -67,21 +67,29 @@ 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 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 event_attributes_with_milestone(event, milestone)
entity_key = new_entity.class.name.underscore.foreign_key
if matching_destination_milestone.present?
event.attributes
.except('id')
.merge(entity_key => new_entity.id,
'milestone_id' => matching_destination_milestone.id,
'milestone_id' => milestone&.id,
'action' => ResourceMilestoneEvent.actions[event.action],
'state' => ResourceMilestoneEvent.states[event.state])
end
end
end
def copy_events(table_name, events_to_copy)
events_to_copy.find_in_batches do |batch|
......@@ -96,6 +104,11 @@ module Issuable
def entity_key
new_entity.class.name.parameterize('_').foreign_key
end
def milestone_events_supported?
original_entity.respond_to?(:resource_milestone_events) &&
new_entity.respond_to?(:resource_milestone_events)
end
end
end
end
......@@ -71,6 +71,25 @@ describe 'Issue promotion', :js do
expect(epic.description).to eq(issue.description)
expect(epic.author).to eq(user)
end
# Spec for https://gitlab.com/gitlab-org/gitlab/-/issues/215549
context 'if there is a remove resource milestone event' do
let!(:resource_milestone_event) { create(:resource_milestone_event, issue: issue, action: 'remove', milestone_id: nil) }
it 'promotes the issue' do
add_note('/promote')
wait_for_requests
epic = Epic.last
expect(page).to have_content 'Promoted issue to an epic.'
expect(issue.reload).to be_closed
expect(epic.title).to eq(issue.title)
expect(epic.description).to eq(issue.description)
expect(epic.author).to eq(user)
end
end
end
context 'when issue is confidential' do
......
......@@ -78,4 +78,21 @@ describe ResourceMilestoneEvent, type: :model do
let(:query_method) { :remove? }
end
end
describe '#milestone_title' do
let(:milestone) { create(:milestone, title: 'v2.3') }
let(:event) { create(:resource_milestone_event, milestone: milestone) }
it 'returns the expected title' do
expect(event.milestone_title).to eq('v2.3')
end
context 'when milestone is nil' do
let(:event) { create(:resource_milestone_event, milestone: nil) }
it 'returns nil' do
expect(event.milestone_title).to be_nil
end
end
end
end
......@@ -89,7 +89,7 @@ describe Issuable::Clone::AttributesRewriter do
create_event(milestone1_project1)
create_event(milestone2_project1)
create_event(milestone1_project1, 'remove')
create_event(nil, 'remove')
create_event(milestone3_project1)
end
......@@ -101,7 +101,7 @@ describe Issuable::Clone::AttributesRewriter do
expect_milestone_event(new_issue_milestone_events.first, milestone: milestone1_project2, action: 'add', state: 'opened')
expect_milestone_event(new_issue_milestone_events.second, milestone: milestone2_project2, action: 'add', state: 'opened')
expect_milestone_event(new_issue_milestone_events.third, milestone: milestone1_project2, action: 'remove', state: 'opened')
expect_milestone_event(new_issue_milestone_events.third, milestone: nil, action: 'remove', state: 'opened')
end
def create_event(milestone, action = 'add')
......@@ -109,7 +109,7 @@ describe Issuable::Clone::AttributesRewriter do
end
def expect_milestone_event(event, expected_attrs)
expect(event.milestone_id).to eq(expected_attrs[:milestone].id)
expect(event.milestone_id).to eq(expected_attrs[:milestone]&.id)
expect(event.action).to eq(expected_attrs[:action])
expect(event.state).to eq(expected_attrs[:state])
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