Return the unmapped work package ID in case of wp not copied

When a foreign work package is linked, it will not get copied during the
project copy service. In this case, just use the original to retain it.
pull/8498/head
Oliver Günther 4 years ago
parent 2523040abf
commit 65fe7ce06f
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 8
      app/services/queries/copy/ordered_work_packages_dependent_service.rb
  2. 29
      spec/services/projects/copy_service_integration_spec.rb

@ -40,13 +40,9 @@ module Queries::Copy
def duplicate_query_order(query, new_query)
query.ordered_work_packages.find_each do |ordered_wp|
wp_id = lookup_work_package_id(ordered_wp.work_package_id)
# Nothing to do if the work package could not be copied for whatever reason
next unless wp_id
copied = ordered_wp.dup
copied.query_id = new_query.id
copied.work_package_id = wp_id
copied.work_package_id = lookup_work_package_id(ordered_wp.work_package_id)
copied.save
end
end
@ -56,7 +52,7 @@ module Queries::Copy
# we're in a mapped condition (e.g., copying a project)
def lookup_work_package_id(id)
if state.work_package_id_lookup
state.work_package_id_lookup[id]
state.work_package_id_lookup[id] || id
else
id
end

@ -344,6 +344,35 @@ describe Projects::CopyService, 'integration', type: :model do
expect(copied_order).to eq(original_order)
end
context 'if one work package is a cross project reference' do
let(:other_project) { FactoryBot.create :project }
before do
work_package2.update! project: other_project
end
let(:only_args) { %w[work_packages queries] }
it 'copies the query and order' do
expect(subject).to be_success
# Only 3 out of the 4 work packages got copied this time
expect(project_copy.work_packages.count).to eq(3)
expect(project_copy.queries.count).to eq(2)
manual_query = project_copy.queries.find_by name: 'Manual query'
expect(manual_query).to be_manually_sorted
expect(query.ordered_work_packages.count).to eq 3
original_order = query.ordered_work_packages.map { |ow| ow.work_package.subject }
copied_order = manual_query.ordered_work_packages.map { |ow| ow.work_package.subject }
expect(copied_order).to eq(original_order)
# Expect reference to the origianl work package
referenced = query.ordered_work_packages.detect { |ow| ow.work_package == work_package2 }
expect(referenced).to be_present
end
end
end
describe '#parent' do

Loading…
Cancel
Save