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)
|
local_results = search_formulae(regex)
|
||||||
puts Formatter.columns(local_results) unless local_results.empty?
|
puts Formatter.columns(local_results) unless local_results.empty?
|
||||||
tap_results = search_taps(query)
|
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?
|
if $stdout.tty?
|
||||||
count = local_results.length + tap_results.length
|
count = local_results.length + tap_results.length
|
||||||
@ -101,22 +101,16 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def search_taps(query)
|
def search_taps(query)
|
||||||
valid_dirnames = ["Formula", "HomebrewFormula", "Casks", ".", ""].freeze
|
valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze
|
||||||
q = "user:Homebrew%20user:caskroom%20filename:#{query}"
|
matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb")
|
||||||
GitHub.open "https://api.github.com/search/code?q=#{q}" do |json|
|
[*matches].map do |match|
|
||||||
json["items"].map do |object|
|
dirname, filename = File.split(match["path"])
|
||||||
dirname, filename = File.split(object["path"])
|
|
||||||
next unless valid_dirnames.include?(dirname)
|
next unless valid_dirnames.include?(dirname)
|
||||||
user = object["repository"]["owner"]["login"]
|
tap = Tap.fetch(match["repository"]["full_name"])
|
||||||
user = user.downcase if user == "Homebrew"
|
|
||||||
repo = object["repository"]["name"].sub(/^homebrew-/, "")
|
|
||||||
tap = Tap.fetch user, repo
|
|
||||||
next if tap.installed?
|
next if tap.installed?
|
||||||
basename = File.basename(filename, ".rb")
|
"#{tap.name}/#{File.basename(filename, ".rb")}"
|
||||||
"#{user}/#{repo}/#{basename}"
|
|
||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def search_formulae(regex)
|
def search_formulae(regex)
|
||||||
aliases = Formula.alias_full_names
|
aliases = Formula.alias_full_names
|
||||||
|
|||||||
@ -7,10 +7,7 @@ describe Homebrew do
|
|||||||
{
|
{
|
||||||
"path" => "Formula/some-formula.rb",
|
"path" => "Formula/some-formula.rb",
|
||||||
"repository" => {
|
"repository" => {
|
||||||
"name" => "homebrew-foo",
|
"full_name" => "Homebrew/homebrew-foo",
|
||||||
"owner" => {
|
|
||||||
"login" => "Homebrew",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@ -4,7 +4,7 @@ require "tempfile"
|
|||||||
module GitHub
|
module GitHub
|
||||||
module_function
|
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_GIST_SCOPES = ["gist"].freeze
|
||||||
CREATE_ISSUE_SCOPES = ["public_repo"].freeze
|
CREATE_ISSUE_SCOPES = ["public_repo"].freeze
|
||||||
@ -228,13 +228,19 @@ module GitHub
|
|||||||
end
|
end
|
||||||
|
|
||||||
def issues_matching(query, qualifiers = {})
|
def issues_matching(query, qualifiers = {})
|
||||||
uri = ISSUES_URI.dup
|
uri = URI.parse("#{API_URL}/search/issues")
|
||||||
uri.query = build_query_string(query, qualifiers)
|
uri.query = build_query_string(query, qualifiers)
|
||||||
open(uri) { |json| json["items"] }
|
open(uri) { |json| json["items"] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def repository(user, repo)
|
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
|
end
|
||||||
|
|
||||||
def build_query_string(query, qualifiers)
|
def build_query_string(query, qualifiers)
|
||||||
@ -286,7 +292,7 @@ module GitHub
|
|||||||
end
|
end
|
||||||
|
|
||||||
def private_repo?(user, repo)
|
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"] }
|
open(uri) { |json| json["private"] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user