From 32b73b67f6676b24f5bd0eb58b332c4312934603 Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Tue, 3 Sep 2024 16:25:38 +0200 Subject: [PATCH] Raise an error at runtime when Faraday Multipart is not installed: - In 7bc6dd50d9d82cd1ce1e025b8aaf34f9823846a2, a warning message was added to let users know that faraday-multipart is required when using Faraday 2.0. This warning is not very helpful, and may lead to projects adding the gem to their dependencies even if they don't need it, as the faraday-multipart middleware is only used for uploading license for GitHub entrepise. This patch will avoid printing the warning unnecessarily but also display a better error message when ultimately the call to `conn.request :multipart` fails. Fix #1701 --- lib/octokit/default.rb | 5 ----- .../management_console.rb | 9 ++++++++- lib/octokit/manage_ghes_client/manage_ghes.rb | 9 ++++++++- .../management_console_spec.rb | 9 +++++++++ spec/octokit/ghes_manage_client/ghes_manage_spec.rb | 9 +++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/octokit/default.rb b/lib/octokit/default.rb index 35eba066a..ca2e2fb73 100644 --- a/lib/octokit/default.rb +++ b/lib/octokit/default.rb @@ -12,11 +12,6 @@ rescue LoadError Octokit::Warnable.octokit_warn 'To use retry middleware with Faraday v2.0+, install `faraday-retry` gem' end - begin - require 'faraday/multipart' - rescue LoadError - Octokit::Warnable.octokit_warn 'To use multipart middleware with Faraday v2.0+, install `faraday-multipart` gem; note: this is used by the ManageGHES client for uploading licenses' - end end module Octokit diff --git a/lib/octokit/enterprise_management_console_client/management_console.rb b/lib/octokit/enterprise_management_console_client/management_console.rb index 4355ebfbb..587187063 100644 --- a/lib/octokit/enterprise_management_console_client/management_console.rb +++ b/lib/octokit/enterprise_management_console_client/management_console.rb @@ -171,7 +171,14 @@ def password_hash def faraday_configuration @faraday_configuration ||= Faraday.new(url: @management_console_endpoint) do |http| http.headers[:user_agent] = user_agent - http.request :multipart + begin + http.request :multipart + rescue Faraday::Error + raise Faraday::Error, <<~ERROR + The `faraday-multipart` gem is required to upload a license. + Please add `gem "faraday-multipart"` to your Gemfile. + ERROR + end http.request :url_encoded # Disabling SSL is essential for certain self-hosted Enterprise instances diff --git a/lib/octokit/manage_ghes_client/manage_ghes.rb b/lib/octokit/manage_ghes_client/manage_ghes.rb index 806614a8e..53ca79665 100644 --- a/lib/octokit/manage_ghes_client/manage_ghes.rb +++ b/lib/octokit/manage_ghes_client/manage_ghes.rb @@ -36,7 +36,14 @@ def set_maintenance_mode(enabled, options = {}) # @return [nil] def upload_license(license) conn = authenticated_client - conn.request :multipart + begin + conn.request :multipart + rescue Faraday::Error + raise Faraday::Error, <<~ERROR + The `faraday-multipart` gem is required to upload a license. + Please add `gem "faraday-multipart"` to your Gemfile. + ERROR + end params = {} params[:license] = Faraday::FilePart.new(license, 'binary') params[:password] = @manage_ghes_password diff --git a/spec/octokit/enterprise_management_console_client/management_console_spec.rb b/spec/octokit/enterprise_management_console_client/management_console_spec.rb index 1cc802af7..a2049a1dc 100644 --- a/spec/octokit/enterprise_management_console_client/management_console_spec.rb +++ b/spec/octokit/enterprise_management_console_client/management_console_spec.rb @@ -14,6 +14,15 @@ expect(@enterprise_management_console_client.last_response.status).to eq(202) assert_requested :post, github_management_console_url('setup/api/start') end + + it 'raises an error if the faraday-multipart gem is not installed' do + middleware_key = :multipart + middleware = Faraday::Request.unregister_middleware(middleware_key) + + expect { @enterprise_management_console_client.upload_license(@license) }.to raise_error Faraday::Error + ensure + Faraday::Request.register_middleware(middleware_key => middleware) if middleware + end end # .upload_license describe '.start_configuration', :vcr do diff --git a/spec/octokit/ghes_manage_client/ghes_manage_spec.rb b/spec/octokit/ghes_manage_client/ghes_manage_spec.rb index 4cbf3fac0..e63079904 100644 --- a/spec/octokit/ghes_manage_client/ghes_manage_spec.rb +++ b/spec/octokit/ghes_manage_client/ghes_manage_spec.rb @@ -38,6 +38,15 @@ expect(@manage_ghes.last_response.status).to eq(202) assert_requested :post, github_manage_ghes_url('/manage/v1/config/init') end + + it 'raises an error if the faraday-multipart gem is not installed' do + middleware_key = :multipart + middleware = Faraday::Request.unregister_middleware(middleware_key) + + expect { @manage_ghes.upload_license(@license) }.to raise_error Faraday::Error + ensure + Faraday::Request.register_middleware(middleware_key => middleware) if middleware + end end # .upload_license describe '.start_configuration', :vcr do