From 0fa5c47d7fa15d677f4604d60223d4db2c52e369 Mon Sep 17 00:00:00 2001 From: Daniel Lee Harple Date: Tue, 14 May 2013 10:42:46 -0400 Subject: [PATCH] Check GitHub API rate limit instead of silently failing Signed-off-by: Jack Nagel --- Library/Contributions/cmd/brew-ls-taps.rb | 7 +++-- Library/Homebrew/cmd/search.rb | 17 +++++++------ Library/Homebrew/utils.rb | 31 +++++++++++++++-------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Library/Contributions/cmd/brew-ls-taps.rb b/Library/Contributions/cmd/brew-ls-taps.rb index c3741656bf..600e0ad580 100755 --- a/Library/Contributions/cmd/brew-ls-taps.rb +++ b/Library/Contributions/cmd/brew-ls-taps.rb @@ -1,7 +1,7 @@ require 'vendor/multi_json' -begin - GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f| +GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f| + begin MultiJson.decode(f.read)["repositories"].each do |repo| if repo['name'] =~ /^homebrew-(\S+)$/ puts tap = if repo['username'] == "Homebrew" @@ -11,7 +11,6 @@ begin end end end + rescue end -rescue - nil end diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 4ae2f314a6..2ceefd2ce8 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -64,18 +64,19 @@ module Homebrew extend self results = [] GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f| - user.downcase! if user == "Homebrew" # special handling for the Homebrew organization - MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| - name = File.basename(file, '.rb') - if file =~ /\.rb$/ and name =~ rx - results << "#{user}/#{repo}/#{name}" - $found += 1 + begin + user.downcase! if user == "Homebrew" # special handling for the Homebrew organization + MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| + name = File.basename(file, '.rb') + if file =~ /\.rb$/ and name =~ rx + results << "#{user}/#{repo}/#{name}" + $found += 1 + end end + rescue end end results - rescue - [] end def search_brews rx diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 7506c87a42..ad943b6f86 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -259,7 +259,16 @@ end module GitHub extend self def open url, headers={}, &block require 'open-uri' - Kernel.open(url, headers.merge('User-Agent' => HOMEBREW_USER_AGENT), &block) + begin + Kernel.open(url, {'User-Agent' => HOMEBREW_USER_AGENT}.merge(headers), &block) + rescue OpenURI::HTTPError => e + if e.io.meta['x-ratelimit-remaining'].to_i <= 0 + require 'vendor/multi_json' + raise "GitHub #{MultiJson.decode(e.io.read)['message']}" + else + raise e + end + end end def issues_for_formula name @@ -276,15 +285,16 @@ module GitHub extend self uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}") open uri do |f| - MultiJson.decode(f.read)['issues'].each do |issue| - # don't include issues that just refer to the tool in their body - issues << issue['html_url'] if issue['title'].include? name + begin + MultiJson.decode(f.read)['issues'].each do |issue| + # don't include issues that just refer to the tool in their body + issues << issue['html_url'] if issue['title'].include? name + end + rescue end end issues - rescue - [] end def find_pull_requests rx @@ -294,11 +304,12 @@ module GitHub extend self uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}") GitHub.open uri do |f| - MultiJson.decode(f.read)['issues'].each do |pull| - yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url'] + begin + MultiJson.decode(f.read)['issues'].each do |pull| + yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url'] + end + rescue end end - rescue - nil end end