Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use the GITHUB_API_TOKEN if it's not empty #231

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/librarian/puppet/source/githubtarball/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def vendor_cache(name, version)
clean_up_old_cached_versions(name)

url = "https://api.github.com/repos/#{name}/tarball/#{version}"
url << "?access_token=#{ENV['GITHUB_API_TOKEN']}" if ENV['GITHUB_API_TOKEN']
add_api_token_to_url(url)

environment.vendor!
File.open(vendored_path(name, version).to_s, 'wb') do |f|
Expand All @@ -95,14 +95,31 @@ def clean_up_old_cached_versions(name)
end
end

def token_key_value
ENV[TOKEN_KEY]
end

def token_key_nil?
token_key_value.nil? || token_key_value.empty?
end

def add_api_token_to_url url
if token_key_nil?
debug { "#{TOKEN_KEY} environment value is empty or missing" }
else
url << "?access_token=#{ENV[TOKEN_KEY]}"
end
url
end

private

def api_call(path)
tags = []
url = "https://api.github.com#{path}?page=1&per_page=100"
while true do
debug { " Module #{name} getting tags at: #{url}" }
url << "&access_token=#{ENV[TOKEN_KEY]}" if ENV[TOKEN_KEY]
add_api_token_to_url(url)
response = http_get(url, :headers => {
"User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}"
})
Expand Down
10 changes: 10 additions & 0 deletions test/librarian/puppet/source/githubtarball_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ def [](key)
let(:repo) { Librarian::Puppet::Source::GitHubTarball::Repo.new(source, "bar") }
let(:headers) { {'User-Agent' => "librarian-puppet v#{Librarian::Puppet::VERSION}"} }
let(:url) { "https://api.github.com/foo?page=1&per_page=100" }
let(:url_with_token) { "https://api.github.com/foo?page=1&per_page=100?access_token=bar" }
ENV['GITHUB_API_TOKEN'] = ''

it "succeeds" do
response = []
repo.expects(:http_get).with(url, {:headers => headers}).returns(FakeResponse.new(200, JSON.dump(response)))
repo.send(:api_call, "/foo").must_equal(response)
end

it "adds GITHUB_API_TOKEN if present" do
ENV['GITHUB_API_TOKEN'] = 'bar'
response = []
repo.expects(:http_get).with(url_with_token, {:headers => headers}).returns(FakeResponse.new(200, JSON.dump(response)))
repo.send(:api_call, "/foo").must_equal(response)
ENV['GITHUB_API_TOKEN'] = ''
end

it "fails when we hit api limit" do
response = {"message" => "Oh boy! API rate limit exceeded!!!"}
repo.expects(:http_get).with(url, {:headers => headers}).returns(FakeResponse.new(403, JSON.dump(response)))
Expand Down