From b37274de8bea7098e1230408c0126bd3ae45c66e Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 21 Mar 2024 17:29:01 -0700 Subject: [PATCH] Port Homebrew::DevCmd::PrPublish --- Library/Homebrew/dev-cmd/pr-publish.rb | 123 +++++++++--------- .../Homebrew/test/dev-cmd/pr-publish_spec.rb | 5 +- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Library/Homebrew/dev-cmd/pr-publish.rb b/Library/Homebrew/dev-cmd/pr-publish.rb index ea0415f71e..a6b83183c4 100644 --- a/Library/Homebrew/dev-cmd/pr-publish.rb +++ b/Library/Homebrew/dev-cmd/pr-publish.rb @@ -1,75 +1,74 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "abstract_command" require "cli/parser" require "utils/github" module Homebrew - module_function + module DevCmd + class PrPublish < AbstractCommand + cmd_args do + description <<~EOS + Publish bottles for a pull request with GitHub Actions. + Requires write access to the repository. + EOS + switch "--autosquash", + description: "If supported on the target tap, automatically reformat and reword commits " \ + "to our preferred format." + switch "--large-runner", + description: "Run the upload job on a large runner." + flag "--branch=", + description: "Branch to use the workflow from (default: `master`)." + flag "--message=", + depends_on: "--autosquash", + description: "Message to include when autosquashing revision bumps, deletions and rebuilds." + flag "--tap=", + description: "Target tap repository (default: `homebrew/core`)." + flag "--workflow=", + description: "Target workflow filename (default: `publish-commit-bottles.yml`)." - sig { returns(CLI::Parser) } - def pr_publish_args - Homebrew::CLI::Parser.new do - description <<~EOS - Publish bottles for a pull request with GitHub Actions. - Requires write access to the repository. - EOS - switch "--autosquash", - description: "If supported on the target tap, automatically reformat and reword commits " \ - "to our preferred format." - switch "--large-runner", - description: "Run the upload job on a large runner." - flag "--branch=", - description: "Branch to use the workflow from (default: `master`)." - flag "--message=", - depends_on: "--autosquash", - description: "Message to include when autosquashing revision bumps, deletions and rebuilds." - flag "--tap=", - description: "Target tap repository (default: `homebrew/core`)." - flag "--workflow=", - description: "Target workflow filename (default: `publish-commit-bottles.yml`)." - - named_args :pull_request, min: 1 - end - end - - def pr_publish - args = pr_publish_args.parse - - tap = Tap.fetch(args.tap || CoreTap.instance.name) - workflow = args.workflow || "publish-commit-bottles.yml" - ref = args.branch || "master" - - inputs = { - autosquash: args.autosquash?, - large_runner: args.large_runner?, - } - inputs[:message] = args.message if args.message.presence - - args.named.uniq.each do |arg| - arg = "#{tap.default_remote}/pull/#{arg}" if arg.to_i.positive? - url_match = arg.match HOMEBREW_PULL_OR_COMMIT_URL_REGEX - _, user, repo, issue = *url_match - odie "Not a GitHub pull request: #{arg}" unless issue - - inputs[:pull_request] = issue - - pr_labels = GitHub.pull_request_labels(user, repo, issue) - if pr_labels.include?("autosquash") - oh1 "Found `autosquash` label on ##{issue}. Requesting autosquash." - inputs[:autosquash] = true - end - if pr_labels.include?("large-bottle-upload") - oh1 "Found `large-bottle-upload` label on ##{issue}. Requesting upload on large runner." - inputs[:large_runner] = true + named_args :pull_request, min: 1 end - if args.tap.present? && !T.must("#{user}/#{repo}".casecmp(tap.full_name)).zero? - odie "Pull request URL is for #{user}/#{repo} but `--tap=#{tap.full_name}` was specified!" - end + sig { override.void } + def run + tap = Tap.fetch(args.tap || CoreTap.instance.name) + workflow = args.workflow || "publish-commit-bottles.yml" + ref = args.branch || "master" - ohai "Dispatching #{tap} pull request ##{issue}" - GitHub.workflow_dispatch_event(user, repo, workflow, ref, **inputs) + inputs = { + autosquash: args.autosquash?, + large_runner: args.large_runner?, + } + inputs[:message] = args.message if args.message.presence + + args.named.uniq.each do |arg| + arg = "#{tap.default_remote}/pull/#{arg}" if arg.to_i.positive? + url_match = arg.match HOMEBREW_PULL_OR_COMMIT_URL_REGEX + _, user, repo, issue = *url_match + odie "Not a GitHub pull request: #{arg}" unless issue + + inputs[:pull_request] = issue + + pr_labels = GitHub.pull_request_labels(user, repo, issue) + if pr_labels.include?("autosquash") + oh1 "Found `autosquash` label on ##{issue}. Requesting autosquash." + inputs[:autosquash] = true + end + if pr_labels.include?("large-bottle-upload") + oh1 "Found `large-bottle-upload` label on ##{issue}. Requesting upload on large runner." + inputs[:large_runner] = true + end + + if args.tap.present? && !T.must("#{user}/#{repo}".casecmp(tap.full_name)).zero? + odie "Pull request URL is for #{user}/#{repo} but `--tap=#{tap.full_name}` was specified!" + end + + ohai "Dispatching #{tap} pull request ##{issue}" + GitHub.workflow_dispatch_event(user, repo, workflow, ref, **inputs) + end + end end end end diff --git a/Library/Homebrew/test/dev-cmd/pr-publish_spec.rb b/Library/Homebrew/test/dev-cmd/pr-publish_spec.rb index 91dbc30a9c..5c643b6a58 100644 --- a/Library/Homebrew/test/dev-cmd/pr-publish_spec.rb +++ b/Library/Homebrew/test/dev-cmd/pr-publish_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/pr-publish" -RSpec.describe "brew pr-publish" do - it_behaves_like "parseable arguments" +RSpec.describe Homebrew::DevCmd::PrPublish do + it_behaves_like "parseable arguments", argv: ["foo"] end