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

[#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.rb
index 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::Peripherals
yield result
end
- 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)
end
end
end
diff --git a/modules/storages/app/models/storages/upload_finalize_link.rb b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
similarity index 81%
rename from modules/storages/app/models/storages/upload_finalize_link.rb
rename to modules/storages/app/common/storages/peripherals/storage_error_helper.rb
index 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
+ end
end
end
diff --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.rb
index 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::Nextcloud
class DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
using 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_proc
end
def query(file_link)
@@ -48,7 +48,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
def outbound_response(file_link)
- @with_refreshed_token.call(@token) do |token|
+ @retry_proc.call(@token) do |token|
begin
response = ServiceResult.success(
result: RestClient.post(
@@ -83,10 +83,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- 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:)
)
end
diff --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.rb
index 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::Nextcloud
class 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::Nextcloud
http = 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::Nextcloud
end
end
- 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:)
)
end
diff --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.rb
index 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::Nextcloud
class UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
- using Storages::Peripherals::ServiceResultRefinements
+ using Storages::Peripherals::ServiceResultRefinements # use '>>' (bind) operator for ServiceResult
URI_BASE_PATH = '/ocs/v2.php/apps/files_sharing/api/v1/shares'.freeze
UPLOAD_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_url
end
def 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)
end
private
@@ -66,7 +62,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- def share(data)
+ def create_file_share(data)
password = SecureRandom.uuid
outbound_response(
@@ -84,7 +80,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- 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.token
destination.password = share.password
@@ -103,7 +99,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def outbound_response(method:, relative_path:, payload:) # rubocop:disable Metrics/AbcSize
- @with_refreshed_token.call(@token) do |token|
+ @retry_proc.call(@token) do |token|
begin
response = ServiceResult.success(
result: RestClient::Request.execute(
@@ -145,10 +141,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- 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:)
)
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
index 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::StorageInteraction
def download_link_query
case @provider_type
when ::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
)
end
else
@@ -55,11 +55,11 @@ module Storages::Peripherals::StorageInteraction
def upload_link_query(finalize_url)
case @provider_type
when ::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::StorageInteraction
def files_query
case @provider_type
when ::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
)
end
else
@@ -85,7 +85,7 @@ module Storages::Peripherals::StorageInteraction
private
- def with_connection_manager
+ def retry_with_refreshed_token
connection_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:))
end
end
end
diff --git a/modules/storages/app/models/storages/storage_error.rb b/modules/storages/app/models/storages/storage_error.rb
index 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 = data
end
def to_s
output = code.to_s
- output << " | #{message}" unless message.nil?
+ output << " | #{log_message}" unless log_message.nil?
output << " | #{data}" unless data.nil?
output
end
diff --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.rb
index 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::StorageUrlHelper
using 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::StorageErrorHelper
resources :download do
get do
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 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::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
using 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::StorageErrorHelper
resources :files do
get do
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 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 do
end
end
- 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, :error
end
describe '#files_query' do
@@ -306,9 +306,9 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
end
end
- 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, :error
end
describe '#upload_link_query' do
@@ -473,11 +473,11 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
end
end
- 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, :error
end
end