Parse JSON early in GitHub module
This commit is contained in:
parent
f7e1244e6d
commit
4d6df3e3bc
@ -1,7 +1,6 @@
|
|||||||
require 'formula'
|
require 'formula'
|
||||||
require 'blacklist'
|
require 'blacklist'
|
||||||
require 'utils'
|
require 'utils'
|
||||||
require 'utils/json'
|
|
||||||
|
|
||||||
module Homebrew extend self
|
module Homebrew extend self
|
||||||
|
|
||||||
@ -103,9 +102,9 @@ module Homebrew extend self
|
|||||||
return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}").directory?
|
return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}").directory?
|
||||||
|
|
||||||
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 |json|
|
||||||
user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization
|
user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization
|
||||||
Utils::JSON.load(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file|
|
json["tree"].map{ |hash| hash['path'] }.compact.each do |file|
|
||||||
name = File.basename(file, '.rb')
|
name = File.basename(file, '.rb')
|
||||||
if file =~ /\.rb$/ and name =~ rx
|
if file =~ /\.rb$/ and name =~ rx
|
||||||
results << "#{user}/#{repo}/#{name}"
|
results << "#{user}/#{repo}/#{name}"
|
||||||
@ -113,10 +112,8 @@ module Homebrew extend self
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
results
|
results
|
||||||
rescue OpenURI::HTTPError, GitHub::Error, Utils::JSON::Error
|
rescue OpenURI::HTTPError, GitHub::Error
|
||||||
opoo <<-EOS.undent
|
opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`"
|
||||||
Failed to search tap: #{user}/#{repo}. Please run `brew update`.
|
|
||||||
EOS
|
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -261,7 +261,9 @@ module GitHub extend self
|
|||||||
|
|
||||||
default_headers = {'User-Agent' => HOMEBREW_USER_AGENT}
|
default_headers = {'User-Agent' => HOMEBREW_USER_AGENT}
|
||||||
default_headers['Authorization'] = "token #{HOMEBREW_GITHUB_API_TOKEN}" if HOMEBREW_GITHUB_API_TOKEN
|
default_headers['Authorization'] = "token #{HOMEBREW_GITHUB_API_TOKEN}" if HOMEBREW_GITHUB_API_TOKEN
|
||||||
Kernel.open(url, default_headers.merge(headers), &block)
|
Kernel.open(url, default_headers.merge(headers)) do |f|
|
||||||
|
yield Utils::JSON.load(f.read)
|
||||||
|
end
|
||||||
rescue OpenURI::HTTPError => e
|
rescue OpenURI::HTTPError => e
|
||||||
if e.io.meta['x-ratelimit-remaining'].to_i <= 0
|
if e.io.meta['x-ratelimit-remaining'].to_i <= 0
|
||||||
raise <<-EOS.undent
|
raise <<-EOS.undent
|
||||||
@ -274,11 +276,13 @@ module GitHub extend self
|
|||||||
end
|
end
|
||||||
rescue SocketError, OpenSSL::SSL::SSLError => e
|
rescue SocketError, OpenSSL::SSL::SSLError => e
|
||||||
raise Error, "Failed to connect to: #{url}\n#{e.message}"
|
raise Error, "Failed to connect to: #{url}\n#{e.message}"
|
||||||
|
rescue Utils::JSON::Error => e
|
||||||
|
raise Error, "Failed to parse JSON response\n#{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def issues_matching(query)
|
def issues_matching(query)
|
||||||
uri = ISSUES_URI + uri_escape(query)
|
uri = ISSUES_URI + uri_escape(query)
|
||||||
open(uri) { |f| Utils::JSON.load(f.read)['issues'] }
|
open(uri) { |json| json["issues"] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def uri_escape(query)
|
def uri_escape(query)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user