Add GitHub::search_code method.
This commit is contained in:
parent
a38133e5a1
commit
2bda194bd9
@ -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,22 +101,16 @@ 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"])
|
||||
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)
|
||||
user = object["repository"]["owner"]["login"]
|
||||
user = user.downcase if user == "Homebrew"
|
||||
repo = object["repository"]["name"].sub(/^homebrew-/, "")
|
||||
tap = Tap.fetch user, repo
|
||||
tap = Tap.fetch(match["repository"]["full_name"])
|
||||
next if tap.installed?
|
||||
basename = File.basename(filename, ".rb")
|
||||
"#{user}/#{repo}/#{basename}"
|
||||
"#{tap.name}/#{File.basename(filename, ".rb")}"
|
||||
end.compact
|
||||
end
|
||||
end
|
||||
|
||||
def search_formulae(regex)
|
||||
aliases = Formula.alias_full_names
|
||||
|
||||
@ -7,10 +7,7 @@ describe Homebrew do
|
||||
{
|
||||
"path" => "Formula/some-formula.rb",
|
||||
"repository" => {
|
||||
"name" => "homebrew-foo",
|
||||
"owner" => {
|
||||
"login" => "Homebrew",
|
||||
},
|
||||
"full_name" => "Homebrew/homebrew-foo",
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user