Skip to content

Commit

Permalink
bump-formula-pr: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SeekingMeaning committed Jan 24, 2021
1 parent 2c83ea7 commit cded623
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
39 changes: 27 additions & 12 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def bump_formula_pr
raise FormulaUnspecifiedError if formula.blank?

odie "This formula is disabled!" if formula.disabled?
odie "This formula is deprecated and does not build!" if formula.deprecation_reason == :does_not_build
odie "This formula is not in a tap!" if formula.tap.blank?
odie "This formula's tap is not a Git repository!" unless formula.tap.git?

Expand All @@ -148,7 +149,7 @@ def bump_formula_pr
check_open_pull_requests(formula, tap_full_name, args: args)

new_version = args.version
check_closed_pull_requests(formula, tap_full_name, version: new_version, args: args) if new_version.present?
check_new_version(formula, tap_full_name, version: new_version, args: args) if new_version.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-") }
Expand All @@ -172,10 +173,10 @@ def bump_formula_pr
old_version = old_formula_version.to_s
forced_version = new_version.present?
new_url_hash = if new_url.present? && new_hash.present?
check_closed_pull_requests(formula, tap_full_name, url: new_url, args: args) if new_version.blank?
check_new_version(formula, tap_full_name, url: new_url, args: args) if new_version.blank?
true
elsif new_tag.present? && new_revision.present?
check_closed_pull_requests(formula, tap_full_name, url: old_url, tag: new_tag, args: args) if new_version.blank?
check_new_version(formula, tap_full_name, url: old_url, tag: new_tag, args: args) if new_version.blank?
false
elsif old_hash.blank?
if new_tag.blank? && new_version.blank? && new_revision.blank?
Expand All @@ -190,9 +191,7 @@ def bump_formula_pr
and old tag are both #{new_tag}.
EOS
end
if new_version.blank?
check_closed_pull_requests(formula, tap_full_name, url: old_url, tag: new_tag, args: args)
end
check_new_version(formula, tap_full_name, url: old_url, tag: new_tag, args: args) if new_version.blank?
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 @@ -219,7 +218,7 @@ def bump_formula_pr
#{new_url}
EOS
end
check_closed_pull_requests(formula, tap_full_name, url: new_url, args: args) if new_version.blank?
check_new_version(formula, tap_full_name, url: new_url, args: args) if new_version.blank?
resource_path, forced_version = fetch_resource(formula, new_version, new_url)
Utils::Tar.validate_file(resource_path)
new_hash = resource_path.sha256
Expand Down Expand Up @@ -462,17 +461,33 @@ def check_open_pull_requests(formula, tap_full_name, args:)
args: args)
end

def check_closed_pull_requests(formula, tap_full_name, args:, version: nil, url: nil, tag: nil)
def check_new_version(formula, tap_full_name, args:, version: nil, url: nil, tag: nil)
if version.nil?
specs = {}
specs[:tag] = tag if tag.present?
version = Version.detect(url, **specs)
end
check_throttle(formula, version)
check_closed_pull_requests(formula, tap_full_name, args: args, version: version)
end

def check_throttle(formula, new_version)
throttled_rate = formula.tap.audit_exceptions.dig(:throttled_formulae, formula.name)
return if throttled_rate.blank?

formula_suffix = Version.new(new_version).patch.to_i
return if formula_suffix.modulo(throttled_rate).zero?

odie "#{formula} should only be updated every #{throttled_rate} releases on multiples of #{throttled_rate}"
end

def check_closed_pull_requests(formula, tap_full_name, args:, version:)
# if we haven't already found open requests, try for an exact match across closed requests
GitHub.check_for_duplicate_pull_requests("#{formula.name} #{version}", tap_full_name,
state: "closed",
file: formula.path.relative_path_from(formula.tap.path).to_s,
args: args)
GitHub.check_for_duplicate_pull_requests(formula.name, tap_full_name,
version: version,
state: "closed",
file: formula.path.relative_path_from(formula.tap.path).to_s,
args: args)
end

def alias_update_pair(formula, new_formula_version)
Expand Down
17 changes: 11 additions & 6 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,19 +626,24 @@ def get_repo_license(user, repo)
nil
end

def fetch_pull_requests(query, tap_full_name, state: nil)
def fetch_pull_requests(name, tap_full_name, state: nil, version: nil)
if version.present?
query = "#{name} #{version}"
regex = /(^|\s)#{Regexp.quote(name)}(:|,|\s)(.*\s)?#{Regexp.quote(version)}(:|,|\s|$)/i
else
query = name
regex = /(^|\s)#{Regexp.quote(name)}(:|,|\s|$)/i
end
issues_for_formula(query, tap_full_name: tap_full_name, state: state).select do |pr|
pr["html_url"].include?("/pull/") &&
/(^|\s)#{Regexp.quote(query)}(:|\s|$)/i =~ pr["title"]
pr["html_url"].include?("/pull/") && regex.match?(pr["title"])
end
rescue RateLimitExceededError => e
opoo e.message
[]
end

def check_for_duplicate_pull_requests(query, tap_full_name, state:, file:, args:)
pull_requests = fetch_pull_requests(query, tap_full_name, state: state)
pull_requests.select! do |pr|
def check_for_duplicate_pull_requests(name, tap_full_name, state:, file:, args:, version: nil)
pull_requests = fetch_pull_requests(name, tap_full_name, state: state, version: version).select do |pr|
pr_files = open_api(url_to("repos", tap_full_name, "pulls", pr["number"], "files"))
pr_files.any? { |f| f["filename"] == file }
end
Expand Down

0 comments on commit cded623

Please sign in to comment.