Port Homebrew::DevCmd::BumpFormulaPr

This commit is contained in:
Douglas Eichelberger 2024-03-18 16:11:28 -07:00
parent ebc458a7fe
commit d873881c47
3 changed files with 513 additions and 508 deletions

View File

@ -119,7 +119,9 @@ module Homebrew
] ]
end end
sig { params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void } sig {
params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void
}
def initialize(cmd = nil, &block) def initialize(cmd = nil, &block)
@parser = OptionParser.new @parser = OptionParser.new

View File

@ -1,17 +1,16 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "formula" require "formula"
require "cli/parser" require "cli/parser"
require "utils/pypi" require "utils/pypi"
require "utils/tar" require "utils/tar"
module Homebrew module Homebrew
module_function module DevCmd
class BumpFormulaPr < AbstractCommand
sig { returns(CLI::Parser) } cmd_args do
def bump_formula_pr_args
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Create a pull request to update <formula> with a new URL or a new tag. Create a pull request to update <formula> with a new URL or a new tag.
@ -89,11 +88,9 @@ module Homebrew
named_args :formula, max: 1, without_api: true named_args :formula, max: 1, without_api: true
end end
end
def bump_formula_pr
args = bump_formula_pr_args.parse
sig { override.void }
def run
if args.revision.present? && args.tag.nil? && args.version.nil? if args.revision.present? && args.tag.nil? && args.version.nil?
raise UsageError, "`--revision` must be passed with either `--tag` or `--version`!" raise UsageError, "`--revision` must be passed with either `--tag` or `--version`!"
end end
@ -134,10 +131,10 @@ module Homebrew
remote_branch = formula.tap.git_repo.origin_branch_name remote_branch = formula.tap.git_repo.origin_branch_name
previous_branch = "-" previous_branch = "-"
check_open_pull_requests(formula, tap_remote_repo, args:) check_open_pull_requests(formula, tap_remote_repo)
new_version = args.version new_version = args.version
check_new_version(formula, tap_remote_repo, version: new_version, args:) if new_version.present? check_new_version(formula, tap_remote_repo, version: new_version) if new_version.present?
opoo "This formula has patches that may be resolved upstream." if formula.patchlist.present? opoo "This formula has patches that may be resolved upstream." if formula.patchlist.present?
if formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") } if formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") }
@ -149,7 +146,7 @@ module Homebrew
new_mirror ||= determine_mirror(new_url) new_mirror ||= determine_mirror(new_url)
new_mirrors ||= [new_mirror] if new_mirror.present? new_mirrors ||= [new_mirror] if new_mirror.present?
check_for_mirrors(formula, old_mirrors, new_mirrors, args:) if new_url.present? check_for_mirrors(formula, old_mirrors, new_mirrors) if new_url.present?
old_hash = formula_spec.checksum&.hexdigest old_hash = formula_spec.checksum&.hexdigest
new_hash = args.sha256 new_hash = args.sha256
@ -161,10 +158,10 @@ module Homebrew
old_version = old_formula_version.to_s old_version = old_formula_version.to_s
forced_version = new_version.present? forced_version = new_version.present?
new_url_hash = if new_url.present? && new_hash.present? new_url_hash = if new_url.present? && new_hash.present?
check_new_version(formula, tap_remote_repo, url: new_url, args:) if new_version.blank? check_new_version(formula, tap_remote_repo, url: new_url) if new_version.blank?
true true
elsif new_tag.present? && new_revision.present? elsif new_tag.present? && new_revision.present?
check_new_version(formula, tap_remote_repo, url: old_url, tag: new_tag, args:) if new_version.blank? check_new_version(formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank?
false false
elsif old_hash.blank? elsif old_hash.blank?
if new_tag.blank? && new_version.blank? && new_revision.blank? if new_tag.blank? && new_version.blank? && new_revision.blank?
@ -179,8 +176,9 @@ module Homebrew
and old tag are both #{new_tag}. and old tag are both #{new_tag}.
EOS EOS
end end
check_new_version(formula, tap_remote_repo, url: old_url, tag: new_tag, args:) if new_version.blank? check_new_version(formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank?
resource_path, forced_version = fetch_resource_and_forced_version(formula, new_version, old_url, tag: new_tag) resource_path, forced_version = fetch_resource_and_forced_version(formula, new_version, old_url,
tag: new_tag)
new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify", "HEAD") new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify", "HEAD")
new_revision = new_revision.strip new_revision = new_revision.strip
elsif new_revision.blank? elsif new_revision.blank?
@ -190,12 +188,12 @@ module Homebrew
elsif new_url.blank? && new_version.blank? elsif new_url.blank? && new_version.blank?
raise UsageError, "#{formula}: no `--url` or `--version` argument specified!" raise UsageError, "#{formula}: no `--url` or `--version` argument specified!"
else else
new_url ||= PyPI.update_pypi_url(old_url, new_version) new_url ||= PyPI.update_pypi_url(old_url, T.must(new_version))
if new_url.blank? if new_url.blank?
new_url = update_url(old_url, old_version, new_version) new_url = update_url(old_url, old_version, T.must(new_version))
if new_mirrors.blank? && old_mirrors.present? if new_mirrors.blank? && old_mirrors.present?
new_mirrors = old_mirrors.map do |old_mirror| new_mirrors = old_mirrors.map do |old_mirror|
update_url(old_mirror, old_version, new_version) update_url(old_mirror, old_version, T.must(new_version))
end end
end end
end end
@ -206,7 +204,7 @@ module Homebrew
#{new_url} #{new_url}
EOS EOS
end end
check_new_version(formula, tap_remote_repo, url: new_url, args:) if new_version.blank? check_new_version(formula, tap_remote_repo, url: new_url) if new_version.blank?
resource_path, forced_version = fetch_resource_and_forced_version(formula, new_version, new_url) resource_path, forced_version = fetch_resource_and_forced_version(formula, new_version, new_url)
Utils::Tar.validate_file(resource_path) Utils::Tar.validate_file(resource_path)
new_hash = resource_path.sha256 new_hash = resource_path.sha256
@ -273,7 +271,7 @@ module Homebrew
if new_mirrors.present? if new_mirrors.present?
replacement_pairs << [ replacement_pairs << [
/^( +)(url "#{Regexp.escape(new_url)}"[^\n]*?\n)/m, /^( +)(url "#{Regexp.escape(T.must(new_url))}"[^\n]*?\n)/m,
"\\1\\2\\1mirror \"#{new_mirrors.join("\"\n\\1mirror \"")}\"\n", "\\1\\2\\1mirror \"#{new_mirrors.join("\"\n\\1mirror \"")}\"\n",
] ]
end end
@ -344,7 +342,7 @@ module Homebrew
ignore_non_pypi_packages: true ignore_non_pypi_packages: true
end end
run_audit(formula, alias_rename, old_contents, args:) run_audit(formula, alias_rename, old_contents)
pr_message = "Created with `brew bump-formula-pr`." pr_message = "Created with `brew bump-formula-pr`."
if resources_checked.nil? && formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") } if resources_checked.nil? && formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") }
@ -393,6 +391,8 @@ module Homebrew
GitHub.create_bump_pr(pr_info, args:) GitHub.create_bump_pr(pr_info, args:)
end end
private
def determine_mirror(url) def determine_mirror(url)
case url case url
when %r{.*ftp\.gnu\.org/gnu.*} when %r{.*ftp\.gnu\.org/gnu.*}
@ -406,7 +406,7 @@ module Homebrew
end end
end end
def check_for_mirrors(formula, old_mirrors, new_mirrors, args:) def check_for_mirrors(formula, old_mirrors, new_mirrors)
return if new_mirrors.present? || old_mirrors.empty? return if new_mirrors.present? || old_mirrors.empty?
if args.force? if args.force?
@ -451,14 +451,14 @@ module Homebrew
end end
end end
def check_open_pull_requests(formula, tap_remote_repo, args:) def check_open_pull_requests(formula, tap_remote_repo)
GitHub.check_for_duplicate_pull_requests(formula.name, tap_remote_repo, GitHub.check_for_duplicate_pull_requests(formula.name, tap_remote_repo,
state: "open", state: "open",
file: formula.path.relative_path_from(formula.tap.path).to_s, file: formula.path.relative_path_from(formula.tap.path).to_s,
quiet: args.quiet?) quiet: args.quiet?)
end end
def check_new_version(formula, tap_remote_repo, args:, version: nil, url: nil, tag: nil) def check_new_version(formula, tap_remote_repo, version: nil, url: nil, tag: nil)
if version.nil? if version.nil?
specs = {} specs = {}
specs[:tag] = tag if tag.present? specs[:tag] = tag if tag.present?
@ -467,7 +467,7 @@ module Homebrew
end end
check_throttle(formula, version) check_throttle(formula, version)
check_closed_pull_requests(formula, tap_remote_repo, args:, version:) check_closed_pull_requests(formula, tap_remote_repo, version:)
end end
def check_throttle(formula, new_version) def check_throttle(formula, new_version)
@ -480,7 +480,7 @@ module Homebrew
odie "#{formula} should only be updated every #{throttled_rate} releases on multiples of #{throttled_rate}" odie "#{formula} should only be updated every #{throttled_rate} releases on multiples of #{throttled_rate}"
end end
def check_closed_pull_requests(formula, tap_remote_repo, args:, version:) def check_closed_pull_requests(formula, tap_remote_repo, version:)
# if we haven't already found open requests, try for an exact match across closed requests # if we haven't already found open requests, try for an exact match across closed requests
GitHub.check_for_duplicate_pull_requests(formula.name, tap_remote_repo, GitHub.check_for_duplicate_pull_requests(formula.name, tap_remote_repo,
version:, version:,
@ -501,7 +501,7 @@ module Homebrew
[versioned_alias, "#{name}@#{new_alias_version}"] [versioned_alias, "#{name}@#{new_alias_version}"]
end end
def run_audit(formula, alias_rename, old_contents, args:) def run_audit(formula, alias_rename, old_contents)
audit_args = ["--formula"] audit_args = ["--formula"]
audit_args << "--strict" if args.strict? audit_args << "--strict" if args.strict?
audit_args << "--online" if args.online? audit_args << "--online" if args.online?
@ -532,4 +532,6 @@ module Homebrew
FileUtils.mv alias_rename.last, alias_rename.first if alias_rename.present? FileUtils.mv alias_rename.last, alias_rename.first if alias_rename.present?
odie "`brew audit` failed!" odie "`brew audit` failed!"
end end
end
end
end end

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "dev-cmd/bump-formula-pr"
RSpec.describe "brew bump-formula-pr" do RSpec.describe Homebrew::DevCmd::BumpFormulaPr do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
end end