| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cli/parser" | 
					
						
							|  |  |  | require "utils/github" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |   def pr_automerge_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2020-04-18 12:13:43 -04:00
										 |  |  |         Find pull requests that can be automatically merged using `brew pr-publish`. | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-04-18 12:13:43 -04:00
										 |  |  |       flag   "--tap=", | 
					
						
							|  |  |  |              description: "Target tap repository (default: `homebrew/core`)." | 
					
						
							|  |  |  |       flag   "--with-label=", | 
					
						
							| 
									
										
										
										
											2020-05-06 16:53:39 +02:00
										 |  |  |              description: "Pull requests must have this label." | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:41 -05:00
										 |  |  |       comma_array "--without-labels", | 
					
						
							|  |  |  |                   description: "Pull requests must not have these labels (default: "\ | 
					
						
							| 
									
										
										
										
											2021-10-23 16:48:38 +02:00
										 |  |  |                                "`do not merge`, `new formula`, `automerge-skip`)." | 
					
						
							| 
									
										
										
										
											2020-05-06 16:53:39 +02:00
										 |  |  |       switch "--without-approval", | 
					
						
							|  |  |  |              description: "Pull requests do not require approval to be merged." | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |       switch "--publish", | 
					
						
							|  |  |  |              description: "Run `brew pr-publish` on matching pull requests." | 
					
						
							| 
									
										
										
										
											2021-07-18 15:54:52 +08:00
										 |  |  |       switch "--no-autosquash", | 
					
						
							| 
									
										
										
										
											2021-07-19 09:43:08 +08:00
										 |  |  |              description: "Instruct `brew pr-publish` to skip automatically reformatting and rewording commits "\ | 
					
						
							| 
									
										
										
										
											2021-07-18 15:36:19 +08:00
										 |  |  |                           "in the pull request to the preferred format." | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |       switch "--ignore-failures", | 
					
						
							|  |  |  |              description: "Include pull requests that have failing status checks." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |       named_args :none | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pr_automerge | 
					
						
							| 
									
										
										
										
											2020-07-26 09:44:26 +02:00
										 |  |  |     args = pr_automerge_args.parse | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 11:06:40 +00:00
										 |  |  |     without_labels = args.without_labels || [ | 
					
						
							|  |  |  |       "do not merge", | 
					
						
							|  |  |  |       "new formula", | 
					
						
							|  |  |  |       "automerge-skip", | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2020-07-26 09:44:26 +02:00
										 |  |  |     tap = Tap.fetch(args.tap || CoreTap.instance.name) | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 12:19:45 +00:00
										 |  |  |     query = "is:pr is:open repo:#{tap.full_name} draft:false" | 
					
						
							| 
									
										
										
										
											2020-07-26 09:44:26 +02:00
										 |  |  |     query += args.ignore_failures? ? " -status:pending" : " status:success" | 
					
						
							|  |  |  |     query += " review:approved" unless args.without_approval? | 
					
						
							|  |  |  |     query += " label:\"#{args.with_label}\"" if args.with_label | 
					
						
							| 
									
										
										
										
											2020-04-15 20:57:35 +02:00
										 |  |  |     without_labels&.each { |label| query += " -label:\"#{label}\"" } | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     odebug "Searching: #{query}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     prs = GitHub.search_issues query | 
					
						
							|  |  |  |     if prs.blank? | 
					
						
							|  |  |  |       ohai "No matching pull requests!" | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-15 20:09:51 -04:00
										 |  |  |     ohai "#{prs.count} matching pull #{"request".pluralize(prs.count)}:" | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     pr_urls = [] | 
					
						
							|  |  |  |     prs.each do |pr| | 
					
						
							|  |  |  |       puts "#{tap.full_name unless tap.core_tap?}##{pr["number"]}: #{pr["title"]}" | 
					
						
							|  |  |  |       pr_urls << pr["html_url"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 21:34:11 +11:00
										 |  |  |     publish_args = ["pr-publish"] | 
					
						
							|  |  |  |     publish_args << "--tap=#{tap}" if tap | 
					
						
							| 
									
										
										
										
											2021-07-20 09:28:38 +08:00
										 |  |  |     publish_args << "--no-autosquash" if args.no_autosquash? | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     if args.publish? | 
					
						
							| 
									
										
										
										
											2020-07-24 18:48:28 +02:00
										 |  |  |       safe_system HOMEBREW_BREW_FILE, *publish_args, *pr_urls | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-10-13 21:34:11 +11:00
										 |  |  |       ohai "Now run:", "  brew #{publish_args.join " "} \\\n    #{pr_urls.join " \\\n    "}" | 
					
						
							| 
									
										
										
										
											2020-04-13 20:14:40 +10:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |