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

[48587] Added project folder copy to project copying

Andreas Pfohl1e1a5676c1fd
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index 5a9f697c188..5957746bbc3 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -30,7 +30,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
class CopyTemplateFolderCommand
using Storages::Peripherals::ServiceResultRefinements
- def initialize(storage:)
+ def initialize(storage)
@uri = URI(storage.host).normalize
@username = storage.username
@password = storage.password
@@ -84,11 +84,11 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
when Net::HTTPSuccess
Util.error(:conflict, 'Destination folder already exists.')
when Net::HTTPUnauthorized
- Util.error(:not_authorized)
+ Util.error(:not_authorized, "Not authorized (validate_destination)")
when Net::HTTPNotFound
ServiceResult.success(result: urls)
else
- Util.error(:unknown)
+ Util.error(:unknown, "Unexpected response (validate_destination): #{response.code}", response)
end
end
end
@@ -96,26 +96,24 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def copy_folder
->(urls) do
- request = Net::HTTP::Copy.new(urls[:source_url])
- request['Destination'] = urls[:destination_url]
-
headers = Util.basic_auth_header(@username, @password)
+ headers['Destination'] = urls[:destination_url]
headers['Depth'] = 'infinity'
- request.initialize_http_header headers
+ request = Net::HTTP::Copy.new(urls[:source_url], headers)
response = Util.http(@uri).request(request)
case response
when Net::HTTPCreated
- ServiceResult.success(message: 'Folder was successfully copied.')
+ ServiceResult.success(message: 'Folder was successfully copied')
when Net::HTTPUnauthorized
- Util.error(:not_authorized)
+ Util.error(:not_authorized, "Not authorized (copy_folder)")
when Net::HTTPNotFound
- Util.error(:not_found)
+ Util.error(:not_found, "Project folder not found (copy_folder)")
when Net::HTTPConflict
Util.error(:conflict, Util.error_text_from_response(response))
else
- Util.error(:unknown)
+ Util.error(:unknown, "Unexpected response (copy_folder): #{response.code}", response)
end
end
end
diff --git a/modules/storages/app/services/projects/copy/storages_dependent_service.rb b/modules/storages/app/services/projects/copy/storages_dependent_service.rb
index 6fae33a47cb..e0acc35f0e4 100644
--- a/modules/storages/app/services/projects/copy/storages_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/storages_dependent_service.rb
@@ -28,6 +28,8 @@
module Projects::Copy
class StoragesDependentService < ::Copy::Dependency
+ using Storages::Peripherals::ServiceResultRefinements
+
def self.human_name
I18n.t(:label_project_storage_plural)
end
@@ -43,7 +45,9 @@ module Projects::Copy
copied_project_storage = create_project_storage(project_storage)
if project_storage.project_folder_automatic?
- copy_project_folder(project_storage, copied_project_storage.project)
+ copy_project_folder(project_storage, copied_project_storage)
+
+ update_project_folder_id(copied_project_storage)
end
end
end
@@ -52,31 +56,54 @@ module Projects::Copy
def create_project_storage(project_storage)
attributes = project_storage
- .attributes.dup.except('id', 'project_id', 'created_at', 'updated_at')
- .merge('project_id' => target.id)
+ .attributes.dup.except('id', 'project_id', 'created_at', 'updated_at')
+ .merge('project_id' => target.id)
service_result = ::Storages::ProjectStorages::CreateService
- .new(user: User.current)
- .call(attributes)
+ .new(user: User.current)
+ .call(attributes)
copied_storage = service_result.result
copied_storage.save
copied_storage
end
- def copy_project_folder(source_project_storage, destination_project)
- source_folder_name = project_folder_path(source_project_storage.project)
- destination_folder_name = project_folder_path(destination_project)
+ def copy_project_folder(source_project_storage, destination_project_storage)
+ source_folder_name = project_folder_path(source_project_storage)
+ destination_folder_name = project_folder_path(destination_project_storage)
Storages::Peripherals::StorageRequests
.new(storage: source_project_storage.storage)
.copy_template_folder_command
.call(source_path: source_folder_name, destination_path: destination_folder_name)
- .on_failure { |result| add_error! source_folder_name, result.to_active_model_errors }
+ .on_failure { |r| add_error!(source_folder_name, r.to_active_model_errors) }
+ end
+
+ def update_project_folder_id(project_storage)
+ destination_folder_name = project_folder_path(project_storage)
+
+ query_params = {
+ depth: '0',
+ path: destination_folder_name,
+ props: %w[oc:fileid]
+ }
+
+ Storages::Peripherals::StorageRequests
+ .new(storage: project_storage.storage)
+ .propfind_query
+ .call(**query_params)
+ .match(
+ on_success: ->(r) do
+ file_id = r[destination_folder_name]["fileid"]
+ project_storage.update!(project_folder_id: file_id)
+ end,
+ on_failure: ->(r) { add_error!(destination_folder_name, r.to_active_model_errors) }
+ )
end
- def project_folder_path(project)
- "#{@group_folder}/#{project.name.gsub('/', '|')} (#{project.id})/"
+ def project_folder_path(project_storage)
+ project = project_storage.project
+ "#{project_storage.storage.group_folder}/#{project.name.gsub('/', '|')} (#{project.id})/"
end
end
end
diff --git a/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb b/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb
index 4e04733611e..43bce8615f5 100644
--- a/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb
+++ b/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplat
let(:origin_user_id) { 'OpenProject' }
let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: url, password: 'OpenProjectSecurePassword') }
- subject { described_class.new(storage:) }
+ subject { described_class.new(storage) }
describe '#call' do
context 'when the source path is blank' do