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

livecheck: support throttle DSL #16918

Merged
merged 5 commits into from Mar 21, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Library/Homebrew/dev-cmd/bump-formula-pr.rb
Expand Up @@ -471,7 +471,8 @@
end

def check_throttle(formula, new_version)
throttled_rate = formula.tap.audit_exceptions.dig(:throttled_formulae, formula.name)
throttled_rate = formula.livecheck.throttle
throttled_rate ||= formula.tap.audit_exceptions.dig(:throttled_formulae, formula.name)

Check warning on line 475 in Library/Homebrew/dev-cmd/bump-formula-pr.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump-formula-pr.rb#L474-L475

Added lines #L474 - L475 were not covered by tests
return if throttled_rate.blank?

formula_suffix = Version.new(new_version).patch.to_i
Expand Down
15 changes: 10 additions & 5 deletions Library/Homebrew/dev-cmd/bump.rb
Expand Up @@ -276,7 +276,8 @@
)

if skip_info.present?
return "#{skip_info[:status]}#{" - #{skip_info[:messages].join(", ")}" if skip_info[:messages].present?}"
return "#{skip_info[:status]}" \
"#{" - #{skip_info[:messages].join(", ")}" if skip_info[:messages].present?}"
end

version_info = Livecheck.latest_version(
Expand All @@ -286,9 +287,13 @@
)
return "unable to get versions" if version_info.blank?

latest = version_info[:latest]

Version.new(latest)
if !version_info.key?(:latest_throttled)

Check warning on line 290 in Library/Homebrew/dev-cmd/bump.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump.rb#L290

Added line #L290 was not covered by tests
Version.new(version_info[:latest])
elsif version_info[:latest_throttled].nil?
"unable to get throttled versions"
else
Version.new(version_info[:latest_throttled])
end
rescue => e
"error: #{e}"
end
Expand Down Expand Up @@ -475,7 +480,7 @@
ohai title
puts <<~EOS
Current #{version_label} #{current_versions}
Latest livecheck version: #{new_versions}
Latest livecheck version: #{new_versions}#{" (throttled)" if formula_or_cask.livecheck.throttle}
EOS
puts <<~EOS unless skip_repology?(formula_or_cask)
Latest Repology version: #{repology_latest}
Expand Down
19 changes: 19 additions & 0 deletions Library/Homebrew/livecheck.rb
Expand Up @@ -29,6 +29,7 @@ def initialize(package_or_resource)
@skip_msg = nil
@strategy = nil
@strategy_block = nil
@throttle = nil
@url = nil
end

Expand Down Expand Up @@ -135,6 +136,23 @@ def strategy(symbol = T.unsafe(nil), &block)
sig { returns(T.nilable(Proc)) }
attr_reader :strategy_block

# Sets the `@throttle` instance variable to the provided `Integer` or returns
# the `@throttle` instance variable when no argument is provided.
sig {
params(
# Throttle rate of version patch number to use for bumpable versions.
rate: Integer,
).returns(T.nilable(Integer))
}
def throttle(rate = T.unsafe(nil))
case rate
when nil
@throttle
when Integer
@throttle = rate
end
end

# Sets the `@url` instance variable to the provided argument or returns the
# `@url` instance variable when no argument is provided. The argument can be
# a `String` (a URL) or a supported `Symbol` corresponding to a URL in the
Expand Down Expand Up @@ -171,6 +189,7 @@ def to_hash
"skip" => @skip,
"skip_msg" => @skip_msg,
"strategy" => @strategy,
"throttle" => @throttle,
"url" => @url,
}
end
Expand Down
28 changes: 27 additions & 1 deletion Library/Homebrew/livecheck/livecheck.rb
Expand Up @@ -372,9 +372,10 @@
info[:version] = {
current: current_str,
latest: latest_str,
latest_throttled: version_info&.dig(:latest_throttled),
outdated: is_outdated,
newer_than_upstream: is_newer_than_upstream,
}
}.compact
info[:meta] = {
livecheckable: formula_or_cask.livecheckable?,
}
Expand Down Expand Up @@ -678,6 +679,7 @@
livecheck_regex = livecheck.regex || referenced_livecheck&.regex
livecheck_strategy = livecheck.strategy || referenced_livecheck&.strategy
livecheck_strategy_block = livecheck.strategy_block || referenced_livecheck&.strategy_block
livecheck_throttle = livecheck.throttle || referenced_livecheck&.throttle

livecheck_url_string = livecheck_url_to_string(
livecheck_url,
Expand All @@ -695,6 +697,7 @@
puts "Cask: #{cask_name(formula_or_cask, full_name:)}"
end
puts "Livecheckable?: #{has_livecheckable ? "Yes" : "No"}"
puts "Throttle: #{livecheck_throttle}" if livecheck_throttle

livecheck_references.each do |ref_formula_or_cask|
case ref_formula_or_cask
Expand Down Expand Up @@ -826,6 +829,28 @@
latest: Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) }),
}

if livecheck_throttle
match_version_map.keep_if { |_match, version| version.patch.to_i.modulo(livecheck_throttle).zero? }
version_info[:latest_throttled] = if match_version_map.blank?

Check warning on line 834 in Library/Homebrew/livecheck/livecheck.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L834

Added line #L834 was not covered by tests
nil
else
Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) })
end

if debug
puts
puts "Matched Throttled Versions:"

Check warning on line 842 in Library/Homebrew/livecheck/livecheck.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L842

Added line #L842 was not covered by tests

if verbose

Check warning on line 844 in Library/Homebrew/livecheck/livecheck.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L844

Added line #L844 was not covered by tests
match_version_map.each do |match, version|
puts "#{match} => #{version.inspect}"

Check warning on line 846 in Library/Homebrew/livecheck/livecheck.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L846

Added line #L846 was not covered by tests
end
else
puts match_version_map.values.join(", ")
end
end
end

if json && verbose
version_info[:meta] = {}

Expand Down Expand Up @@ -854,6 +879,7 @@
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
version_info[:meta][:regex] = regex.inspect if regex.present?
version_info[:meta][:cached] = true if strategy_data[:cached] == true
version_info[:meta][:throttle] = livecheck_throttle if livecheck_throttle
end

return version_info
Expand Down
12 changes: 12 additions & 0 deletions Library/Homebrew/test/livecheck_spec.rb
Expand Up @@ -100,6 +100,17 @@
end
end

describe "#throttle" do
it "returns nil if not set" do
expect(livecheckable_f.throttle).to be_nil
end

it "returns the Integer if set" do
livecheckable_f.throttle(10)
expect(livecheckable_f.throttle).to eq(10)
end
end

describe "#url" do
let(:url_string) { "https://brew.sh" }

Expand Down Expand Up @@ -143,6 +154,7 @@
"skip" => false,
"skip_msg" => nil,
"strategy" => nil,
"throttle" => nil,
"url" => nil,
},
)
Expand Down