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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Bail out when copying fails
Marcello Rochaa767385520fb
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.rbindex 2336a953005..6caf5d2cf20 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@@ -33,8 +33,8 @@ module Storagesclass CopyTemplateFolderCommandusing ServiceResultRefinements- def self.call(storage:, source_path:, destination_path:)- new(storage).call(source_path:, destination_path:)+ def self.call(auth_strategy:, storage:, source_path:, destination_path:)+ new(storage).call(auth_strategy:, source_path:, destination_path:)enddef initialize(storage)@@ -42,19 +42,18 @@ module Storages@data = ResultData::CopyTemplateFolder.new(id: nil, polling_url: nil, requires_polling: false)end- def call(source_path:, destination_path:)+ def call(auth_strategy:, source_path:, destination_path:)valid_input_result = validate_inputs(source_path, destination_path).on_failure { |failure| return failure }remote_urls = build_origin_urls(**valid_input_result.result)- ensure_remote_folder_does_not_exist(remote_urls[:destination_url]).on_failure { |failure| return failure }+ ensure_remote_folder_does_not_exist(auth_strategy, remote_urls[:destination_url]).on_failure do |failure|+ return failure+ end- copy_folder(**remote_urls).on_failure { |failure| return failure }+ copy_folder(auth_strategy, **remote_urls).on_failure { |failure| return failure }- get_folder_id(valid_input_result.result[:destination_path]).on_success do |command_result|- return ServiceResult- .success(result: @data.with(id: command_result.result[destination_path]["fileid"]))- end+ get_folder_id(valid_input_result.result[:destination_path])endprivate@@ -87,45 +86,67 @@ module Storages{ source_url:, destination_url: }end- def ensure_remote_folder_does_not_exist(destination_url)- response = OpenProject.httpx.basic_auth(@storage.username, @storage.password).head(destination_url)+ def ensure_remote_folder_does_not_exist(auth_strategy, destination_url)+ response = Authentication[auth_strategy].call(storage: @storage) { |http| http.head(destination_url) }case responsein { status: 200..299 }- Util.error(:conflict, "Destination folder already exists.")+ ServiceResult.failure(result: :conflict,+ errors: Util.storage_error(+ response:, code: :conflict, source: self.class,+ log_message: "The copy would overwrite an already existing folder"+ ))in { status: 401 }- Util.error(:unauthorized, "unauthorized (validate_destination)")+ ServiceResult.failure(result: :unauthorized,+ errors: Util.storage_error(response:, code: :unauthorized, source: self.class))in { status: 404 }ServiceResult.successelse- Util.error(:unknown, "Unexpected response (validate_destination): #{response.code}", response)+ ServiceResult.failure(result: :error,+ errors: Util.storage_error(response:, code: :error, source: self.class))+ end+ end++ def copy_folder(auth_strategy, source_url:, destination_url:)+ response = Authentication[auth_strategy].call(storage: @storage) do |http|+ http.request("COPY", source_url, headers: { "Destination" => destination_url, "Depth" => "infinity" })end++ handle_response(response)end- def copy_folder(source_url:, destination_url:)- response = OpenProject- .httpx- .basic_auth(@storage.username, @storage.password)- .request("COPY", source_url, headers: { "Destination" => destination_url, "Depth" => "infinity" })+ def handle_response(response)+ source = self.classcase responsein { status: 200..299 }ServiceResult.success(message: "Folder was successfully copied")in { status: 401 }- Util.error(:unauthorized, "Unauthorized (copy_folder)")+ ServiceResult.failure(result: :unauthorized,+ errors: Util.storage_error(response:, code: :unauthorized, source:))+ in { status: 403 }+ ServiceResult.failure(result: :forbidden,+ errors: Util.storage_error(response:, code: :forbidden, source:))in { status: 404 }- Util.error(:not_found, "Project folder not found (copy_folder)")+ ServiceResult.failure(result: :not_found,+ errors: Util.storage_error(response:, code: :not_found, source:,+ log_message: "Template folder not found"))in { status: 409 }- Util.error(:conflict, Util.error_text_from_response(response))+ ServiceResult.failure(result: :conflict,+ errors: Util.storage_error(+ response:, code: :conflict, source:,+ log_message: Util.error_text_from_response(response)+ ))else- Util.error(:unknown, "Unexpected response (copy_folder): #{response.status}", response)+ ServiceResult.failure(result: :error,+ errors: Util.storage_error(response:, code: :error, source:))endenddef get_folder_id(destination_path)Registry.resolve("#{@storage.short_provider_type}.queries.file_ids")- .call(storage: @storage, path: destination_path)+ .call(storage: @storage, path: destination_path).map { |result| @data.with(id: result[destination_path]["fileid"]) }endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rbindex 6a1745cb17c..23ebcad28f5 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb@@ -54,6 +54,13 @@ module Storages{ headers: { "Depth" => number } }end+ def storage_error(response:, code:, source:, log_message: nil)+ # Some errors, like timeouts, aren't json responses so we need to adapt+ data = StorageErrorData.new(source:, payload: response.to_s)++ StorageError.new(code:, data:, log_message:)+ end+def error(code, log_message = nil, data = nil)ServiceResult.failure(result: code, # This is needed to work with the ConnectionManager token refresh mechanism.diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command.rbindex 30fc2a1a397..56e418d9161 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command.rb@@ -33,7 +33,7 @@ module Storagesmodule StorageInteractionmodule OneDriveclass CopyTemplateFolderCommand- def self.call(storage:, source_path:, destination_path:)+ def self.call(auth_strategy:, storage:, source_path:, destination_path:)if source_path.blank? || destination_path.blank?return ServiceResult.failure(result: :error,@@ -42,7 +42,7 @@ module Storages)end- new(storage).call(source_location: source_path, destination_name: destination_path)+ new(storage).call(auth_strategy:, source_location: source_path, destination_name: destination_path)enddef initialize(storage)@@ -50,8 +50,8 @@ module Storages@data = ResultData::CopyTemplateFolder.new(id: nil, polling_url: nil, requires_polling: true)end- def call(source_location:, destination_name:)- Util.using_admin_token(@storage) do |httpx|+ def call(auth_strategy:, source_location:, destination_name:)+ Authentication[auth_strategy].call(storage: @storage) do |httpx|handle_response(httpx.post(copy_path_for(source_location), json: { name: destination_name }))@@ -61,24 +61,35 @@ module Storagesprivatedef handle_response(response)+ source = self.class+case responsein { status: 202 }ServiceResult.success(result: @data.with(polling_url: response.headers[:location]))in { status: 401 }- ServiceResult.failure(result: :unauthorized)+ ServiceResult.failure(result: :unauthorized,+ errors: Util.storage_error(response:, code: :unauthorized, source:))in { status: 403 }- ServiceResult.failure(result: :forbidden)+ ServiceResult.failure(result: :forbidden,+ errors: Util.storage_error(response:, code: :forbidden, source:))in { status: 404 }- ServiceResult.failure(result: :not_found, message: "Template folder not found")+ ServiceResult.failure(result: :not_found,+ errors: Util.storage_error(response:, code: :not_found, source:,+ log_message: "Template folder not found"))in { status: 409 }- ServiceResult.failure(result: :conflict, message: "The copy would overwrite an already existing folder")+ ServiceResult.failure(result: :conflict,+ errors: Util.storage_error(+ response:, code: :conflict, source:,+ log_message: "The copy would overwrite an already existing folder"+ ))else- ServiceResult.failure(result: :error)+ ServiceResult.failure(result: :error,+ errors: Util.storage_error(response:, code: :error, source:))endenddef copy_path_for(source_location)- "/v1.0/drives/#{@storage.drive_id}/items/#{source_location}/[email protected]=fail"+ "#{@storage.uri}v1.0/drives/#{@storage.drive_id}/items/#{source_location}/[email protected]=fail"endendenddiff --git a/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb b/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rbindex 3957ffe04b1..68f4b41e820 100644--- a/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb+++ b/modules/storages/app/services/storages/project_storages/copy_project_folders_service.rb@@ -44,9 +44,12 @@ module Storagesreturn ServiceResult.success(result: @data) if source.project_folder_inactive?return ServiceResult.success(result: @data.with(id: source.project_folder_id)) if source.project_folder_manual?+ auth_strategy = Peripherals::Registry.resolve("#{source.storage.short_provider_type}.authentication.userless").call+Peripherals::Registry.resolve("#{source.storage.short_provider_type}.commands.copy_template_folder")- .call(storage: source.storage,+ .call(auth_strategy:,+ storage: source.storage,source_path: source.project_folder_location,destination_path: target.managed_project_folder_path)enddiff --git a/modules/storages/app/workers/storages/copy_project_folders_job.rb b/modules/storages/app/workers/storages/copy_project_folders_job.rbindex 4d186d746f0..210b1eb98c9 100644--- a/modules/storages/app/workers/storages/copy_project_folders_job.rb+++ b/modules/storages/app/workers/storages/copy_project_folders_job.rb@@ -42,6 +42,7 @@ module Storagesuser = batch.properties[:user]project_folder_result = results_from_polling || initiate_copy(target)+ project_folder_result.on_failure { |failed| return log_failure(failed) }ProjectStorages::UpdateService.new(user:, model: target).call(project_folder_id: project_folder_result.result.id,@@ -95,7 +96,16 @@ module Storagesdef log_failure(failed)return if failed.success?- OpenProject.logger.warn failed.errors.inspect.to_s+ if failed.errors.is_a? StorageError+ errors = failed.errors.to_active_model_errors.full_messages+ batch.properties[:errors].push(*errors)+ else+ batch.properties[:errors].push(*failed.errors.to_s)+ end++ batch.save++ OpenProject.logger.warn failed.errors.to_senddef polling_infodiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rbindex ae17be709bc..4360ddf5791 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb@@ -43,6 +43,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplatlet(:destination_path) { "boring-destination" }let(:source_url) { "#{url}/remote.php/dav/files/#{CGI.escape(origin_user_id)}/#{source_path}" }let(:destination_url) { "#{url}/remote.php/dav/files/#{CGI.escape(origin_user_id)}/#{destination_path}" }+ let(:auth_strategy) { Storages::Peripherals::StorageInteraction::AuthenticationStrategies::NextcloudStrategies::UserLess.call }subject { described_class.new(storage) }@@ -51,14 +52,14 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplatdescribe "parameter validation" doit "source_path cannot be blank" do- result = subject.call(source_path: "", destination_path: "destination")+ result = subject.call(auth_strategy:, source_path: "", destination_path: "destination")expect(result).to be_failureexpect(result.errors.log_message).to eq("Source and destination paths must be present.")endit "destination_path cannot blank" do- result = subject.call(source_path: "source", destination_path: "")+ result = subject.call(auth_strategy:, source_path: "source", destination_path: "")expect(result).to be_failureexpect(result.errors.log_message).to eq("Source and destination paths must be present.")@@ -68,10 +69,10 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplatdescribe "remote server overwrite protection" doit "destination_path must not exist on the remote server" dostub_request(:head, destination_url).to_return(status: 200)- result = subject.call(source_path:, destination_path:)+ result = subject.call(auth_strategy:, source_path:, destination_path:)expect(result).to be_failure- expect(result.errors.log_message).to eq("Destination folder already exists.")+ expect(result.errors.log_message).to eq("The copy would overwrite an already existing folder")endend@@ -112,7 +113,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplatendit "must be successful" do- result = subject.call(source_path:, destination_path:)+ result = subject.call(auth_strategy:, source_path:, destination_path:)expect(result).to be_successexpect(result.result.id).to eq("349")@@ -134,7 +135,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplatendit "returns a :conflict failure if the copy fails" do- result = subject.call(source_path:, destination_path:)+ result = subject.call(auth_strategy:, source_path:, destination_path:)expect(result).to be_failureexpect(result.errors.code).to eq(:conflict)diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command_spec.rbindex fe18cd81966..2b882e84289 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/copy_template_folder_command_spec.rb@@ -50,8 +50,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::CopyTemplateshared_let(:source_path) { base_template_folder.id }- it "is registered under commands.one_drive.copy_template_folder",- skip: "Skipped while we decide on what to do with the copy project folder" do+ it "is registered under commands.one_drive.copy_template_folder" doexpect(Storages::Peripherals::Registry.resolve("one_drive.commands.copy_template_folder")).to eq(described_class)end@@ -62,13 +61,14 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::CopyTemplateit ".call takes 3 required parameters: storage, source_path, destination_path" domethod = described_class.method(:call)- expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq source_path], %i[keyreq destination_path])+ expect(method.parameters)+ .to contain_exactly(%i[keyreq auth_strategy], %i[keyreq storage], %i[keyreq source_path], %i[keyreq destination_path])endit "destination_path and source_path can't be empty" do- missing_source = described_class.call(storage:, source_path: "", destination_path: "Path")- missing_path = described_class.call(storage:, source_path: "Path", destination_path: nil)- missing_both = described_class.call(storage:, source_path: nil, destination_path: "")+ missing_source = described_class.call(auth_strategy:, storage:, source_path: "", destination_path: "Path")+ missing_path = described_class.call(auth_strategy:, storage:, source_path: "Path", destination_path: nil)+ missing_both = described_class.call(auth_strategy:, storage:, source_path: nil, destination_path: "")expect([missing_both, missing_path, missing_source]).to all(be_failure)end@@ -92,7 +92,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::CopyTemplateit "copies origin folder and all underlying files and folders to the destination_path",vcr: "one_drive/copy_template_folder_copy_successful" do- command_result = described_class.call(storage:, source_path:, destination_path: "My New Folder")+ command_result = described_class.call(auth_strategy:, storage:, source_path:, destination_path: "My New Folder")expect(command_result).to be_successexpect(command_result.result).to be_requires_polling@@ -104,36 +104,32 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::CopyTemplatedescribe "error handling" docontext "when the source_path does not exist" doit "fails", vcr: "one_drive/copy_template_source_not_found" do- result = described_class.call(storage:, source_path: "TheCakeIsALie", destination_path: "Not Happening")+ result = described_class.call(auth_strategy:, storage:, source_path: "TheCakeIsALie", destination_path: "Not Happening")expect(result).to be_failureendit "explains the nature of the error", vcr: "one_drive/copy_template_source_not_found" do- result = described_class.call(storage:, source_path: "TheCakeIsALie", destination_path: "Not Happening")+ result = described_class.call(auth_strategy:, storage:, source_path: "TheCakeIsALie", destination_path: "Not Happening")- expect(result.message).to eq("Template folder not found")+ expect(result.errors.to_s).to match(/not_found \| Template folder not found/)end-- it "logs the occurrence"endcontext "when it would overwrite an already existing folder" doit "fails", vcr: "one_drive/copy_template_folder_no_overwrite" doexisting_folder = original_folders.first[:name]- result = described_class.call(storage:, source_path:, destination_path: existing_folder)+ result = described_class.call(auth_strategy:, storage:, source_path:, destination_path: existing_folder)expect(result).to be_failureendit "explains the nature of the error", vcr: "one_drive/copy_template_folder_no_overwrite" doexisting_folder = original_folders.first[:name]- result = described_class.call(storage:, source_path:, destination_path: existing_folder)+ result = described_class.call(auth_strategy:, storage:, source_path:, destination_path: existing_folder)- expect(result.message).to eq("The copy would overwrite an already existing folder")+ expect(result.errors.to_s).to match(/conflict \| The copy would overwrite an already existing folder/)end-- it "logs the occurrence"endendenddiff --git a/modules/storages/spec/services/storages/project_storages/copy_project_folders_service_spec.rb b/modules/storages/spec/services/storages/project_storages/copy_project_folders_service_spec.rbindex aae1dbce88c..6e4feef1bf3 100644--- a/modules/storages/spec/services/storages/project_storages/copy_project_folders_service_spec.rb+++ b/modules/storages/spec/services/storages/project_storages/copy_project_folders_service_spec.rb@@ -28,7 +28,7 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'spec_helper'+require "spec_helper"require_module_spec_helperRSpec.describe Storages::ProjectStorages::CopyProjectFoldersService, :webmock do@@ -42,35 +42,39 @@ RSpec.describe Storages::ProjectStorages::CopyProjectFoldersService, :webmock docontext "with automatically managed project folders" dolet(:source) { create(:project_storage, :as_automatically_managed, storage:) }- it 'if polling is required, returns a nil id and an url' do+ it "if polling is required, returns a nil id and an url" doStorages::Peripherals::Registry.stub("#{source.storage.short_provider_type}.commands.copy_template_folder",- ->(storage:, source_path:, destination_path:) do+ ->(auth_strategy:, storage:, source_path:, destination_path:) do+ strategy = Storages::Peripherals::Registry+ .resolve("#{source.storage.short_provider_type}.authentication.userless").call++ expect(auth_strategy.class).to eq(strategy.class)expect(storage).to eq(source.storage)expect(source_path).to eq(source.project_folder_location)expect(destination_path).to eq(target.managed_project_folder_path)# Return a success for the provider copy with no polling required- ServiceResult.success(result: result_data.with(polling_url: 'https://polling.url.de/cool/subresources'))+ ServiceResult.success(result: result_data.with(polling_url: "https://polling.url.de/cool/subresources"))end)result = service.call(source:, target:)expect(result).to be_success- expect(result.result.to_h).to eq({ id: nil, polling_url: 'https://polling.url.de/cool/subresources',+ expect(result.result.to_h).to eq({ id: nil, polling_url: "https://polling.url.de/cool/subresources",requires_polling: false })endendcontext "with manually managed project folders" do- let(:source) { create(:project_storage, project_folder_id: 'this_is_a_unique_id', project_folder_mode: 'manual') }+ let(:source) { create(:project_storage, project_folder_id: "this_is_a_unique_id", project_folder_mode: "manual") }it "succeeds" doresult = service.call(source:, target:)expect(result).to be_successend- it 'returns the source folder id' do+ it "returns the source folder id" doresult = service.call(source:, target:)expect(result.result.id).to eq(source.project_folder_id)@@ -78,13 +82,13 @@ RSpec.describe Storages::ProjectStorages::CopyProjectFoldersService, :webmock doendcontext "with non-managed project folders" do- let(:source) { create(:project_storage, project_folder_id: nil, project_folder_mode: 'inactive') }+ let(:source) { create(:project_storage, project_folder_id: nil, project_folder_mode: "inactive") }it "succeeds" doexpect(service.call(source:, target:)).to be_successend- it 'returns the origin folder id (nil)' do+ it "returns the origin folder id (nil)" doresult = service.call(source:, target:)expect(result.result.id).to eq(source.project_folder_id)diff --git a/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb b/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rbindex 7ed8bdc9090..697b4a104bb 100644--- a/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb+++ b/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb@@ -133,9 +133,10 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock, with_good_job_ba})Storages::Peripherals::Registry- .stub("#{storage.short_provider_type}.commands.copy_template_folder", ->(storage:, source_path:, destination_path:) {- ServiceResult.success(result: copy_result.with(id: "copied-folder", polling_url:))- })+ .stub("#{storage.short_provider_type}.commands.copy_template_folder",+ ->(auth_strategy:, storage:, source_path:, destination_path:) {+ ServiceResult.success(result: copy_result.with(id: "copied-folder", polling_url:))+ })endit "copies the folders from source to target" do@@ -159,79 +160,114 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock, with_good_job_baexpect(file_link.origin_id).to eq(target_deep_file_ids["/#{target.managed_project_folder_path}#{file_link.name}"].id)endend- end-- context "when the storage requires polling" do- let(:copy_incomplete_response) do- { operation: "ItemCopy", percentageComplete: 27.8, status: "inProgress" }.to_json- end- let(:copy_complete_response) do- { percentageComplete: 100.0, resourceId: "01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM", status: "completed" }.to_json- end+ context "when the storage requires polling" do+ let(:copy_incomplete_response) do+ { operation: "ItemCopy", percentageComplete: 27.8, status: "inProgress" }.to_json+ end- before do- Storages::Peripherals::Registry- .stub("#{storage.short_provider_type}.commands.copy_template_folder", ->(storage:, source_path:, destination_path:) {- ServiceResult.success(result: copy_result.with(polling_url:, requires_polling: true))- })+ let(:copy_complete_response) do+ { percentageComplete: 100.0, resourceId: "01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM", status: "completed" }.to_json+ end- Storages::Peripherals::Registry- .stub("#{storage.short_provider_type}.queries.file_path_to_id_map", ->(storage:, auth_strategy:, folder:) {- ServiceResult.success(result: target_deep_file_ids)- })+ before do+ Storages::Peripherals::Registry+ .stub("#{storage.short_provider_type}.commands.copy_template_folder",+ ->(auth_strategy:, storage:, source_path:, destination_path:) {+ ServiceResult.success(result: copy_result.with(polling_url:, requires_polling: true))+ })++ Storages::Peripherals::Registry+ .stub("#{storage.short_provider_type}.queries.file_path_to_id_map", ->(storage:, auth_strategy:, folder:) {+ ServiceResult.success(result: target_deep_file_ids)+ })++ Storages::Peripherals::Registry+ .stub("#{storage.short_provider_type}.queries.files_info", ->(storage:, auth_strategy:, file_ids:) {+ ServiceResult.success(result: source_file_infos)+ })- Storages::Peripherals::Registry- .stub("#{storage.short_provider_type}.queries.files_info", ->(storage:, auth_strategy:, file_ids:) {- ServiceResult.success(result: source_file_infos)- })+ stub_request(:get, polling_url)+ .and_return(status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" })+ end- stub_request(:get, polling_url)- .and_return(status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" })- end+ it "stores the polling url on the batch properties" do+ batch = GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source:, target:, work_packages_map:)+ end+ GoodJob.perform_inline+ batch.reload- it "stores the polling url on the batch properties" do- batch = GoodJob::Batch.enqueue(user:) do- described_class.perform_later(source:, target:, work_packages_map:)+ expect(batch.properties.dig(:polling, source.id.to_s, :polling_url)).to eq(polling_url)+ expect(batch.properties.dig(:polling, source.id.to_s, :polling_state)).to eq(:ongoing)end- GoodJob.perform_inline- batch.reload- expect(batch.properties.dig(:polling, source.id.to_s, :polling_url)).to eq(polling_url)- expect(batch.properties.dig(:polling, source.id.to_s, :polling_state)).to eq(:ongoing)+ context "when the polling completes" do+ before do+ stub_request(:get, polling_url)+ .and_return(+ { status: 202, body: copy_incomplete_response, headers: { "Content-Type" => "application/json" } },+ { status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" } }+ )+ end++ it "updates the storages" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source:, target:, work_packages_map:)+ end+ GoodJob.perform_inline++ target.reload+ expect(target.project_folder_mode).to eq(source.project_folder_mode)+ expect(target.project_folder_id).to eq("01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM")+ end++ it "handles re-enqueues and polling" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source:, target:, work_packages_map:)+ end+ GoodJob.perform_inline+ job = GoodJob::Job.order(:created_at).last++ expect(job.executions_count).to eq(3)+ expect(job.serialized_params["exception_executions"]["[Storages::Errors::PollingRequired]"]).to eq(2)+ end+ endend- context "when the polling completes" do+ context "when an error occurs" dobefore do- stub_request(:get, polling_url)- .and_return(- { status: 202, body: copy_incomplete_response, headers: { "Content-Type" => "application/json" } },- { status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" } }- )+ Storages::Peripherals::Registry+ .stub("#{storage.short_provider_type}.commands.copy_template_folder",+ ->(auth_strategy:, storage:, source_path:, destination_path:) {+ ServiceResult.failure(+ result: :error,+ errors: Storages::StorageError.new(code: :error, log_message: "General Failure reporting for duty")+ )+ })end- it "updates the storages" do- GoodJob::Batch.enqueue(user:) do+ it "records the error on batch" do+ batch = GoodJob::Batch.enqueue(user:, errors: []) dodescribed_class.perform_later(source:, target:, work_packages_map:)end+GoodJob.perform_inline+ batch.reload- target.reload- expect(target.project_folder_mode).to eq(source.project_folder_mode)- expect(target.project_folder_id).to eq("01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM")+ expect(batch.properties[:errors]).not_to be_emptyend- it "handles re-enqueues and polling" do- GoodJob::Batch.enqueue(user:) do- described_class.perform_later(source:, target:, work_packages_map:)- end- GoodJob.perform_inline- job = GoodJob::Job.order(:created_at).last+ it "logs the error" do+ allow(OpenProject.logger).to receive(:warn)+ GoodJob::Batch.enqueue(user:, errors: []) { described_class.perform_later(source:, target:, work_packages_map:) }- expect(job.executions_count).to eq(3)- expect(job.serialized_params["exception_executions"]["[Storages::Errors::PollingRequired]"]).to eq(2)+ GoodJob.perform_inline+ expect(OpenProject.logger).to have_received(:warn)+ .with("error | General Failure reporting for duty")+ .onceendend+ # rubocop:enable Lint/UnusedBlockArgumentend- # rubocop:enable Lint/UnusedBlockArgumentend