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
Introduce new module Util inside storages.
Pavel Balashov1a4150415b3c
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex b40501d1192..8ca7f885d7e 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -586,14 +586,6 @@ module APIroot_url.gsub(duplicate_regexp, '') + send(path, arguments)end-- def self.join_uri_path(uri, *parts)- # We use `File.join` to ensure single `/` in between every part. This API will break if executed on a- # Windows context, as it used `\` as file separators. But we anticipate that OpenProject- # Server is not run on a Windows context.- # URI::join cannot be used, as it behaves very different for the path parts depending on trailing slashes.- File.join(uri.to_s, *parts)- endenddef api_v3_pathsdiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rbindex 775f3349fdd..4f41b260e7c 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb@@ -28,15 +28,13 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass CreateFolderCommand < Storages::Peripherals::StorageInteraction::StorageCommand- include API::V3::Utilities::PathHelper- include Errorsusing Storages::Peripherals::ServiceResultRefinementsdef initialize(storage)super()@uri = URI(storage.host).normalize- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(storage.username))+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(storage.username))@groupfolder = storage.groupfolder@username = storage.username@password = storage.password@@ -62,34 +60,30 @@ module Storages::Peripherals::StorageInteraction::Nextcloudif error_text_from_response(response) == 'The resource you tried to create already exists'ServiceResult.success(message: 'Folder already exists.')else- error(:not_allowed)+ Util.error(:not_allowed)endwhen Net::HTTPUnauthorized- error(:not_authorized)+ Util.error(:not_authorized)when Net::HTTPNotFound- error(:not_found)+ Util.error(:not_found)when Net::HTTPConflict- error(:conflict, error_text_from_response(response))+ Util.error(:conflict, error_text_from_response(response))else- error(:error)+ Util.error(:error)endend# rubocop:enable Metrics/AbcSizeprivate- def error_text_from_response(response)- Nokogiri::XML(response.body).xpath("//s:message").text- end-- def escape_whitespace(value)- value.gsub(' ', '%20')- end-def requested_folder(folder)raise ArgumentError.new("Folder can't be nil or empty string!") if folder.blank?- escape_whitespace(folder)+ Util.escape_whitespace(folder)+ end++ def error_text_from_response(response)+ Nokogiri::XML(response.body).xpath("//s:message").textendendenddiff --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 896a0ecdf0f..c984baf6927 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@@ -28,15 +28,13 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery- include API::V3::Utilities::PathHelper- include Errorsusing Storages::Peripherals::ServiceResultRefinementsdef initialize(base_uri:, token:, retry_proc:)super()@base_uri = base_uri- @uri = api_v3_paths.join_uri_path(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')+ @uri = Util.join_uri_path(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')@token = token@retry_proc = retry_procend@@ -52,8 +50,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud# rubocop:disable Metrics/AbcSizedef outbound_response(file_link)@retry_proc.call(@token) do |token|- begin- service_result = ServiceResult.success(+ service_result = begin+ ServiceResult.success(result: RestClient.post(@uri.to_s,{ fileId: file_link.origin_id },@@ -65,19 +63,19 @@ module Storages::Peripherals::StorageInteraction::Nextcloud))rescue RestClient::Unauthorized => e- service_result = error(:not_authorized, 'Outbound request not authorized!', e.response)+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)rescue RestClient::NotFound => e- service_result = error(:not_found, 'Outbound request destination not found!', e.response)+ Util.error(:not_found, 'Outbound request destination not found!', e.response)rescue RestClient::ExceptionWithResponse => e- service_result = error(:error, 'Outbound request failed!', e.response)+ Util.error(:error, 'Outbound request failed!', e.response)rescue StandardError- service_result = error(:error, 'Outbound request failed!')+ Util.error(:error, 'Outbound request failed!')endservice_result.bind do |response|# The nextcloud API returns a successful response with empty body if the authorization is missing or expiredif response.body.blank?- error(:not_authorized, 'Outbound request not authorized!')+ Util.error(:not_authorized, 'Outbound request not authorized!')elseServiceResult.success(result: response)end@@ -88,13 +86,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud# rubocop:enable Metrics/AbcSizedef download_link(token, origin_name)- api_v3_paths.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))+ Util.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))enddef direct_download_token(body:)token = parse_direct_download_token(body:)if token.blank?- return error(:error, "Received unexpected json response", body)+ return Util.error(:error, "Received unexpected json response", body)endServiceResult.success(result: token)diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rbindex 953a211eb4f..25f4a13efb9 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb@@ -28,8 +28,6 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass FileQuery < Storages::Peripherals::StorageInteraction::StorageQuery- include API::V3::Utilities::PathHelper- include Errorsusing Storages::Peripherals::ServiceResultRefinementsFILE_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/fileinfo'.freeze@@ -50,14 +48,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloudprivate- # rubocop:disable Metrics/AbcSizedef file_info(file_id)@retry_proc.call(@token) do |token|- begin- service_result = ServiceResult.success(+ service_result = begin+ ServiceResult.success(result: RestClient::Request.execute(method: :get,- url: api_v3_paths.join_uri_path(@base_uri, FILE_INFO_PATH, file_id),+ url: Util.join_uri_path(@base_uri, FILE_INFO_PATH, file_id),headers: {'Authorization' => "Bearer #{token.access_token}",'Accept' => 'application/json',@@ -66,13 +63,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud))rescue RestClient::Unauthorized => e- service_result = error(:not_authorized, 'Outbound request not authorized!', e.response)+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)rescue RestClient::NotFound => e- service_result = error(:not_found, 'Outbound request destination not found!', e.response)+ Util.error(:not_found, 'Outbound request destination not found!', e.response)rescue RestClient::ExceptionWithResponse => e- service_result = error(:error, 'Outbound request failed!', e.response)+ Util.error(:error, 'Outbound request failed!', e.response)rescue StandardError- service_result = error(:error, 'Outbound request failed!')+ Util.error(:error, 'Outbound request failed!')end# rubocop:disable Style/OpenStructUse@@ -81,12 +78,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloudendend- # rubocop:enable Metrics/AbcSize-def handle_access_control(file_info_response)data = file_info_response.ocs.dataif data.statuscode == 403- error(:forbidden)+ Util.error(:forbidden)elseServiceResult.success(result: 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 c8827fc98f9..5aefb8c1ffd 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@@ -35,16 +35,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud@uri = base_uri@token = token@retry_proc = retry_proc- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(token.origin_user_id))+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(token.origin_user_id))end- def query(parent)+ # rubocop:disable Metrics/AbcSize+ def query(folder)http = Net::HTTP.new(@uri.host, @uri.port)http.use_ssl = @uri.scheme == 'https'result = @retry_proc.call(@token) do |token|response = http.propfind(- "#{@base_path}#{requested_folder(parent)}",+ "#{@base_path}#{requested_folder(folder)}",requested_properties,{'Depth' => '1',@@ -52,22 +53,28 @@ module Storages::Peripherals::StorageInteraction::Nextcloud})- response.is_a?(Net::HTTPSuccess) ? ServiceResult.success(result: response.body) : error(response)+ case response+ when Net::HTTPSuccess+ ServiceResult.success(result: response.body)+ when Net::HTTPNotFound+ Util.error(:not_found)+ when Net::HTTPUnauthorized+ Util.error(:not_authorized)+ else+ Util.error(:error)+ endendstorage_files(result)end+ # rubocop:enable Metrics/AbcSizeprivate- def escape_whitespace(value)- value.gsub(' ', '%20')- end-def requested_folder(folder)return '' if folder.nil?- escape_whitespace(folder)+ Util.escape_whitespace(folder)end# rubocop:disable Metrics/AbcSize@@ -91,24 +98,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud# rubocop:enable Metrics/AbcSize- def error(response)- case response- when Net::HTTPNotFound- error_result(:not_found)- when Net::HTTPUnauthorized- error_result(:not_authorized)- else- error_result(:error)- end- end-- 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:, log_message:, data:)- )- end-def storage_files(response)response.map do |xml|a = Nokogiri::XML(xml)diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rbindex ac1ac9a4314..ee7444187a2 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb@@ -99,8 +99,8 @@ module Storages::Peripherals::StorageInteraction::Nextclouddef outbound_response(method:, relative_path:, payload:) # rubocop:disable Metrics/AbcSize@retry_proc.call(@token) do |token|- begin- response = ServiceResult.success(+ response = begin+ ServiceResult.success(result: RestClient::Request.execute(method:,url: @base_uri.merge(relative_path).to_s,@@ -114,13 +114,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud))rescue RestClient::Unauthorized => e- response = error(:not_authorized, 'Outbound request not authorized!', e.response)+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)rescue RestClient::NotFound => e- response = error(:not_found, 'Outbound request destination not found!', e.response)+ Util.error(:not_found, 'Outbound request destination not found!', e.response)rescue RestClient::ExceptionWithResponse => e- response = error(:error, 'Outbound request failed!', e.response)+ Util.error(:error, 'Outbound request failed!', e.response)rescue StandardError- response = error(:error, 'Outbound request failed!')+ Util.error(:error, 'Outbound request failed!')end# rubocop:disable Style/OpenStructUse@@ -129,7 +129,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud.bind do |r|# The nextcloud API returns a successful response with empty body if the authorization is missing or expiredif r.body.blank?- error(:not_authorized, 'Outbound request not authorized!')+ Util.error(:not_authorized, 'Outbound request not authorized!')elseServiceResult.success(result: r)end@@ -139,12 +139,5 @@ module Storages::Peripherals::StorageInteraction::Nextcloud# rubocop:enable Style/OpenStructUse Style/MultilineBlockChainendend-- 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:, log_message:, data:)- )- endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rbindex 798b6d8d8ac..055561936c6 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb@@ -28,8 +28,6 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass SetPermissionsCommand < Storages::Peripherals::StorageInteraction::StorageCommand- include API::V3::Utilities::PathHelper- include Errorsusing Storages::Peripherals::ServiceResultRefinementsPERMISSION_MAP = {@@ -44,7 +42,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloudsuper()@uri = URI(storage.host).normalize- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(storage.username))+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(storage.username))@groupfolder = storage.groupfolder@group = storage.group@username = storage.username@@ -67,24 +65,20 @@ module Storages::Peripherals::StorageInteraction::Nextcloudwhen Net::HTTPSuccessServiceResult.successwhen Net::HTTPNotFound- error(:not_found)+ Util.error(:not_found)when Net::HTTPUnauthorized- error(:not_authorized)+ Util.error(:not_authorized)else- error(:error)+ Util.error(:error)endendprivate- def escape_whitespace(value)- value.gsub(' ', '%20')- end-def requested_folder(folder)raise ArgumentError.new("Folder can't be nil or empty string!") if folder.blank?- escape_whitespace(folder)+ Util.escape_whitespace(folder)enddef converted_permissions(permissions:)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.rbindex f5e8522bb8c..5eea1c7bd73 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,8 +28,6 @@module Storages::Peripherals::StorageInteraction::Nextcloudclass UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery- include API::V3::Utilities::PathHelper- include Errorsusing Storages::Peripherals::ServiceResultRefinements # use '>>' (bind) operator for ServiceResultURI_TOKEN_REQUEST = 'index.php/apps/integration_openproject/direct-upload-token'.freeze@@ -53,7 +51,7 @@ module Storages::Peripherals::StorageInteraction::Nextclouddef validated(data)if data.nil? || data['parent'].nil?- error(:error, 'Data is invalid', data)+ Util.error(:error, 'Data is invalid', data)elseServiceResult.success(result: Struct.new(:parent).new(data['parent'])@@ -70,18 +68,18 @@ module Storages::Peripherals::StorageInteraction::Nextcloudenddef build_upload_link(response)- destination = URI.parse(api_v3_paths.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))+ destination = URI.parse(Util.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))ServiceResult.success(result: Storages::UploadLink.new(destination))end# rubocop:disable Metrics/AbcSizedef outbound_response(method:, relative_path:, payload:)@retry_proc.call(@token) do |token|- begin- response = ServiceResult.success(+ response = begin+ ServiceResult.success(result: RestClient::Request.execute(method:,- url: api_v3_paths.join_uri_path(@base_uri, relative_path),+ url: Util.join_uri_path(@base_uri, relative_path),payload: payload.to_json,headers: {'Authorization' => "Bearer #{token.access_token}",@@ -91,13 +89,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud))rescue RestClient::Unauthorized => e- response = error(:not_authorized, 'Outbound request not authorized!', e.response)+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)rescue RestClient::NotFound => e- response = error(:not_found, 'Outbound request destination not found!', e.response)+ Util.error(:not_found, 'Outbound request destination not found!', e.response)rescue RestClient::ExceptionWithResponse => e- response = error(:error, 'Outbound request failed!', e.response)+ Util.error(:error, 'Outbound request failed!', e.response)rescue StandardError- response = error(:error, 'Outbound request failed!')+ Util.error(:error, 'Outbound request failed!')end# rubocop:disable Style/OpenStructUse@@ -106,7 +104,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud.bind do |r|# The nextcloud API returns a successful response with empty body if the authorization is missing or expiredif r.body.blank?- error(:not_authorized, 'Outbound request not authorized!')+ Util.error(:not_authorized, 'Outbound request not authorized!')elseServiceResult.success(result: r)end@@ -116,7 +114,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud# rubocop:enable Style/OpenStructUse Style/MultilineBlockChainendend-# rubocop:enable Metrics/AbcSizeendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rbsimilarity index 71%rename from modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rbrename to modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rbindex 8ed761faae3..dfd3c2a2fe9 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb@@ -26,13 +26,25 @@# See COPYRIGHT and LICENSE files for more details.#++-module Storages::Peripherals::StorageInteraction::Nextcloud- module Errors+module Storages::Peripherals::StorageInteraction::Nextcloud::Util+ class << self+ def escape_whitespace(value)+ value.gsub(' ', '%20')+ end+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:, log_message:, data:))end++ def join_uri_path(uri, *parts)+ # We use `File.join` to ensure single `/` in between every part. This API will break if executed on a+ # Windows context, as it used `\` as file separators. But we anticipate that OpenProject+ # Server is not run on a Windows context.+ # URI::join cannot be used, as it behaves very different for the path parts depending on trailing slashes.+ File.join(uri.to_s, *parts)+ endendend