| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cli/parser" | 
					
						
							|  |  |  | require "utils/github" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |   def pr_publish_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2020-04-18 12:13:43 -04:00
										 |  |  |         Publish bottles for a pull request with GitHub Actions. | 
					
						
							| 
									
										
										
										
											2020-07-02 22:47:45 +02:00
										 |  |  |         Requires write access to the repository. | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2023-03-28 21:51:01 +08:00
										 |  |  |       switch "--autosquash", | 
					
						
							|  |  |  |              description: "If supported on the target tap, automatically reformat and reword commits " \ | 
					
						
							|  |  |  |                           "to our preferred format." | 
					
						
							| 
									
										
										
										
											2021-07-18 05:27:07 +08:00
										 |  |  |       switch "--no-autosquash", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Skip automatically reformatting and rewording commits in the pull request " \ | 
					
						
							| 
									
										
										
										
											2023-03-28 23:07:03 +08:00
										 |  |  |                           "to the preferred format, even if supported on the target tap." | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  |       switch "--large-runner", | 
					
						
							|  |  |  |              description: "Run the upload job on a large runner." | 
					
						
							| 
									
										
										
										
											2021-05-19 18:06:21 +08:00
										 |  |  |       flag   "--branch=", | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  |              description: "Branch to use the workflow from (default: `master`)." | 
					
						
							| 
									
										
										
										
											2020-10-03 16:35:48 +10:00
										 |  |  |       flag   "--message=", | 
					
						
							| 
									
										
										
										
											2023-03-28 21:51:01 +08:00
										 |  |  |              depends_on:  "--autosquash", | 
					
						
							| 
									
										
										
										
											2020-10-03 16:35:48 +10:00
										 |  |  |              description: "Message to include when autosquashing revision bumps, deletions, and rebuilds." | 
					
						
							| 
									
										
										
										
											2020-07-02 22:47:45 +02:00
										 |  |  |       flag   "--tap=", | 
					
						
							|  |  |  |              description: "Target tap repository (default: `homebrew/core`)." | 
					
						
							| 
									
										
										
										
											2020-07-28 11:29:09 +02:00
										 |  |  |       flag   "--workflow=", | 
					
						
							|  |  |  |              description: "Target workflow filename (default: `publish-commit-bottles.yml`)." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       named_args :pull_request, min: 1
 | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def pr_publish | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = pr_publish_args.parse | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 21:51:01 +08:00
										 |  |  |     odeprecated "`brew pr-publish --no-autosquash`" if args.no_autosquash? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 00:56:29 +01:00
										 |  |  |     tap = Tap.fetch(args.tap || CoreTap.instance.name) | 
					
						
							|  |  |  |     workflow = args.workflow || "publish-commit-bottles.yml" | 
					
						
							| 
									
										
										
										
											2021-05-19 18:06:21 +08:00
										 |  |  |     ref = args.branch || "master" | 
					
						
							| 
									
										
										
										
											2020-03-31 10:02:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  |     inputs = { | 
					
						
							| 
									
										
										
										
											2023-03-30 11:11:15 +08:00
										 |  |  |       autosquash:   args.autosquash?, | 
					
						
							|  |  |  |       large_runner: args.large_runner?, | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     inputs[:message] = args.message if args.message.presence | 
					
						
							| 
									
										
										
										
											2020-10-03 16:35:48 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 12:05:50 +02:00
										 |  |  |     args.named.uniq.each do |arg| | 
					
						
							| 
									
										
										
										
											2020-07-02 22:47:45 +02:00
										 |  |  |       arg = "#{tap.default_remote}/pull/#{arg}" if arg.to_i.positive? | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |       url_match = arg.match HOMEBREW_PULL_OR_COMMIT_URL_REGEX | 
					
						
							|  |  |  |       _, user, repo, issue = *url_match | 
					
						
							|  |  |  |       odie "Not a GitHub pull request: #{arg}" unless issue | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       inputs[:pull_request] = issue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-28 21:59:53 +08:00
										 |  |  |       pr_labels = GitHub.pull_request_labels(user, repo, issue) | 
					
						
							| 
									
										
										
										
											2023-03-28 23:07:03 +08:00
										 |  |  |       if pr_labels.include?("autosquash") | 
					
						
							|  |  |  |         oh1 "Found `autosquash` label on ##{issue}. Requesting autosquash." | 
					
						
							|  |  |  |         inputs[:autosquash] = true | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-28 21:59:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-13 18:31:26 -07:00
										 |  |  |       if args.tap.present? && !T.must("#{user}/#{repo}".casecmp(tap.full_name)).zero? | 
					
						
							| 
									
										
										
										
											2021-01-24 21:40:41 -05:00
										 |  |  |         odie "Pull request URL is for #{user}/#{repo} but `--tap=#{tap.full_name}` was specified!" | 
					
						
							| 
									
										
										
										
											2020-07-27 12:29:08 +02:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |       ohai "Dispatching #{tap} pull request ##{issue}" | 
					
						
							| 
									
										
										
										
											2023-03-28 21:58:18 +08:00
										 |  |  |       GitHub.workflow_dispatch_event(user, repo, workflow, ref, inputs) | 
					
						
							| 
									
										
										
										
											2020-03-22 13:12:48 +11:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |