Skip to content

Commit

Permalink
[Fix rubocop#9827] Add Token auth support to download raw config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Fung committed Jun 1, 2021
1 parent 7551234 commit afd2ab6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
@@ -0,0 +1 @@
* [#9827](https://github.com/rubocop/rubocop/issues/9827): Add Token auth support to download raw yml config from private repo. ([@AirWick219][])
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/configuration.adoc
Expand Up @@ -138,6 +138,16 @@ inherit_from:
- ../.rubocop.yml
----

You can inherit from a repo with a
link:https://docs.github.com/en/developers/apps/about-apps#personal-access-token[GitHub personal access token]
that is authorized to access the repo.

[source,yaml]
----
inherit_from:
- http://<personal_access_token>@raw.github.com/example/rubocop.yml
----

=== Inheriting configuration from a dependency gem

The optional `inherit_gem` directive is used to include configuration from
Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/remote_config.rb
Expand Up @@ -55,6 +55,7 @@ def request(uri = @uri, limit = 10, &block)
def generate_request(uri)
request = Net::HTTP::Get.new(uri.request_uri)

request['Authorization'] = "token #{uri.user}" if uri.user
request['If-Modified-Since'] = File.stat(cache_path).mtime.rfc2822 if cache_path_exists?

yield request
Expand All @@ -70,7 +71,7 @@ def handle_response(response, limit, &block)
begin
response.error!
rescue StandardError => e
message = "#{e.message} while downloading remote config file #{uri}"
message = "#{e.message} while downloading remote config file #{cloned_url}"
raise e, message
end
end
Expand All @@ -94,9 +95,15 @@ def cache_path_expired?
end

def cache_name_from_uri
uri = @uri.clone
uri = cloned_url
uri.query = nil
uri.to_s.gsub!(/[^0-9A-Za-z]/, '-')
end

def cloned_url
uri = @uri.clone
uri.user = nil if uri.user
uri
end
end
end
56 changes: 56 additions & 0 deletions spec/rubocop/remote_config_spec.rb
Expand Up @@ -39,6 +39,62 @@
assert_requested :get, remote_config_url
end

context 'when remote URL is configured with token auth' do
let(:token) { 'personal_access_token' }
let(:remote_config_url) { "http://#{token}@example.com/rubocop.yml" }
let(:stripped_remote_config_url) { 'http://example.com/rubocop.yml' }

before do
stub_request(:get, stripped_remote_config_url)
.to_return(headers: { 'Authorization' => "token #{token}" })
stub_request(:get, stripped_remote_config_url)
.to_return(status: 200, body: "Style/Encoding:\n Enabled: true")
end

it 'downloads the file if the file does not exist' do
expect(remote_config).to eq(cached_file_path)
expect(File.exist?(cached_file_path)).to be_truthy
end

it 'does not download the file if cache lifetime has not been reached' do
FileUtils.touch cached_file_path, mtime: Time.now - ((60 * 60) * 20)

expect(remote_config).to eq(cached_file_path)
assert_not_requested :get, remote_config_url
end

it 'downloads the file if cache lifetime has been reached' do
FileUtils.touch cached_file_path, mtime: Time.now - ((60 * 60) * 30)

expect(remote_config).to eq(cached_file_path)
assert_requested :get, stripped_remote_config_url
end

context 'when the remote URL responds with 404' do
before do
stub_request(:get, stripped_remote_config_url).to_return(status: 404)
end

it 'raises error' do
expect do
remote_config
end.to raise_error(Net::HTTPServerException,
'404 "" while downloading remote config file http://example.com/rubocop.yml')
end
end

context 'when the remote URL responds with 500' do
before { stub_request(:get, stripped_remote_config_url).to_return(status: 500) }

it 'raises error' do
expect do
remote_config
end.to raise_error(Net::HTTPFatalError,
'500 "" while downloading remote config file http://example.com/rubocop.yml')
end
end
end

context 'when the remote URL responds with redirect' do
let(:new_location) { 'http://cdn.example.com/rubocop.yml' }

Expand Down

0 comments on commit afd2ab6

Please sign in to comment.