From 222f96d37b3c41b0538e96245ab37c6f01b872c2 Mon Sep 17 00:00:00 2001 From: Daniel Lee Harple Date: Tue, 7 May 2013 14:07:25 -0400 Subject: [PATCH] Pass a User-Agent when fetching data from the GitHub API See . Signed-off-by: Jack Nagel --- Library/Contributions/cmd/brew-ls-taps.rb | 3 +-- Library/Homebrew/cmd/search.rb | 5 ++--- Library/Homebrew/utils.rb | 9 ++++++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Library/Contributions/cmd/brew-ls-taps.rb b/Library/Contributions/cmd/brew-ls-taps.rb index f6c92ebc0c..c3741656bf 100755 --- a/Library/Contributions/cmd/brew-ls-taps.rb +++ b/Library/Contributions/cmd/brew-ls-taps.rb @@ -1,8 +1,7 @@ -require 'open-uri' require 'vendor/multi_json' begin - open "https://api.github.com/legacy/repos/search/homebrew" do |f| + GitHub.open "https://api.github.com/legacy/repos/search/homebrew" do |f| MultiJson.decode(f.read)["repositories"].each do |repo| if repo['name'] =~ /^homebrew-(\S+)$/ puts tap = if repo['username'] == "Homebrew" diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index f64bb5405c..4ae2f314a6 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -1,5 +1,6 @@ require "formula" require "blacklist" +require "utils" module Homebrew extend self def search @@ -59,12 +60,10 @@ module Homebrew extend self def search_tap user, repo, rx return [] if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}-#{repo.downcase}").directory? - - require 'open-uri' require 'vendor/multi_json' results = [] - 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 MultiJson.decode(f.read)["tree"].map{ |hash| hash['path'] }.compact.each do |file| name = File.basename(file, '.rb') diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 81068e9efd..7506c87a42 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -257,6 +257,11 @@ def nostdout end module GitHub extend self + def open url, headers={}, &block + require 'open-uri' + Kernel.open(url, headers.merge('User-Agent' => HOMEBREW_USER_AGENT), &block) + end + def issues_for_formula name # bit basic as depends on the issue at github having the exact name of the # formula in it. Which for stuff like objective-caml is unlikely. So we @@ -264,7 +269,6 @@ module GitHub extend self name = f.name if Formula === name - require 'open-uri' require 'vendor/multi_json' issues = [] @@ -284,13 +288,12 @@ module GitHub extend self end def find_pull_requests rx - require 'open-uri' require 'vendor/multi_json' query = rx.source.delete('.*').gsub('\\', '') uri = URI.parse("https://api.github.com/legacy/issues/search/mxcl/homebrew/open/#{query}") - open uri do |f| + 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'] end