| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  | #:  * `gist-logs` [`--new-issue`|`-n`] <formula>: | 
					
						
							| 
									
										
										
										
											2016-08-17 01:19:40 +02:00
										 |  |  | #:    Upload logs for a failed build of <formula> to a new Gist. | 
					
						
							| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-08-17 01:19:40 +02:00
										 |  |  | #:    <formula> is usually the name of the formula to install, but it can be specified | 
					
						
							|  |  |  | #:    in several different ways. See [SPECIFYING FORMULAE][]. | 
					
						
							| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-08-17 01:19:40 +02:00
										 |  |  | #:    If `--new-issue` is passed, automatically create a new issue in the appropriate | 
					
						
							|  |  |  | #:    GitHub repository as well as creating the Gist. | 
					
						
							| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-08-17 01:19:40 +02:00
										 |  |  | #:    If no logs are found, an error message is presented. | 
					
						
							| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2016-05-08 20:38:17 +01:00
										 |  |  | require "system_config" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "stringio" | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  | require "socket" | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def gistify_logs(f) | 
					
						
							| 
									
										
										
										
											2015-04-25 22:07:06 -04:00
										 |  |  |     files = load_logs(f.logs) | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     build_time = f.logs.ctime | 
					
						
							|  |  |  |     timestamp = build_time.strftime("%Y-%m-%d_%H-%M-%S") | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     s = StringIO.new | 
					
						
							| 
									
										
										
										
											2016-05-08 20:38:17 +01:00
										 |  |  |     SystemConfig.dump_verbose_config s | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     # Dummy summary file, asciibetically first, to control display title of gist | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |     files["# #{f.name} - #{timestamp}.txt"] = { content: brief_build_info(f) } | 
					
						
							|  |  |  |     files["00.config.out"] = { content: s.string } | 
					
						
							|  |  |  |     files["00.doctor.out"] = { content: `brew doctor 2>&1` } | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |     unless f.core_formula? | 
					
						
							|  |  |  |       tap = <<-EOS.undent
 | 
					
						
							|  |  |  |         Formula: #{f.name} | 
					
						
							|  |  |  |         Tap: #{f.tap} | 
					
						
							|  |  |  |         Path: #{f.path} | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |       files["00.tap.out"] = { content: tap } | 
					
						
							| 
									
										
										
										
											2015-02-11 17:29:56 +08:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |     # Description formatted to work well as page title when viewing gist | 
					
						
							|  |  |  |     if f.core_formula? | 
					
						
							|  |  |  |       descr = "#{f.name} on #{OS_VERSION} - Homebrew build logs" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       descr = "#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     url = create_gist(files, descr) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     if ARGV.include?("--new-issue") || ARGV.switch?("n") | 
					
						
							| 
									
										
										
										
											2016-03-25 18:35:06 +08:00
										 |  |  |       if GitHub.api_credentials_type == :none | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |         puts <<-EOS.undent
 | 
					
						
							|  |  |  |           You can create a new personal access token: | 
					
						
							|  |  |  |            #{GitHub::ALL_SCOPES_URL} | 
					
						
							|  |  |  |           and then set the new HOMEBREW_GITHUB_API_TOKEN as the authentication method. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |         login! | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |       url = create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts url if url | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 05:41:50 -04:00
										 |  |  |   def brief_build_info(f) | 
					
						
							|  |  |  |     build_time_str = f.logs.ctime.strftime("%Y-%m-%d %H:%M:%S") | 
					
						
							|  |  |  |     s = <<-EOS.undent
 | 
					
						
							|  |  |  |       Homebrew build logs for #{f.full_name} on #{OS_VERSION} | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |     if ARGV.include?("--with-hostname") | 
					
						
							|  |  |  |       hostname = Socket.gethostname | 
					
						
							|  |  |  |       s << "Host: #{hostname}\n" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     s << "Build date: #{build_time_str}\n" | 
					
						
							|  |  |  |     s | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   # Hack for ruby < 1.9.3 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |   def noecho_gets | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     system "stty -echo" | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     result = $stdin.gets | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     system "stty echo" | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts | 
					
						
							|  |  |  |     result | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |   def login! | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     print "GitHub User: " | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     ENV["HOMEBREW_GITHUB_API_USERNAME"] = $stdin.gets.chomp | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     print "Password: " | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     ENV["HOMEBREW_GITHUB_API_PASSWORD"] = noecho_gets.chomp | 
					
						
							| 
									
										
										
										
											2014-11-10 23:32:02 +01:00
										 |  |  |     puts | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-25 22:07:06 -04:00
										 |  |  |   def load_logs(dir) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |     logs = {} | 
					
						
							| 
									
										
										
										
											2016-11-13 23:37:40 +01:00
										 |  |  |     if dir.exist? | 
					
						
							|  |  |  |       dir.children.sort.each do |file| | 
					
						
							|  |  |  |         contents = file.size? ? file.read : "empty log" | 
					
						
							|  |  |  |         # small enough to avoid GitHub "unicorn" page-load-timeout errors | 
					
						
							|  |  |  |         max_file_size = 1_000_000
 | 
					
						
							|  |  |  |         contents = truncate_text_to_approximate_size(contents, max_file_size, front_weight: 0.2) | 
					
						
							|  |  |  |         logs[file.basename.to_s] = { content: contents } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     raise "No logs." if logs.empty? | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |     logs | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |   def create_gist(files, description) | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |     url = "https://api.github.com/gists" | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     data = { "public" => true, "files" => files, "description" => description } | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |     scopes = GitHub::CREATE_GIST_SCOPES | 
					
						
							|  |  |  |     GitHub.open(url, data: data, scopes: scopes)["html_url"] | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |   def create_issue(repo, title, body) | 
					
						
							|  |  |  |     url = "https://api.github.com/repos/#{repo}/issues" | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     data = { "title" => title, "body" => body } | 
					
						
							| 
									
										
										
										
											2016-10-30 15:38:19 -04:00
										 |  |  |     scopes = GitHub::CREATE_ISSUE_SCOPES | 
					
						
							|  |  |  |     GitHub.open(url, data: data, scopes: scopes)["html_url"] | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def gist_logs | 
					
						
							| 
									
										
										
										
											2016-05-08 20:39:22 -06:00
										 |  |  |     raise FormulaUnspecifiedError if ARGV.resolved_formulae.length != 1
 | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 13:05:18 +01:00
										 |  |  |     gistify_logs(ARGV.resolved_formulae.first) | 
					
						
							| 
									
										
										
										
											2014-09-20 14:25:09 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |