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
[#50684] Migrate to httpx http ruby client
Pavel Balashoue3772ff9c595
diff --git a/Gemfile b/Gemfileindex 4353ca3b469..8adb3d89c6e 100644--- a/Gemfile+++ b/Gemfile@@ -318,6 +318,7 @@ group :development, :test dogem 'pry-byebug', '~> 3.10.0', platforms: [:mri]gem 'pry-rails', '~> 0.3.6'gem 'pry-rescue', '~> 1.5.2'+ gem 'pry-doc'# ruby lintinggem 'rubocop', require: falsediff --git a/Gemfile.lock b/Gemfile.lockindex 879a1287dda..50a58fb34a0 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -776,6 +776,9 @@ GEMpry-byebug (3.10.1)byebug (~> 11.0)pry (>= 0.13, < 0.15)+ pry-doc (1.5.0)+ pry (~> 0.11)+ yard (~> 0.9.11)pry-rails (0.3.9)pry (>= 0.10.4)pry-rescue (1.5.2)@@ -1210,6 +1213,7 @@ DEPENDENCIESplaintext (~> 0.3.2)prawn (~> 2.4)pry-byebug (~> 3.10.0)+ pry-docpry-rails (~> 0.3.6)pry-rescue (~> 1.5.2)puffing-billy (~> 4.0.0)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.rbindex 150af97837d..a446748cdf3 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@@ -32,9 +32,6 @@ module Storagesprivatedef make_file_request(drive_item_id, token, select_url_query)- # response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|- # http.get(uri_path_for(drive_item_id) + select_url_query, { 'Authorization' => "Bearer #{token.access_token}" })- # endresponse = HTTPX.get(UTIL.join_uri_path(@uri, uri_path_for(drive_item_id) + select_url_query),headers: { 'Authorization' => "Bearer #{token.access_token}" }diff --git a/modules/storages/app/validator/nextcloud_compatible_host_validator.rb b/modules/storages/app/validator/nextcloud_compatible_host_validator.rbindex 206045b4ae8..8c53bc2c03c 100644--- a/modules/storages/app/validator/nextcloud_compatible_host_validator.rb+++ b/modules/storages/app/validator/nextcloud_compatible_host_validator.rb@@ -29,8 +29,11 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatorMINIMAL_NEXTCLOUD_VERSION = 22AUTHORIZATION_HEADER = "Bearer TESTBEARERTOKEN".freeze+ HTTPX_TIMEOUT_SETTINGS = { timeout: { connect_timeout: 5, read_timeout: 3 } }++def validate_each(contract, attribute, value)- return unless contract.model.changed_attributes.include?(attribute)+ return if contract.model.changed_attributes.exclude?(attribute)validate_capabilities(contract, attribute, value)validate_setup_completeness(contract, attribute, value) if contract.errors.empty?@@ -39,7 +42,16 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatorprivatedef validate_capabilities(contract, attribute, value)- response = request_capabilities(value)+ uri = URI.parse(File.join(value, '/ocs/v2.php/cloud/capabilities'))++ response =+ begin+ HTTPX+ .with(HTTPX_TIMEOUT_SETTINGS)+ .get(uri, headers: { "Ocs-Apirequest" => "true", "Accept" => "application/json" })+ rescue StandardError => e+ e+ enderror_type = check_capabilities_response(response)if error_type@@ -48,14 +60,31 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatorendend+ def check_capabilities_response(response)+ return :cannot_be_connected_to if response.is_a? StandardError+ return :cannot_be_connected_to unless response.status.in? 200..299+ return :not_nextcloud_server unless read_version(response)+ return :minimal_nextcloud_version_unmet unless major_version_sufficient?(response)++ nil+ end+# Apparently some Nextcloud installations do not use mod_rewrite. Then requesting its app root (the storage host name)# the response is a redirect to a URI containing 'index.php' in its path. If that is the case that installation# is not compatible with our integration. It is missing support for Bearer token based authorization. Apparently# Apache strips that part of the request header by default.# https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/oauth2.htmldef validate_setup_completeness(contract, attribute, value)- response = request_config_check(value)- error_type = check_config_check_response(response)+ uri = URI.parse(File.join(value, 'index.php/apps/integration_openproject/check-config'))+ response =+ begin+ HTTPX+ .with(HTTPX_TIMEOUT_SETTINGS)+ .get(uri, headers: { "Authorization" => AUTHORIZATION_HEADER })+ rescue StandardError => e+ e+ end+ error_type = check_setup_completeness_response(response)if error_typecontract.errors.add(attribute, error_type)@@ -63,33 +92,24 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatorendend- def check_capabilities_response(response)- return :cannot_be_connected_to if response.is_a? StandardError- return :cannot_be_connected_to unless response.is_a? Net::HTTPSuccess- return :not_nextcloud_server unless json_response_with_version?(response)- return :minimal_nextcloud_version_unmet unless major_version_sufficient?(response)-- nil- end-- def check_config_check_response(response)+ def check_setup_completeness_response(response)return :cannot_be_connected_to if response.is_a? StandardError- return :op_application_not_installed if response.is_a? Net::HTTPRedirection- return :cannot_be_connected_to unless response.is_a? Net::HTTPSuccess+ return :op_application_not_installed if response.status.in? 300..399+ return :cannot_be_connected_to unless response.status.in? 200..299return :authorization_header_missing if read_authorization_header(response) != AUTHORIZATION_HEADERnilenddef message(host, response_or_exception, error_type)- if response_or_exception.is_a?(Net::HTTPResponse)+ if response_or_exception.is_a?(HTTPX::Response)response = response_or_exceptionelseexception = response_or_exceptionendmessage = "Nextcloud server invalid host=#{host.inspect} error_type=#{error_type}"- message << " http_status=#{response.code}" if response+ message << " http_status=#{response.status}" if responsecase error_typewhen :cannot_be_connected_to@@ -113,56 +133,15 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatortrueend- def make_request(request)- uri = request.uri-- req_options = {- max_retries: 0,- open_timeout: 5, # seconds- read_timeout: 3, # seconds- use_ssl: uri.scheme == "https"- }-- begin- Net::HTTP.start(uri.host, uri.port, req_options) do |http|- http.request(request)- end- rescue StandardError => e- e- end- end-- def request_capabilities(host)- uri = URI.parse(File.join(host, '/ocs/v2.php/cloud/capabilities'))- request = Net::HTTP::Get.new(uri)- request["Ocs-Apirequest"] = "true"- request["Accept"] = "application/json"-- make_request(request)- end-- def request_config_check(host)- uri = URI.parse(File.join(host, 'index.php/apps/integration_openproject/check-config'))- request = Net::HTTP::Get.new(uri)- request["Authorization"] = AUTHORIZATION_HEADER-- make_request(request)- end-- def json_response_with_version?(response)- response['content-type'].split(';').first.strip.downcase == 'application/json' \- && read_version(response)- rescue JSON::ParserError- false- end-def read_version(response)- JSON.parse(response.body).dig('ocs', 'data', 'version', 'major')+ response.json.dig('ocs', 'data', 'version', 'major')+ rescue HTTPX::Error, MultiJson::ParseError+ falseenddef read_authorization_header(response)- JSON.parse(response.body)['authorization_header']- rescue JSON::ParserError+ response.json['authorization_header']+ rescue HTTPX::Error, MultiJson::ParseErrornilendenddiff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rbindex e6c9345f43c..04c6673f9cf 100644--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb@@ -177,7 +177,7 @@ RSpec.shared_examples_for 'nextcloud storage contract', :storage_server_helpers,context 'when connection fails' dobefore do- allow(Net::HTTP).to receive(:start).and_raise(SocketError, 'Failed to open TCP connection (SIMULATED)')+ allow_any_instance_of(HTTPX::Session).to receive(:get).and_raise(HTTPX::TimeoutError, 'SIMULATED Timeout')endinclude_examples 'contract is invalid', host: :cannot_be_connected_todiff --git a/modules/storages/spec/models/nextcloud_storage_spec.rb b/modules/storages/spec/models/nextcloud_storage_spec.rbdeleted file mode 100644index e69de29bb2d..00000000000