From a65f649cf54d90700d039854c1619027cda9e8f6 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 20 Jan 2014 17:46:33 -0800 Subject: [PATCH] utils: improve issue searching. * issues_matching now returns an array * prints issues titles and URLs * find_pull_requests shows closed PRs if no matching PRs are open Closes Homebrew/homebrew#26032. --- Library/Homebrew/exceptions.rb | 2 +- Library/Homebrew/utils.rb | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 69bf94e2e1..febff797a2 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -211,7 +211,7 @@ class BuildError < Homebrew::InstallationError puts unless RUBY_VERSION < "1.8.6" || issues.empty? puts "These open issues may also help:" - puts issues.map{ |s| " #{s}" }.join("\n") + puts issues.map{ |i| "#{i['title']} (#{i['html_url']})" }.join("\n") end end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 8cde5e7469..f48e92b83a 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -276,9 +276,9 @@ module GitHub extend self raise Error, "Failed to connect to: #{url}\n#{e.message}" end - def each_issue_matching(query, &block) + def issues_matching(query) uri = ISSUES_URI + uri_escape(query) - open(uri) { |f| Utils::JSON.load(f.read)['issues'].each(&block) } + open(uri) { |f| Utils::JSON.load(f.read)['issues'] } end def uri_escape(query) @@ -297,26 +297,30 @@ module GitHub extend self name = f.name if Formula === name - issues = [] - - each_issue_matching(name) do |issue| - # don't include issues that just refer to the tool in their body - issues << issue['html_url'] if issue['title'].include? name - end - - issues + # don't include issues that just refer to the tool in their body + issues_matching(name).select {|issue| issue['title'].include? name } end def find_pull_requests rx return if ENV['HOMEBREW_NO_GITHUB_API'] - puts "Searching open pull requests..." + puts "Searching pull requests..." query = rx.source.delete('.*').gsub('\\', '') - each_issue_matching(query) do |issue| - if rx === issue['title'] && issue.has_key?('pull_request_url') && issue['state'] != 'closed' - yield issue['pull_request_url'] - end + open_or_closed_prs = issues_matching(query).select do |issue| + rx === issue['title'] && issue.has_key?('pull_request_url') end + open_prs = open_or_closed_prs.select {|i| i['state'] != 'closed' } + if open_prs.any? + puts "Open pull requests:" + prs = open_prs + elsif open_or_closed_prs.any? + puts "Closed pull requests:" + prs = open_or_closed_prs + else + return + end + + prs.each {|i| yield "#{i['title']} (#{i['pull_request_url']})" } end end