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
[#56334] introduced url builder for all one drive commands/queries
Eric Schubert4dea8654e430
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rbindex 434525a10d5..4312a625416 100644--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb@@ -39,7 +39,6 @@ module Storages# rubocop:disable Lint/MissingSuperdef initialize(storage)@storage = storage- @uri = storage.uriraise(ArgumentError, "Storage must have configured OAuth client credentials") if storage.oauth_client.blank?@@ -54,7 +53,7 @@ module Storagesdef extract_origin_user_id(rack_access_token)OpenProject.httpx.get(- Util.join_uri_path(@uri, "/v1.0/me"),+ UrlBuilder.url(@storage.uri, "/v1.0/me"),headers: { "Authorization" => "Bearer #{rack_access_token.access_token}", "Accept" => "application/json" }).raise_for_status.json["id"]enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rbindex 9fe43ff52b4..3d3d30460bf 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb@@ -43,7 +43,7 @@ module Storagesdef call(auth_strategy:)Authentication[auth_strategy].call(storage: @storage) do |http|- handle_response http.get(Util.join_uri_path(@storage.uri, "/v1.0/me"))+ handle_response http.get(UrlBuilder.url(@storage.uri, "/v1.0/me"))endenddiff --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 56e418d9161..04408181ceb 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@@ -53,7 +53,7 @@ module Storagesdef 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 })+ httpx.post(url_for(source_location) + query, json: { name: destination_name }))endend@@ -88,9 +88,11 @@ module Storagesendend- def copy_path_for(source_location)- "#{@storage.uri}v1.0/drives/#{@storage.drive_id}/items/#{source_location}/[email protected]=fail"+ def url_for(source_location)+ UrlBuilder.url(Util.drive_base_uri(@storage), "/items", source_location, "/copy")end++ def query = "[email protected]=fail"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rbindex e7c5db32354..0b7ffe8ee79 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb@@ -45,7 +45,7 @@ module Storagesdef call(auth_strategy:, folder_name:, parent_location:)Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|- handle_response http.post(uri_for(parent_location), body: payload(folder_name))+ handle_response http.post(url_for(parent_location), body: payload(folder_name))endend@@ -55,10 +55,12 @@ module StoragesUtil.json_content_typeend- def uri_for(parent_location)- return "#{base_uri}/root/children" if parent_location.root?-- "#{base_uri}/items/#{parent_location}/children"+ def url_for(parent_location)+ if parent_location.root?+ UrlBuilder.url(Util.drive_base_uri(@storage), "/root/children")+ else+ UrlBuilder.url(Util.drive_base_uri(@storage), "/items", parent_location.path, "/children")+ endenddef handle_response(response)@@ -88,8 +90,6 @@ module Storages"@microsoft.graph.conflictBehavior" => "fail"}.to_jsonend-- def base_uri = "#{@storage.uri}v1.0/drives/#{@storage.drive_id}"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rbindex 166eaab54bf..ced44cffa6b 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/delete_folder_command.rb@@ -44,7 +44,7 @@ module Storagesdef call(auth_strategy:, location:)Authentication[auth_strategy].call(storage: @storage) do |http|handle_response http.delete(- Util.join_uri_path(@storage.uri, "/v1.0/drives/#{@storage.drive_id}/items/#{location}")+ UrlBuilder.url(Util.drive_base_uri(@storage), "items", location))endenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rbindex 000ac6c43e1..f5cc5cd536e 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/download_link_query.rb@@ -49,7 +49,7 @@ module StoragesendAuthentication[auth_strategy].call(storage: @storage) do |http|- handle_errors http.get(Util.join_uri_path(@storage.uri, uri_path_for(file_link.origin_id)))+ handle_errors http.get(url_for(file_link.origin_id))endend@@ -78,8 +78,8 @@ module Storagesendend- def uri_path_for(file_id)- "/v1.0/drives/#{@storage.drive_id}/items/#{file_id}/content"+ def url_for(file_id)+ UrlBuilder.url(Util.drive_base_uri(@storage), "items", file_id, "content")endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rbindex 81d8d60e104..3fc630c834a 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb@@ -100,7 +100,7 @@ module Storagesowner_name: json.dig(:createdBy, :user, :displayName),owner_id: json.dig(:createdBy, :user, :id),permissions: nil,- location: Util.escape_path(Util.extract_location(json[:parentReference], json[:name])),+ location: UrlBuilder.path(Util.extract_location(json[:parentReference], json[:name])),last_modified_at: Time.zone.parse(json.dig(:fileSystemInfo, :lastModifiedDateTime)),created_at: Time.zone.parse(json.dig(:fileSystemInfo, :createdDateTime)),last_modified_by_name: json.dig(:lastModifiedBy, :user, :displayName),diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rbindex 51a7896a2db..e92fc0cba31 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb@@ -45,8 +45,7 @@ module Storagesdef call(auth_strategy:, folder:)Authentication[auth_strategy].call(storage: @storage) do |http|- call = http.get(Util.join_uri_path(@storage.uri, children_uri_path_for(folder) + FIELDS))- response = handle_response(call, :value)+ response = handle_response(http.get(children_url_for(folder) + FIELDS), :value)if response.result.empty?empty_response(http, folder)@@ -91,8 +90,7 @@ module Storagesenddef empty_response(http, folder)- response = http.get(Util.join_uri_path(@storage.uri, location_uri_path_for(folder) + FIELDS))- handle_response(response, :id).map do |parent_location_id|+ handle_response(http.get(location_url_for(folder) + FIELDS), :id).map do |parent_location_id|empty_storage_files(folder.path, parent_location_id)endend@@ -146,20 +144,18 @@ module Storagespermissions: %i[readable writeable])end- def children_uri_path_for(folder)- return "/v1.0/drives/#{@storage.drive_id}/root/children" if folder.root?+ def children_url_for(folder)+ base_uri = Util.drive_base_uri(@storage)+ return UrlBuilder.url(base_uri, "/root/children") if folder.root?- "/v1.0/drives/#{@storage.drive_id}/root:#{encode_path(folder.path)}:/children"+ "#{UrlBuilder.url(base_uri, '/root')}:#{UrlBuilder.path(folder.path)}:/children"end- def location_uri_path_for(folder)- return "/v1.0/drives/#{@storage.drive_id}/root" if folder.root?+ def location_url_for(folder)+ base_uri = UrlBuilder.url(Util.drive_base_uri(@storage), "/root")+ return base_uri if folder.root?- "/v1.0/drives/#{@storage.drive_id}/root:#{encode_path(folder.path)}"- end-- def encode_path(path)- path.split("/").map { |fragment| URI.encode_uri_component(fragment) }.join("/")+ "#{base_uri}:#{UrlBuilder.path(folder.path)}"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rbindex d1bf993579a..3db86b888c1 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb@@ -38,24 +38,23 @@ module Storagesdef initialize(storage)@storage = storage- @uri = storage.urienddef call(http:, folder:, fields: [])- select_url_query = if fields.empty?- ""- else- "?$select=#{fields.join(',')}"- end+ query = if fields.empty?+ ""+ else+ "?$select=#{fields.join(',')}"+ end- make_children_request(folder, http, select_url_query)+ make_children_request(folder, http, query)endprivate- def make_children_request(folder, http, select_url_query)- response = http.get(Util.join_uri_path(@uri, uri_path_for(folder) + select_url_query))- handle_responses(response)+ def make_children_request(folder, http, query)+ url = UrlBuilder.url(Util.drive_base_uri(@storage), uri_path_for(folder))+ handle_responses(http.get(url + query))enddef handle_responses(response)@@ -78,9 +77,9 @@ module Storagesenddef uri_path_for(folder)- return "/v1.0/drives/#{@storage.drive_id}/root/children" if folder.root?+ return "/root/children" if folder.root?- "/v1.0/drives/#{@storage.drive_id}/items/#{folder.path}/children"+ "/items/#{folder.path}/children"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rbindex 1c7ae445296..23fa67fb26e 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb@@ -51,7 +51,8 @@ module Storagesprivatedef make_file_request(drive_item_id, http, select_url_query)- handle_response http.get("#{@storage.uri}#{uri_path_for(drive_item_id)}#{select_url_query}")+ url = UrlBuilder.url(Util.drive_base_uri(@storage), uri_path_for(drive_item_id))+ handle_response http.get("#{url}#{select_url_query}")enddef handle_response(response)@@ -75,9 +76,9 @@ module Storagesdef uri_path_for(file_id)if file_id == "/"- "v1.0/drives/#{@storage.drive_id}/root"+ "/root"else- "v1.0/drives/#{@storage.drive_id}/items/#{file_id}"+ "/items/#{file_id}"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rbindex 33ea6da2d93..e5cade0f1f4 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_storage_query.rb@@ -50,7 +50,7 @@ module Storagesprivatedef request_drive(http)- handle_responses http.get(Util.join_uri_path(@storage.uri, drive_uri_path))+ handle_responses http.get(request_url)enddef handle_responses(response)@@ -72,8 +72,8 @@ module Storagesendend- def drive_uri_path- "/v1.0/drives/#{@storage.drive_id}?$select=webUrl"+ def request_url+ "#{Util.drive_base_uri(@storage)}?$select=webUrl"enddef web_urldiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rbindex e236ec5c904..05fcc91817e 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/rename_file_command.rb@@ -45,7 +45,8 @@ module Storagesvalidate_input_data(file_id:, name:).on_failure { |failure| return failure }Authentication[auth_strategy].call(storage: @storage, http_options: Util.json_content_type) do |http|- handle_response http.patch(uri_path(file_id), body: { name: }.to_json)+ handle_response http.patch(UrlBuilder.url(Util.drive_base_uri(@storage), "items", file_id),+ body: { name: }.to_json)endend@@ -62,10 +63,6 @@ module Storagesendend- def uri_path(source)- "#{@storage.uri}v1.0/drives/#{@storage.drive_id}/items/#{source}"- end-# rubocop:disable Metrics/AbcSizedef handle_response(response)case responsediff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rbindex 7fda3dd4992..c9e71925497 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb@@ -130,6 +130,7 @@ module Storages{ read: read_permissions, write: write_permissions }end+ # rubocop:disable Metrics/AbcSizedef handle_response(response)case responsein { status: 200 }@@ -154,6 +155,8 @@ module Storagesendend+ # rubocop:enable Metrics/AbcSize+def permission_path(item_id, permission_id)"#{permissions_path(item_id)}/#{permission_id}"end@@ -167,7 +170,7 @@ module Storagesenddef item_path(item_id)- "/v1.0/drives/#{@storage.drive_id}/items/#{item_id}"+ UrlBuilder.url(Util.drive_base_uri(@storage), "/items", item_id)enddef log_error(error)diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rbindex 6187c86413a..30f5ff89c47 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/upload_link_query.rb@@ -45,12 +45,8 @@ module Storagesreturn upload_data_failure if invalid?(upload_data:)Authentication[auth_strategy].call(storage: @storage) do |http|- response = http.post(- Util.join_uri_path(@storage.uri, uri_path_for(upload_data.folder_id, upload_data.file_name)),- json: payload(upload_data.file_name)- )-- handle_response(response)+ handle_response http.post(url(upload_data.folder_id, upload_data.file_name),+ json: payload(upload_data.file_name))endend@@ -71,6 +67,7 @@ module Storages{ item: { "@microsoft.graph.conflictBehavior" => "rename", name: filename } }end+ # rubocop:disable Metrics/AbcSizedef handle_response(response)case responsein { status: 200..299 }@@ -91,8 +88,13 @@ module Storagesendend- def uri_path_for(folder, filename)- "/v1.0/drives/#{@storage.drive_id}/items/#{folder}:/#{URI.encode_uri_component(filename)}:/createUploadSession"+ # rubocop:enable Metrics/AbcSize++ def url(folder, filename)+ base = UrlBuilder.url(Util.drive_base_uri(@storage), "/items/", folder)+ file_path = UrlBuilder.path(filename)++ "#{base}:#{file_path}:/createUploadSession"endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rbindex 059d4891cc5..bc9272aab7f 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/util.rb@@ -36,12 +36,6 @@ module Storagesusing ServiceResultRefinementsclass << self- def escape_path(path)- escaped_path = path.split("/").map { |i| CGI.escapeURIComponent(i) }.join("/")- escaped_path << "/" if path[-1] == "/"- escaped_path- end-def mime_type(json)json.dig(:file, :mimeType) || (json.key?(:folder) ? "application/x-op-directory" : nil)end@@ -77,12 +71,8 @@ module StoragesStorageError.new(code:, data:, log_message:)end- def join_uri_path(uri, *)- # 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, *)+ def drive_base_uri(storage)+ URI.parse(UrlBuilder.url(storage.uri, "/v1.0/drives", storage.drive_id))enddef json_content_type@@ -136,7 +126,7 @@ module Storageslast_modified_at: Time.zone.parse(json.dig(:fileSystemInfo, :lastModifiedDateTime)),created_by_name: json.dig(:createdBy, :user, :displayName),last_modified_by_name: json.dig(:lastModifiedBy, :user, :displayName),- location: Util.escape_path(Util.extract_location(json[:parentReference], json[:name])),+ location: UrlBuilder.path(Util.extract_location(json[:parentReference], json[:name])),permissions: %i[readable writeable])enddiff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_path_to_id_map_query_invalid_parent.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_path_to_id_map_query_invalid_parent.ymlindex 9845cf54b2b..8e5446d7e0f 100644--- a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_path_to_id_map_query_invalid_parent.yml+++ b/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_path_to_id_map_query_invalid_parent.yml@@ -57,7 +57,7 @@ http_interactions:recorded_at: Wed, 21 Feb 2024 15:41:34 GMT- request:method: get- uri: https://graph.microsoft.com/v1.0/drives/b!dmVLG22QlE2PSW0AqVB7UOhZ8n7tjkVGkgqLNnuw2OBb-brzKzZAR4DYT1k9KPXs/items//I/just/made/that/up?$select=id,name,parentReference+ uri: https://graph.microsoft.com/v1.0/drives/b!dmVLG22QlE2PSW0AqVB7UOhZ8n7tjkVGkgqLNnuw2OBb-brzKzZAR4DYT1k9KPXs/items/I/just/made/that/up?$select=id,name,parentReferencebody:encoding: US-ASCIIstring: ''