Check GitHub API rate limit instead of silently failing

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Daniel Lee Harple 2013-05-14 10:42:46 -04:00 committed by Jack Nagel
parent 222f96d37b
commit 0fa5c47d7f
3 changed files with 33 additions and 22 deletions

View File

@ -1,7 +1,7 @@
require 'vendor/multi_json' 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| MultiJson.decode(f.read)["repositories"].each do |repo|
if repo['name'] =~ /^homebrew-(\S+)$/ if repo['name'] =~ /^homebrew-(\S+)$/
puts tap = if repo['username'] == "Homebrew" puts tap = if repo['username'] == "Homebrew"
@ -11,7 +11,6 @@ begin
end end
end end
end end
rescue
end end
rescue
nil
end end

View File

@ -64,18 +64,19 @@ module Homebrew extend self
results = [] results = []
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |f| 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 begin
MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| user.downcase! if user == "Homebrew" # special handling for the Homebrew organization
name = File.basename(file, '.rb') MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file|
if file =~ /\.rb$/ and name =~ rx name = File.basename(file, '.rb')
results << "#{user}/#{repo}/#{name}" if file =~ /\.rb$/ and name =~ rx
$found += 1 results << "#{user}/#{repo}/#{name}"
$found += 1
end
end end
rescue
end end
end end
results results
rescue
[]
end end
def search_brews rx def search_brews rx

View File

@ -259,7 +259,16 @@ end
module GitHub extend self module GitHub extend self
def open url, headers={}, &block def open url, headers={}, &block
require 'open-uri' 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 end
def issues_for_formula name 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}") uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{name}")
open uri do |f| open uri do |f|
MultiJson.decode(f.read)['issues'].each do |issue| begin
# don't include issues that just refer to the tool in their body MultiJson.decode(f.read)['issues'].each do |issue|
issues << issue['html_url'] if issue['title'].include? name # 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
end end
issues issues
rescue
[]
end end
def find_pull_requests rx 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}") uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}")
GitHub.open uri do |f| GitHub.open uri do |f|
MultiJson.decode(f.read)['issues'].each do |pull| begin
yield pull['pull_request_url'] if rx.match pull['title'] and pull['pull_request_url'] 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
end end
rescue
nil
end end
end end