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