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
WIP - yield with ServiceResult - more or less working
Frank Bergmann4c48c42d0018
diff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rbindex e9737e069f2..7c3dc1657b7 100644--- a/modules/storages/app/services/storages/file_link_sync_service.rb+++ b/modules/storages/app/services/storages/file_link_sync_service.rb@@ -120,7 +120,6 @@ class Storages::FileLinkSyncServiceoauth_client = OAuthClient.find_by(integration_id: storage_id)connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client:)connection_manager_service_result = connection_manager.get_access_token # No scope for Nextcloud...-unless connection_manager_service_result.successset_error_for_file_links(storage_file_links)return@@ -128,31 +127,46 @@ class Storages::FileLinkSyncService@oauth_client_token = connection_manager_service_result.result- # Get info about files. Result is either an Exception or Net::HTTPOK with bodystorage_origin_file_ids = storage_file_links.map(&:origin_id)+ nextcloud_request_result = nextcloud_request_with_token_refresh(connection_manager) do+ response = request_files_info(storage_origin_file_ids)+ # Extract ServiceResult with result = :error, :not_authorized or object with data+ parse_files_info_response(response)+ end+ @service_result.merge!(nextcloud_request_result) # Pass errors from Nextcloud into service result- tries = 2- parsed_response = :not_authorized- while parsed_response == :not_authorized && tries > 0- tries -= 1- http_response = request_files_info(storage_origin_file_ids)- parsed_response = parse_files_info_response(http_response) # symbol for errors, Hash for 200 result-- if parsed_response == :not_authorized && tries > 0- refresh_service_result = connection_manager.refresh_token- unless refresh_service_result.success?- @service_result.merge!(refresh_service_result)- break # parsed_response is correctly :not_authorized here- end- end+ if nextcloud_request_result.failure?+ set_error_for_file_links(storage_file_links)+ returnend- if parsed_response.class.to_s != "Hash"+ if nextcloud_request_result.result.class.to_s != "Hash"set_error_for_file_links(storage_file_links)returnend- set_file_link_permissions(storage_file_links, parsed_response)+ set_file_link_permissions(storage_file_links, nextcloud_request_result.result)+ end++ # @returns ServiceResult with result to be :error or any type of object with data+ def nextcloud_request_with_token_refresh(connection_manager)+ # `yield` needs to returns a ServiceResult:+ # success: result= any object with data+ # failure: result= :error or :not_authorized+ yield_service_result = yield++ if yield_service_result.failure? && yield_service_result.result == :not_authorized+ refresh_service_result = connection_manager.refresh_token+ if refresh_service_result.failure?+ failed_service_result = ServiceResult.failure(result: :error)+ failed_service_result.merge!(refresh_service_result)+ return failed_service_result+ end++ yield_service_result = yield # Should contain result=<data> in case of success+ end++ yield_service_resultenddef set_error_for_file_links(storage_file_links)@@ -208,20 +222,25 @@ class Storages::FileLinkSyncService# Takes a response from querying Nextcloud file IDS (an Exception or a HTTP::Response),# parses the returned JSON and- # @return array of file information or an :error symbol+ # @returns ServiceResult containing data in result and success=true,+ # or success=false with result=:error or result=:not_authorized.def parse_files_info_response(response)- return :error if response.is_a? StandardError- return :error unless response.key?('content-type') # Reply without content-type can't be valid.- return :error if response['content-type'].split(';').first.strip.downcase != 'application/json'- return :not_authorized if response.code == "401" # Nextcloud response if token has expired- return :error if response.code != "200" # Interpret any other response as an error+ return ServiceResult.failure(result: :error) if+ response.nil? ||+ response.is_a?(StandardError) ||+ !response.key?('content-type') || # Reply without content-type can't be valid.+ response['content-type'].split(';').first.strip.downcase != 'application/json'++ return ServiceResult.failure(result: :not_authorized) if response.code == "401" # Nextcloud response if token is not valid+ return ServiceResult.failure(result: :error) unless response.code == "200" # Interpret any other response as an errorbeginresponse_hash = JSON.parse(response.body).dig('ocs', 'data')- rescue JSON::ParserError- return :error+ rescue JSON::ParserError => e+ return ServiceResult.failure(result: :error)+ .errors.add(:base, "JSON parser error: #{e.message}")end- response_hash+ ServiceResult.success(result: response_hash)endenddiff --git a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rbindex 526b34d3d1b..970569e2e88 100644--- a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb+++ b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb@@ -276,7 +276,7 @@ describe ::Storages::FileLinkSyncService, type: :model do.to receive(:refresh_token).and_return(ServiceResult.failure.tap do |result|- result.errors.add(:base, "Error")+ result.errors.add(:base, "Error from Nextcloud")end)allow(connection_manager)@@ -286,7 +286,7 @@ describe ::Storages::FileLinkSyncService, type: :model doit 'returns a failed ServiceResult with an error' doexpect(subject).to be_a ServiceResultexpect(subject.success).to be_falsey- expect(subject.message).to include "Error"+ expect(subject.message).to include "Error from Nextcloud"endend