Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jul 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 27 additions & 16 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Expand Up @@ -127,6 +127,10 @@ def bump_formula_pr
raise FormulaUnspecifiedError unless 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?
devel_message = " (devel)"
Expand Down Expand Up @@ -155,15 +159,16 @@ def bump_formula_pr
new_url.sub "mirrors.ocf.berkeley.edu/debian", "mirrorservice.org/sites/ftp.debian.org/debian"
end
new_mirrors ||= [new_mirror] unless new_mirror.nil?
new_version = args.version
old_url = formula_spec.url
old_tag = formula_spec.specs[:tag]
old_formula_version = formula_version(formula, requested_spec)
old_version = old_formula_version.to_s
forced_version = false
forced_version = new_version.present?
new_url_hash = if new_url && new_hash
check_all_pull_requests(formula, tap_full_name, url: new_url) unless new_version
true
elsif new_tag && new_revision
check_all_pull_requests(formula, tap_full_name, url: old_url, tag: new_tag) unless new_version
false
elsif !hash_type
odie "#{formula}: no --tag= or --version= argument specified!" if !new_tag && !new_version
Expand All @@ -174,6 +179,7 @@ def bump_formula_pr
and old tag are both #{new_tag}.
EOS
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)
new_revision = Utils.popen_read("git -C \"#{resource_path}\" rev-parse -q --verify HEAD")
new_revision = new_revision.strip
Expand All @@ -189,6 +195,7 @@ def bump_formula_pr
#{new_url}
EOS
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)
tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ]
if tar_file_extensions.any? { |extension| new_url.include? extension }
Expand Down Expand Up @@ -244,7 +251,7 @@ def bump_formula_pr
]
end

backup_file = File.read(formula.path) unless args.dry_run?
old_contents = File.read(formula.path) unless args.dry_run?

if new_mirrors
replacement_pairs << [
Expand Down Expand Up @@ -307,8 +314,6 @@ def bump_formula_pr

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 args.force?
opoo "#{formula}: Removing all mirrors because a --mirror= argument was not specified."
Expand All @@ -321,13 +326,13 @@ def bump_formula_pr
end

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
You probably need to bump this formula manually since changing the
version from #{old_formula_version} to #{new_formula_version} would be a downgrade.
EOS
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
You probably need to bump this formula manually since the new version
and old version are both #{new_formula_version}.
Expand All @@ -340,7 +345,7 @@ def bump_formula_pr
alias_rename.map! { |a| formula.tap.alias_dir/a }
end

run_audit(formula, alias_rename, backup_file)
run_audit(formula, alias_rename, old_contents)

formula.path.parent.cd do
branch = "#{formula.name}-#{new_formula_version}"
Expand All @@ -365,7 +370,7 @@ def bump_formula_pr
remote_url = Utils.popen_read("git remote get-url --push origin").chomp
username = formula.tap.user
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

safe_system "git", "fetch", "--unshallow", "origin" if shallow
Expand Down Expand Up @@ -438,10 +443,10 @@ def fetch_resource(formula, new_version, url, **specs)
[resource.fetch, forced_version]
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)
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}!"
else
# GitHub API responds immediately but fork takes a few seconds to be ready.
Expand Down Expand Up @@ -506,12 +511,20 @@ def fetch_pull_requests(query, tap_full_name, state: nil)
[]
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
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 } : {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be extracted into another variable? The ? usage is a bit hard to parse.

# 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?
check_for_duplicate_pull_requests(pull_requests)
end

def check_for_duplicate_pull_requests(pull_requests)
return if pull_requests.blank?

duplicates_message = <<~EOS
Expand All @@ -522,10 +535,8 @@ def check_for_duplicate_pull_requests(formula, backup_file, tap_full_name, versi
if args.force? && !args.quiet?
opoo duplicates_message
elsif !args.force? && args.quiet?
formula.path.atomic_write(backup_file) unless args.dry_run?
odie error_message
elsif !args.force?
formula.path.atomic_write(backup_file) unless args.dry_run?
odie <<~EOS
#{duplicates_message.chomp}
#{error_message}
Expand All @@ -545,7 +556,7 @@ def alias_update_pair(formula, new_formula_version)
[versioned_alias, "#{name}@#{new_alias_version}"]
end

def run_audit(formula, alias_rename, backup_file)
def run_audit(formula, alias_rename, old_contents)
if args.dry_run?
if args.no_audit?
ohai "Skipping `brew audit`"
Expand All @@ -569,7 +580,7 @@ def run_audit(formula, alias_rename, backup_file)
end
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?
odie "`brew audit` failed!"
end
Expand Down