Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

move attachment copying inside the wp copy service

ulferts439a0ca210e0
diff --git a/app/services/projects/copy/work_packages_dependent_service.rb b/app/services/projects/copy/work_packages_dependent_service.rb
index 3f78e750247..7b0187b8b49 100644
--- a/app/services/projects/copy/work_packages_dependent_service.rb
+++ b/app/services/projects/copy/work_packages_dependent_service.rb
@@ -81,7 +81,7 @@ module Projects::Copy
.new(user:,
work_package: source_work_package,
contract_class: WorkPackages::CopyProjectContract)
- .call(**overrides)
+ .call(attachments: copy_attachments?, **overrides)
if service_call.success?
service_call.result
@@ -175,5 +175,9 @@ module Projects::Copy
new_wp_id]
end
end
+
+ def copy_attachments?
+ (params.dig(:params, :only) || []).any? { |k| k.to_sym == :work_package_attachments }
+ end
end
end
diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rb
index c688c58220e..ff2c5884f20 100644
--- a/app/services/projects/copy_service.rb
+++ b/app/services/projects/copy_service.rb
@@ -36,7 +36,6 @@ module Projects
::Projects::Copy::VersionsDependentService,
::Projects::Copy::CategoriesDependentService,
::Projects::Copy::WorkPackagesDependentService,
- ::Projects::Copy::WorkPackageAttachmentsDependentService,
::Projects::Copy::WikiDependentService,
::Projects::Copy::WikiPageAttachmentsDependentService,
::Projects::Copy::ForumsDependentService,
diff --git a/app/services/work_packages/copy_service.rb b/app/services/work_packages/copy_service.rb
index 171b09c666f..9b1b0e0e311 100644
--- a/app/services/work_packages/copy_service.rb
+++ b/app/services/work_packages/copy_service.rb
@@ -29,6 +29,7 @@
class WorkPackages::CopyService
include ::Shared::ServiceContext
include Contracted
+ include ::Copy::Concerns::CopyAttachments
attr_accessor :user,
:work_package,
@@ -40,22 +41,26 @@ class WorkPackages::CopyService
self.contract_class = contract_class
end
- def call(send_notifications: true, **attributes)
+ def call(send_notifications: true, attachments: true, **attributes)
in_context(work_package, send_notifications) do
- copy(attributes, send_notifications)
+ copy(attributes, attachments, send_notifications)
end
end
protected
- def copy(attribute_override, send_notifications)
- attributes = copied_attributes(work_package, attribute_override)
-
- copied = create(attributes, send_notifications)
+ def copy(attribute_override, attachments, send_notifications)
+ copied = create(work_package,
+ attribute_override,
+ send_notifications)
if copied.success?
remove_author_watcher(copied.result)
copy_watchers(copied.result)
+
+ if attachments
+ copy_attachments('WorkPackage', from_id: work_package.id, to_id: copied.result.id)
+ end
end
copied.state.copied_from_work_package_id = work_package&.id
@@ -63,11 +68,11 @@ class WorkPackages::CopyService
copied
end
- def create(attributes, send_notifications)
+ def create(work_package, attribute_overrides, send_notifications)
WorkPackages::CreateService
.new(user:,
contract_class:)
- .call(**attributes.merge(send_notifications:).symbolize_keys)
+ .call(**copied_attributes(work_package, attribute_overrides).merge(send_notifications:).symbolize_keys)
end
def copied_attributes(work_package, override)
diff --git a/spec/services/work_packages/copy_service_integration_spec.rb b/spec/services/work_packages/copy_service_integration_spec.rb
index 3b283b576ac..657b784b5b6 100644
--- a/spec/services/work_packages/copy_service_integration_spec.rb
+++ b/spec/services/work_packages/copy_service_integration_spec.rb
@@ -73,9 +73,7 @@ describe WorkPackages::CopyService, 'integration', type: :model do
.call(**attributes)
end
- before do
- login_as(user)
- end
+ current_user { user }
describe '#call' do
shared_examples_for 'copied work package' do
@@ -301,5 +299,34 @@ describe WorkPackages::CopyService, 'integration', type: :model do
it_behaves_like 'copied work package'
end
+
+ context 'with attachments' do
+ let!(:attachment) do
+ create(:attachment,
+ container: work_package)
+ end
+
+ context 'when specifying to copy attachments (default)' do
+ it 'copies the attachment' do
+ expect(copy.attachments.length)
+ .to eq 1
+
+ expect(copy.attachments.first.attributes.slice(:digest, :file, :filesize))
+ .to eq attachment.attributes.slice(:digest, :file, :filesize)
+
+ expect(copy.attachments.first.id)
+ .not_to eq attachment.id
+ end
+ end
+
+ context 'when specifying to not copy attachments' do
+ let(:attributes) { { attachments: false } }
+
+ it 'copies the attachment' do
+ expect(copy.attachments.length)
+ .to eq 0
+ end
+ end
+ end
end
end