bump-formula-pr: check duplicate PRs as early as possible

This commit is contained in:
Seeker 2020-07-16 20:35:09 -07:00
parent df17f8a0cf
commit 06d9152098

View File

@ -127,6 +127,10 @@ module Homebrew
raise FormulaUnspecifiedError unless formula raise FormulaUnspecifiedError unless formula
tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula) tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula)
check_open_pull_requests(formula, tap_full_name)
new_version = args.version
check_all_pull_requests(formula, tap_full_name, version: new_version) if new_version
requested_spec, formula_spec = if args.devel? requested_spec, formula_spec = if args.devel?
devel_message = " (devel)" devel_message = " (devel)"
@ -155,15 +159,16 @@ module Homebrew
new_url.sub "mirrors.ocf.berkeley.edu/debian", "mirrorservice.org/sites/ftp.debian.org/debian" new_url.sub "mirrors.ocf.berkeley.edu/debian", "mirrorservice.org/sites/ftp.debian.org/debian"
end end
new_mirrors ||= [new_mirror] unless new_mirror.nil? new_mirrors ||= [new_mirror] unless new_mirror.nil?
new_version = args.version
old_url = formula_spec.url old_url = formula_spec.url
old_tag = formula_spec.specs[:tag] old_tag = formula_spec.specs[:tag]
old_formula_version = formula_version(formula, requested_spec) old_formula_version = formula_version(formula, requested_spec)
old_version = old_formula_version.to_s old_version = old_formula_version.to_s
forced_version = false forced_version = new_version.present?
new_url_hash = if new_url && new_hash new_url_hash = if new_url && new_hash
check_all_pull_requests(formula, tap_full_name, url: new_url) unless new_version
true true
elsif new_tag && new_revision elsif new_tag && new_revision
check_all_pull_requests(formula, tap_full_name, url: old_url, tag: new_tag) unless new_version
false false
elsif !hash_type elsif !hash_type
odie "#{formula}: no --tag= or --version= argument specified!" if !new_tag && !new_version odie "#{formula}: no --tag= or --version= argument specified!" if !new_tag && !new_version
@ -174,6 +179,7 @@ module Homebrew
and old tag are both #{new_tag}. and old tag are both #{new_tag}.
EOS EOS
end end
check_all_pull_requests(formula, tap_full_name, url: old_url, tag: new_tag) unless new_version
resource_path, forced_version = fetch_resource(formula, new_version, old_url, tag: new_tag) resource_path, forced_version = fetch_resource(formula, new_version, old_url, tag: new_tag)
new_revision = Utils.popen_read("git -C \"#{resource_path}\" rev-parse -q --verify HEAD") new_revision = Utils.popen_read("git -C \"#{resource_path}\" rev-parse -q --verify HEAD")
new_revision = new_revision.strip new_revision = new_revision.strip
@ -189,6 +195,7 @@ module Homebrew
#{new_url} #{new_url}
EOS EOS
end end
check_all_pull_requests(formula, tap_full_name, url: new_url) unless new_version
resource_path, forced_version = fetch_resource(formula, new_version, new_url) resource_path, forced_version = fetch_resource(formula, new_version, new_url)
tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ] tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ]
if tar_file_extensions.any? { |extension| new_url.include? extension } if tar_file_extensions.any? { |extension| new_url.include? extension }
@ -244,7 +251,7 @@ module Homebrew
] ]
end end
backup_file = File.read(formula.path) unless args.dry_run? old_contents = File.read(formula.path) unless args.dry_run?
if new_mirrors if new_mirrors
replacement_pairs << [ replacement_pairs << [
@ -307,8 +314,6 @@ module Homebrew
new_formula_version = formula_version(formula, requested_spec, new_contents) new_formula_version = formula_version(formula, requested_spec, new_contents)
check_for_duplicate_pull_requests(formula, backup_file, tap_full_name, new_formula_version.to_s)
if !new_mirrors && !formula_spec.mirrors.empty? if !new_mirrors && !formula_spec.mirrors.empty?
if args.force? if args.force?
opoo "#{formula}: Removing all mirrors because a --mirror= argument was not specified." opoo "#{formula}: Removing all mirrors because a --mirror= argument was not specified."
@ -321,13 +326,13 @@ module Homebrew
end end
if new_formula_version < old_formula_version if new_formula_version < old_formula_version
formula.path.atomic_write(backup_file) unless args.dry_run? formula.path.atomic_write(old_contents) unless args.dry_run?
odie <<~EOS odie <<~EOS
You probably need to bump this formula manually since changing the You probably need to bump this formula manually since changing the
version from #{old_formula_version} to #{new_formula_version} would be a downgrade. version from #{old_formula_version} to #{new_formula_version} would be a downgrade.
EOS EOS
elsif new_formula_version == old_formula_version elsif new_formula_version == old_formula_version
formula.path.atomic_write(backup_file) unless args.dry_run? formula.path.atomic_write(old_contents) unless args.dry_run?
odie <<~EOS odie <<~EOS
You probably need to bump this formula manually since the new version You probably need to bump this formula manually since the new version
and old version are both #{new_formula_version}. and old version are both #{new_formula_version}.
@ -340,7 +345,7 @@ module Homebrew
alias_rename.map! { |a| formula.tap.alias_dir/a } alias_rename.map! { |a| formula.tap.alias_dir/a }
end end
run_audit(formula, alias_rename, backup_file) run_audit(formula, alias_rename, old_contents)
formula.path.parent.cd do formula.path.parent.cd do
branch = "#{formula.name}-#{new_formula_version}" branch = "#{formula.name}-#{new_formula_version}"
@ -365,7 +370,7 @@ module Homebrew
remote_url = Utils.popen_read("git remote get-url --push origin").chomp remote_url = Utils.popen_read("git remote get-url --push origin").chomp
username = formula.tap.user username = formula.tap.user
else else
remote_url, username = forked_repo_info(formula, tap_full_name, backup_file) remote_url, username = forked_repo_info(formula, tap_full_name, old_contents)
end end
safe_system "git", "fetch", "--unshallow", "origin" if shallow safe_system "git", "fetch", "--unshallow", "origin" if shallow
@ -438,10 +443,10 @@ module Homebrew
[resource.fetch, forced_version] [resource.fetch, forced_version]
end end
def forked_repo_info(formula, tap_full_name, backup_file) def forked_repo_info(formula, tap_full_name, old_contents)
response = GitHub.create_fork(tap_full_name) response = GitHub.create_fork(tap_full_name)
rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e rescue GitHub::AuthenticationFailedError, *GitHub.api_errors => e
formula.path.atomic_write(backup_file) formula.path.atomic_write(old_contents)
odie "Unable to fork: #{e.message}!" odie "Unable to fork: #{e.message}!"
else else
# GitHub API responds immediately but fork takes a few seconds to be ready. # GitHub API responds immediately but fork takes a few seconds to be ready.
@ -506,12 +511,20 @@ module Homebrew
[] []
end end
def check_for_duplicate_pull_requests(formula, backup_file, tap_full_name, version) def check_open_pull_requests(formula, tap_full_name)
# check for open requests # check for open requests
pull_requests = fetch_pull_requests(formula.name, tap_full_name, state: "open") pull_requests = fetch_pull_requests(formula.name, tap_full_name, state: "open")
check_for_duplicate_pull_requests(pull_requests)
end
def check_all_pull_requests(formula, tap_full_name, version: nil, url: nil, tag: nil)
version ||= Version.detect(url, tag ? { tag: tag } : {})
# if we haven't already found open requests, try for an exact match across all requests # if we haven't already found open requests, try for an exact match across all requests
pull_requests = fetch_pull_requests("#{formula.name} #{version}", tap_full_name) if pull_requests.blank? pull_requests = fetch_pull_requests("#{formula.name} #{version}", tap_full_name) if pull_requests.blank?
check_for_duplicate_pull_requests(pull_requests)
end
def check_for_duplicate_pull_requests(pull_requests)
return if pull_requests.blank? return if pull_requests.blank?
duplicates_message = <<~EOS duplicates_message = <<~EOS
@ -522,10 +535,8 @@ module Homebrew
if args.force? && !args.quiet? if args.force? && !args.quiet?
opoo duplicates_message opoo duplicates_message
elsif !args.force? && args.quiet? elsif !args.force? && args.quiet?
formula.path.atomic_write(backup_file) unless args.dry_run?
odie error_message odie error_message
elsif !args.force? elsif !args.force?
formula.path.atomic_write(backup_file) unless args.dry_run?
odie <<~EOS odie <<~EOS
#{duplicates_message.chomp} #{duplicates_message.chomp}
#{error_message} #{error_message}
@ -545,7 +556,7 @@ module Homebrew
[versioned_alias, "#{name}@#{new_alias_version}"] [versioned_alias, "#{name}@#{new_alias_version}"]
end end
def run_audit(formula, alias_rename, backup_file) def run_audit(formula, alias_rename, old_contents)
if args.dry_run? if args.dry_run?
if args.no_audit? if args.no_audit?
ohai "Skipping `brew audit`" ohai "Skipping `brew audit`"
@ -569,7 +580,7 @@ module Homebrew
end end
return unless failed_audit return unless failed_audit
formula.path.atomic_write(backup_file) formula.path.atomic_write(old_contents)
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