[DRE-commits] [librarian-puppet] 10/153: Only use the GITHUB_API_TOKEN if it's not empty

Stig Sandbeck Mathisen ssm at debian.org
Wed Jun 1 20:30:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository librarian-puppet.

commit e15268a78ac3b4a3682837621416d963356f9e81
Author: Peter Souter <p.morsou at gmail.com>
Date:   Fri Jun 13 17:13:18 2014 +0100

    Only use the GITHUB_API_TOKEN if it's not empty
    
    Also, for consistency use ENV[TOKEN_KEY] across the board.
---
 lib/librarian/puppet/source/githubtarball/repo.rb  | 21 +++++++++++++++++++--
 test/librarian/puppet/source/githubtarball_test.rb | 10 ++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/lib/librarian/puppet/source/githubtarball/repo.rb b/lib/librarian/puppet/source/githubtarball/repo.rb
index 8a2ad40..af47fac 100644
--- a/lib/librarian/puppet/source/githubtarball/repo.rb
+++ b/lib/librarian/puppet/source/githubtarball/repo.rb
@@ -71,7 +71,7 @@ module Librarian
             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|
@@ -95,6 +95,23 @@ module Librarian
             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)
@@ -102,7 +119,7 @@ module Librarian
             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}"
               })
diff --git a/test/librarian/puppet/source/githubtarball_test.rb b/test/librarian/puppet/source/githubtarball_test.rb
index 9d12cdc..7180ca9 100644
--- a/test/librarian/puppet/source/githubtarball_test.rb
+++ b/test/librarian/puppet/source/githubtarball_test.rb
@@ -28,6 +28,8 @@ describe Librarian::Puppet::Source::GitHubTarball::Repo do
     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 = []
@@ -35,6 +37,14 @@ describe Librarian::Puppet::Source::GitHubTarball::Repo do
       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)))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/librarian-puppet.git



More information about the Pkg-ruby-extras-commits mailing list