From 2bda194bd91c0767517fe11adafcaacb3150aff0 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 24 Apr 2017 19:31:21 +0200 Subject: [PATCH] Add `GitHub::search_code` method. --- Library/Homebrew/cmd/search.rb | 26 +++++++------------ .../test/cmd/search_remote_tap_spec.rb | 5 +--- Library/Homebrew/utils/github.rb | 14 +++++++--- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 110e1559e7..f71a14ba10 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -59,7 +59,7 @@ module Homebrew local_results = search_formulae(regex) puts Formatter.columns(local_results) unless local_results.empty? tap_results = search_taps(query) - puts Formatter.columns(tap_results) if tap_results && !tap_results.empty? + puts Formatter.columns(tap_results) unless tap_results.empty? if $stdout.tty? count = local_results.length + tap_results.length @@ -101,21 +101,15 @@ module Homebrew end def search_taps(query) - valid_dirnames = ["Formula", "HomebrewFormula", "Casks", ".", ""].freeze - q = "user:Homebrew%20user:caskroom%20filename:#{query}" - GitHub.open "https://api.github.com/search/code?q=#{q}" do |json| - json["items"].map do |object| - dirname, filename = File.split(object["path"]) - next unless valid_dirnames.include?(dirname) - user = object["repository"]["owner"]["login"] - user = user.downcase if user == "Homebrew" - repo = object["repository"]["name"].sub(/^homebrew-/, "") - tap = Tap.fetch user, repo - next if tap.installed? - basename = File.basename(filename, ".rb") - "#{user}/#{repo}/#{basename}" - end.compact - end + valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze + matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb") + [*matches].map do |match| + dirname, filename = File.split(match["path"]) + next unless valid_dirnames.include?(dirname) + tap = Tap.fetch(match["repository"]["full_name"]) + next if tap.installed? + "#{tap.name}/#{File.basename(filename, ".rb")}" + end.compact end def search_formulae(regex) diff --git a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb index be7c20865a..b0beb122c4 100644 --- a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb +++ b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb @@ -7,10 +7,7 @@ describe Homebrew do { "path" => "Formula/some-formula.rb", "repository" => { - "name" => "homebrew-foo", - "owner" => { - "login" => "Homebrew", - }, + "full_name" => "Homebrew/homebrew-foo", }, }, ], diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index a5ed5394ac..2daa239822 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -4,7 +4,7 @@ require "tempfile" module GitHub module_function - ISSUES_URI = URI.parse("https://api.github.com/search/issues") + API_URL = "https://api.github.com".freeze CREATE_GIST_SCOPES = ["gist"].freeze CREATE_ISSUE_SCOPES = ["public_repo"].freeze @@ -228,13 +228,19 @@ module GitHub end def issues_matching(query, qualifiers = {}) - uri = ISSUES_URI.dup + uri = URI.parse("#{API_URL}/search/issues") uri.query = build_query_string(query, qualifiers) open(uri) { |json| json["items"] } end def repository(user, repo) - open(URI.parse("https://api.github.com/repos/#{user}/#{repo}")) { |j| j } + open(URI.parse("#{API_URL}/repos/#{user}/#{repo}")) { |j| j } + end + + def search_code(*params) + uri = URI.parse("#{API_URL}/search/code") + uri.query = "q=#{uri_escape(params.join(" "))}" + open(uri) { |json| json["items"] } end def build_query_string(query, qualifiers) @@ -286,7 +292,7 @@ module GitHub end def private_repo?(user, repo) - uri = URI.parse("https://api.github.com/repos/#{user}/#{repo}") + uri = URI.parse("#{API_URL}/repos/#{user}/#{repo}") open(uri) { |json| json["private"] } end end