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
[#44874] Fixed suggestions from code review
Andreas Pfohl64b86b23feb5
diff --git a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb b/modules/storages/app/common/storages/peripherals/service_result_refinements.rbindex 4fe200e790c..7b721177f7d 100644--- a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb@@ -42,21 +42,13 @@ module Storages::Peripheralsyield resultend- end-- refine(ServiceResult.singleton_class) do- def chain(initial:, steps:) # rubocop:disable Metrics/AbcSize- unless initial.instance_of?(ServiceResult)- raise TypeError, "Expected a #{ServiceResult.name.split('::').last}, got #{initial.class.name}."- end- unless steps.instance_of?(Array) && steps.all? { |step| step.instance_of?(Method) }- raise TypeError, "Expected an Array of Method, got #{steps.class.name}."+ def >>(other)+ unless other.respond_to?(:call)+ raise TypeError, "Expected an object responding to 'call', got #{other.class.name}."end- steps.map(&:to_proc).reduce(initial) do |state, method|- state.bind { |value| method.call(value) }- end+ bind(&other)endendenddiff --git a/modules/storages/app/models/storages/upload_finalize_link.rb b/modules/storages/app/common/storages/peripherals/storage_error_helper.rbsimilarity index 81%rename from modules/storages/app/models/storages/upload_finalize_link.rbrename to modules/storages/app/common/storages/peripherals/storage_error_helper.rbindex c13f625ef3f..ea122e492a1 100644--- a/modules/storages/app/models/storages/upload_finalize_link.rb+++ b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb@@ -26,11 +26,17 @@# See COPYRIGHT and LICENSE files for more details.#++-class Storages::UploadFinalizeLink- attr_reader :destination, :payload+module Storages::Peripherals+ module StorageErrorHelper+ def raise_error(error)+ Rails.logger.error(error)- def initialize(destination = '', payload = nil)- @destination = destination- @payload = payload+ case error.code+ when :not_found+ raise API::Errors::NotFound.new+ else+ raise API::Errors::InternalError.new+ end+ endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rbindex 3b9795be54b..86972b9731d 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb@@ -30,13 +30,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloudclass DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQueryusing Storages::Peripherals::ServiceResultRefinements- def initialize(base_uri:, token:, with_refreshed_token:)+ def initialize(base_uri:, token:, retry_proc:)super()@base_uri = base_uri@uri = URI::join(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')@token = token- @with_refreshed_token = with_refreshed_token+ @retry_proc = retry_procenddef query(file_link)@@ -48,7 +48,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudprivatedef outbound_response(file_link)- @with_refreshed_token.call(@token) do |token|+ @retry_proc.call(@token) do |token|beginresponse = ServiceResult.success(result: RestClient.post(@@ -83,10 +83,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- def error(code, message = nil, data = nil)+ def error(code, log_message = nil, data = nil)ServiceResult.failure(result: code, # This is needed to work with the ConnectionManager token refresh mechanism.- errors: Storages::StorageError.new(code:, message:, data:)+ errors: Storages::StorageError.new(code:, log_message:, data:))enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rbindex d582da0bf27..7cd93866c96 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb@@ -28,11 +28,11 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass FilesQuery < Storages::Peripherals::StorageInteraction::StorageQuery- def initialize(base_uri:, token:, with_refreshed_token:)+ def initialize(base_uri:, token:, retry_proc:)super()@uri = base_uri@token = token- @with_refreshed_token = with_refreshed_token+ @retry_proc = retry_proc@base_path = "/remote.php/dav/files/#{token.origin_user_id}"end@@ -40,7 +40,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudhttp = Net::HTTP.new(@uri.host, @uri.port)http.use_ssl = @uri.scheme == 'https'- result = @with_refreshed_token.call(@token) do |token|+ result = @retry_proc.call(@token) do |token|response = http.propfind("#{@base_path}#{requested_folder(parent)}",requested_properties,@@ -92,10 +92,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- def error_result(code, message = nil, data = nil)+ def error_result(code, log_message = nil, data = nil)ServiceResult.failure(result: code, # This is needed to work with the ConnectionManager token refresh mechanism.- errors: Storages::StorageError.new(code:, message:, data:)+ errors: Storages::StorageError.new(code:, log_message:, data:))enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rbindex 1572cc3b173..442db38a256 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb@@ -28,29 +28,25 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery- using Storages::Peripherals::ServiceResultRefinements+ using Storages::Peripherals::ServiceResultRefinements # use '>>' (bind) operator for ServiceResultURI_BASE_PATH = '/ocs/v2.php/apps/files_sharing/api/v1/shares'.freezeUPLOAD_LINK_BASE = '/public.php/webdav'.freeze- def initialize(base_uri:, token:, with_refreshed_token:, finalize_url:)+ def initialize(base_uri:, token:, retry_proc:, finalize_url:)super()@base_uri = base_uri@token = token- @with_refreshed_token = with_refreshed_token+ @retry_proc = retry_proc@finalize_url = finalize_urlenddef query(data)- ServiceResult.chain(- initial: validated(data),- steps: [- method(:share),- method(:file_drop_share),- method(:upload_link)- ]- )+ validated(data) >>+ method(:create_file_share) >>+ method(:apply_drop_permission) >>+ method(:build_upload_link)endprivate@@ -66,7 +62,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- def share(data)+ def create_file_share(data)password = SecureRandom.uuidoutbound_response(@@ -84,7 +80,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- def file_drop_share(share)+ def apply_drop_permission(share)outbound_response(method: :put,relative_path: "#{URI_BASE_PATH}/#{share.id}",@@ -94,7 +90,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud).map { share }end- def upload_link(share)+ def build_upload_link(share)destination = @base_uri.merge("#{UPLOAD_LINK_BASE}/#{share.file_name}")destination.user = share.tokendestination.password = share.password@@ -103,7 +99,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudenddef outbound_response(method:, relative_path:, payload:) # rubocop:disable Metrics/AbcSize- @with_refreshed_token.call(@token) do |token|+ @retry_proc.call(@token) do |token|beginresponse = ServiceResult.success(result: RestClient::Request.execute(@@ -145,10 +141,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- def error(code, message = nil, data = nil)+ def error(code, log_message = nil, data = nil)ServiceResult.failure(result: code, # This is needed to work with the ConnectionManager token refresh mechanism.- errors: Storages::StorageError.new(code:, message:, data:)+ errors: Storages::StorageError.new(code:, log_message:, data:))endenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rbindex 31ebaf46371..dadd23a77da 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb@@ -40,11 +40,11 @@ module Storages::Peripherals::StorageInteractiondef download_link_querycase @provider_typewhen ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD- with_connection_manager do |token, with_refreshed_token|+ retry_with_refreshed_token do |token, with_refreshed_token_proc|::Storages::Peripherals::StorageInteraction::Nextcloud::DownloadLinkQuery.new(base_uri: @uri,token:,- with_refreshed_token:+ retry_proc: with_refreshed_token_proc)endelse@@ -55,11 +55,11 @@ module Storages::Peripherals::StorageInteractiondef upload_link_query(finalize_url)case @provider_typewhen ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD- with_connection_manager do |token, with_refreshed_token|+ retry_with_refreshed_token do |token, with_refreshed_token_proc|::Storages::Peripherals::StorageInteraction::Nextcloud::UploadLinkQuery.new(base_uri: @uri,token:,- with_refreshed_token:,+ retry_proc: with_refreshed_token_proc,finalize_url:)end@@ -71,11 +71,11 @@ module Storages::Peripherals::StorageInteractiondef files_querycase @provider_typewhen ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD- with_connection_manager do |token, with_refreshed_token|+ retry_with_refreshed_token do |token, with_refreshed_token_proc|::Storages::Peripherals::StorageInteraction::Nextcloud::FilesQuery.new(base_uri: @uri,token:,- with_refreshed_token:+ retry_proc: with_refreshed_token_proc)endelse@@ -85,7 +85,7 @@ module Storages::Peripherals::StorageInteractionprivate- def with_connection_manager+ def retry_with_refreshed_tokenconnection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)connection_manager.get_access_token.match(on_success: ->(token) do@@ -95,8 +95,8 @@ module Storages::Peripherals::StorageInteraction)end- def error(code, message = nil, data = nil)- ServiceResult.failure(errors: Storages::StorageError.new(code:, message:, data:))+ def error(code, log_message = nil, data = nil)+ ServiceResult.failure(errors: Storages::StorageError.new(code:, log_message:, data:))endendenddiff --git a/modules/storages/app/models/storages/storage_error.rb b/modules/storages/app/models/storages/storage_error.rbindex 92d8ef70005..2a73edb6825 100644--- a/modules/storages/app/models/storages/storage_error.rb+++ b/modules/storages/app/models/storages/storage_error.rb@@ -27,17 +27,17 @@#++class Storages::StorageError- attr_reader :code, :message, :data+ attr_reader :code, :log_message, :data- def initialize(code:, message: nil, data: nil)+ def initialize(code:, log_message: nil, data: nil)@code = code- @message = message+ @log_message = log_message@data = dataenddef to_soutput = code.to_s- output << " | #{message}" unless message.nil?+ output << " | #{log_message}" unless log_message.nil?output << " | #{data}" unless data.nil?outputenddiff --git a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb b/modules/storages/lib/api/v3/file_links/file_links_download_api.rbindex 6b46b6dfcba..93db60161c5 100644--- a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb+++ b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb@@ -27,21 +27,8 @@#++class API::V3::FileLinks::FileLinksDownloadAPI < ::API::OpenProjectAPI- helpers Storages::Peripherals::StorageUrlHelperusing Storages::Peripherals::ServiceResultRefinements-- helpers do- def raise_error(error)- Rails.logger.error(error)-- case error.code- when :not_found- raise API::Errors::NotFound.new- else- raise API::Errors::InternalError.new- end- end- end+ helpers Storages::Peripherals::StorageUrlHelper, Storages::Peripherals::StorageErrorHelperresources :download doget dodiff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rbindex dc22fa3bd35..467b81322ba 100644--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb@@ -29,19 +29,7 @@module API::V3::StorageFilesclass StorageFilesAPI < ::API::OpenProjectAPIusing Storages::Peripherals::ServiceResultRefinements-- helpers do- def raise_error(error)- Rails.logger.error(error)-- case error.code- when :not_found- raise API::Errors::NotFound.new- else- raise API::Errors::InternalError.new- end- end- end+ helpers Storages::Peripherals::StorageErrorHelperresources :files doget dodiff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rbindex aa3faece490..903b9932641 100644--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb@@ -178,9 +178,9 @@ describe Storages::Peripherals::StorageRequests, webmock: true doendend- it_behaves_like 'outbound is failing', 404, :not_found- it_behaves_like 'outbound is failing', 401, :not_authorized- it_behaves_like 'outbound is failing', 500, :error+ include_examples 'outbound is failing', 404, :not_found+ include_examples 'outbound is failing', 401, :not_authorized+ include_examples 'outbound is failing', 500, :errorenddescribe '#files_query' do@@ -306,9 +306,9 @@ describe Storages::Peripherals::StorageRequests, webmock: true doendend- it_behaves_like 'outbound is failing', 404, :not_found- it_behaves_like 'outbound is failing', 401, :not_authorized- it_behaves_like 'outbound is failing', 500, :error+ include_examples 'outbound is failing', 404, :not_found+ include_examples 'outbound is failing', 401, :not_authorized+ include_examples 'outbound is failing', 500, :errorenddescribe '#upload_link_query' do@@ -473,11 +473,11 @@ describe Storages::Peripherals::StorageRequests, webmock: true doendend- it_behaves_like 'outbound is failing', :post, '', 404, :not_found- it_behaves_like 'outbound is failing', :post, '', 401, :not_authorized- it_behaves_like 'outbound is failing', :post, '', 500, :error- it_behaves_like 'outbound is failing', :put, '/37', 404, :not_found- it_behaves_like 'outbound is failing', :put, '/37', 401, :not_authorized- it_behaves_like 'outbound is failing', :put, '/37', 500, :error+ include_examples 'outbound is failing', :post, '', 404, :not_found+ include_examples 'outbound is failing', :post, '', 401, :not_authorized+ include_examples 'outbound is failing', :post, '', 500, :error+ include_examples 'outbound is failing', :put, '/37', 404, :not_found+ include_examples 'outbound is failing', :put, '/37', 401, :not_authorized+ include_examples 'outbound is failing', :put, '/37', 500, :errorendend