diff options
| author | Ben Muschol | 2017-08-13 13:10:38 -0400 |
|---|---|---|
| committer | Ben Muschol | 2017-08-13 15:03:44 -0400 |
| commit | e93ec12b32f3449f4669cf53469a4d5308b10cc3 (patch) | |
| tree | 72476000915220c84b580c3628a33efe26d524b1 /Library | |
| parent | d6d681ca01f8acf6b8d5e66cc1ca67310e9b8913 (diff) | |
| download | brew-e93ec12b32f3449f4669cf53469a4d5308b10cc3.tar.bz2 | |
Remove duplicate url generation logic in Github module
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cli/search.rb | 5 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/search.rb | 5 | ||||
| -rw-r--r-- | Library/Homebrew/test/utils/github_spec.rb | 22 | ||||
| -rw-r--r-- | Library/Homebrew/utils/github.rb | 57 |
4 files changed, 59 insertions, 30 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 643d18d55..0cc4cc56c 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -15,8 +15,9 @@ module Hbc end def self.search_remote(query) - matches = GitHub.search_code("user:caskroom", "path:Casks", "filename:#{query}", "extension:rb") - [*matches].map do |match| + matches = GitHub.search_code(user: "caskroom", path: "Casks", + filename: query, extension: "rb") + Array(matches).map do |match| tap = Tap.fetch(match["repository"]["full_name"]) next if tap.installed? "#{tap.name}/#{File.basename(match["path"], ".rb")}" diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 5b83d80e2..4607f6d1a 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -102,8 +102,9 @@ module Homebrew def search_taps(query) valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze - matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb") - [*matches].map do |match| + matches = GitHub.search_code(user: ["Homebrew", "caskroom"], filename: query, extension: "rb") + + Array(matches).map do |match| dirname, filename = File.split(match["path"]) next unless valid_dirnames.include?(dirname) tap = Tap.fetch(match["repository"]["full_name"]) diff --git a/Library/Homebrew/test/utils/github_spec.rb b/Library/Homebrew/test/utils/github_spec.rb index 9b539262f..cf5f6c808 100644 --- a/Library/Homebrew/test/utils/github_spec.rb +++ b/Library/Homebrew/test/utils/github_spec.rb @@ -2,12 +2,30 @@ require "utils/github" describe GitHub do describe "::search_code", :needs_network do - it "searches code" do - results = subject.search_code("repo:Homebrew/brew", "path:/", "filename:readme", "language:markdown") + it "queries GitHub code with the passed paramaters" do + results = subject.search_code(repo: "Homebrew/brew", path: "/", + filename: "readme", language: "markdown") expect(results.count).to eq(1) expect(results.first["name"]).to eq("README.md") expect(results.first["path"]).to eq("README.md") end end + + describe "::query_string" do + it "builds a query with the given hash parameters formatted as key:value" do + query = subject.query_string(user: "Homebrew", repo: "Brew") + expect(query).to eq("q=user%3aHomebrew+repo%3aTest&per_page=100") + end + + it "adds a variable number of top-level string parameters to the query when provided" do + query = subject.query_string("value1", "value2", user: "Homebrew") + expect(query).to eq("q=value1+value2+user%3aHomebrew&per_page=100") + end + + it "turns array values into multiple key:value parameters" do + query = subject.query_string(user: ["Homebrew", "caskroom"]) + expect(query).to eq("q=user%3aHomebrew+user%3acaskroom&per_page=100") + end + end end diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 07eea4384..6f56e505c 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -228,34 +228,15 @@ module GitHub end def issues_matching(query, qualifiers = {}) - uri = URI.parse("#{API_URL}/search/issues") - uri.query = build_query_string(query, qualifiers) - open(uri) { |json| json["items"] } + search("issues", query, **qualifiers) end def repository(user, repo) - open(URI.parse("#{API_URL}/repos/#{user}/#{repo}")) - 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) - s = "q=#{uri_escape(query)}+" - s << build_search_qualifier_string(qualifiers) - s << "&per_page=100" + open(path_to("repos", user, repo)) end - def build_search_qualifier_string(qualifiers) - { - repo: "Homebrew/homebrew-core", - in: "title", - }.update(qualifiers).map do |qualifier, value| - "#{qualifier}:#{value}" - end.join("+") + def search_code(**params) + search("code", **params) end def uri_escape(query) @@ -292,7 +273,35 @@ module GitHub end def private_repo?(full_name) - uri = URI.parse("#{API_URL}/repos/#{full_name}") + uri = path_to "repos", full_name open(uri) { |json| json["private"] } end + + def query_string(*main_params, **qualifiers) + params_list = main_params + + qualifiers.each do |key, value| + if value.is_a? Array + value.each { |v| params_list << format_parameter(key, v) } + else + params_list << format_parameter(key, v) + end + end + + "q=#{uri_escape(params_list.join(" "))}&per_page=100" + end + + def format_paramater(key, value) + "#{key}:#{value}" + end + + def path_to(*subroutes) + URI.parse(File.join(API_URL, *subroutes)) + end + + def search(entity, *queries, **qualifiers) + uri = path_to "search", entity + uri.query = query_string(*queries, **qualifiers) + open(uri) { |json| json["items"] } + end end |
