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

[#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.rb
index 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/MissingSuper
def initialize(storage)
@storage = storage
- @uri = storage.uri
raise(ArgumentError, "Storage must have configured OAuth client credentials") if storage.oauth_client.blank?
@@ -54,7 +53,7 @@ module Storages
def 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"]
end
diff --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.rb
index 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 Storages
def 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"))
end
end
diff --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.rb
index 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 Storages
def 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 })
)
end
end
@@ -88,9 +88,11 @@ module Storages
end
end
- 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"
end
end
end
diff --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.rb
index 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 Storages
def 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))
end
end
@@ -55,10 +55,12 @@ module Storages
Util.json_content_type
end
- 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")
+ end
end
def handle_response(response)
@@ -88,8 +90,6 @@ module Storages
"@microsoft.graph.conflictBehavior" => "fail"
}.to_json
end
-
- def base_uri = "#{@storage.uri}v1.0/drives/#{@storage.drive_id}"
end
end
end
diff --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.rb
index 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 Storages
def 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)
)
end
end
diff --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.rb
index 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 Storages
end
Authentication[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))
end
end
@@ -78,8 +78,8 @@ module Storages
end
end
- 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")
end
end
end
diff --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.rb
index 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 Storages
owner_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.rb
index 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 Storages
def 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 Storages
end
def 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)
end
end
@@ -146,20 +144,18 @@ module Storages
permissions: %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)}"
end
end
end
diff --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.rb
index 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 Storages
def initialize(storage)
@storage = storage
- @uri = storage.uri
end
def 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)
end
private
- 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))
end
def handle_responses(response)
@@ -78,9 +77,9 @@ module Storages
end
def 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"
end
end
end
diff --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.rb
index 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 Storages
private
def 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}")
end
def handle_response(response)
@@ -75,9 +76,9 @@ module Storages
def 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}"
end
end
end
diff --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.rb
index 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 Storages
private
def request_drive(http)
- handle_responses http.get(Util.join_uri_path(@storage.uri, drive_uri_path))
+ handle_responses http.get(request_url)
end
def handle_responses(response)
@@ -72,8 +72,8 @@ module Storages
end
end
- def drive_uri_path
- "/v1.0/drives/#{@storage.drive_id}?$select=webUrl"
+ def request_url
+ "#{Util.drive_base_uri(@storage)}?$select=webUrl"
end
def web_url
diff --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.rb
index 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 Storages
validate_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)
end
end
@@ -62,10 +63,6 @@ module Storages
end
end
- def uri_path(source)
- "#{@storage.uri}v1.0/drives/#{@storage.drive_id}/items/#{source}"
- end
-
# rubocop:disable Metrics/AbcSize
def handle_response(response)
case response
diff --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.rb
index 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/AbcSize
def handle_response(response)
case response
in { status: 200 }
@@ -154,6 +155,8 @@ module Storages
end
end
+ # rubocop:enable Metrics/AbcSize
+
def permission_path(item_id, permission_id)
"#{permissions_path(item_id)}/#{permission_id}"
end
@@ -167,7 +170,7 @@ module Storages
end
def item_path(item_id)
- "/v1.0/drives/#{@storage.drive_id}/items/#{item_id}"
+ UrlBuilder.url(Util.drive_base_uri(@storage), "/items", item_id)
end
def 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.rb
index 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 Storages
return 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))
end
end
@@ -71,6 +67,7 @@ module Storages
{ item: { "@microsoft.graph.conflictBehavior" => "rename", name: filename } }
end
+ # rubocop:disable Metrics/AbcSize
def handle_response(response)
case response
in { status: 200..299 }
@@ -91,8 +88,13 @@ module Storages
end
end
- 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"
end
end
end
diff --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.rb
index 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 Storages
using ServiceResultRefinements
class << 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 Storages
StorageError.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))
end
def json_content_type
@@ -136,7 +126,7 @@ module Storages
last_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]
)
end
diff --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.yml
index 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,parentReference
body:
encoding: US-ASCII
string: ''