Skip to content

Commit

Permalink
livecheck: support throttle DSL
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Cho <[email protected]>
  • Loading branch information
cho-m committed Mar 19, 2024
1 parent e3797d3 commit 7ff01d8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions Library/Homebrew/dev-cmd/bump-formula-pr.rb
Expand Up @@ -472,6 +472,7 @@ def check_new_version(formula, tap_remote_repo, args:, version: nil, url: nil, t

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

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#L475

Added line #L475 was not covered by tests
return if throttled_rate.blank?

formula_suffix = Version.new(new_version).patch.to_i
Expand Down
27 changes: 19 additions & 8 deletions Library/Homebrew/dev-cmd/bump.rb
Expand Up @@ -252,7 +252,9 @@ def skip_ineligible_formulae(formula_or_cask)
end

sig {
params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T.any(Version, String))
params(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L255 was not covered by tests
formula_or_cask: T.any(Formula, Cask::Cask),
).returns([T.any(Version, String), T.nilable(T.any(Version, String))])
}
def livecheck_result(formula_or_cask)
name = Livecheck.package_or_resource_name(formula_or_cask)
Expand Down Expand Up @@ -280,21 +282,28 @@ def livecheck_result(formula_or_cask)
)

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?}", nil
end

version_info = Livecheck.latest_version(
formula_or_cask,
referenced_formula_or_cask:,
json: true, full_name: false, verbose: true, debug: false
)
return "unable to get versions" if version_info.blank?
return "unable to get versions", nil if version_info.blank?

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/bump.rb#L295-L296

Added lines #L295 - L296 were not covered by tests
nil
elsif version_info[:latest_throttled].nil?
"unable to get throttled versions"
else
Version.new(version_info[:latest_throttled])
end

Version.new(latest)
[latest, latest_throttled]

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L304 was not covered by tests
rescue => e
"error: #{e}"
["error: #{e}", nil]

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L306 was not covered by tests
end

sig {
Expand Down Expand Up @@ -350,7 +359,9 @@ def retrieve_versions_by_arch(formula_or_cask:, repositories:, args:, name:)
current_version_value = Version.new(loaded_formula_or_cask.version)
end

livecheck_latest = livecheck_result(loaded_formula_or_cask)
livecheck_latest, livecheck_latest_throttled = livecheck_result(loaded_formula_or_cask)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L362 was not covered by tests
# TODO: Pass down `livecheck_latest` info to print output for throttled formulae or casks
livecheck_latest = livecheck_latest_throttled if livecheck_latest_throttled

new_version_value = if (livecheck_latest.is_a?(Version) && livecheck_latest >= current_version_value) ||
current_version_value == "latest"
Expand Down Expand Up @@ -478,7 +489,7 @@ def retrieve_and_display_info_and_open_pr(formula_or_cask, name, repositories, a
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, args:)
Latest Repology version: #{repology_latest}
Expand Down
19 changes: 19 additions & 0 deletions Library/Homebrew/livecheck.rb
Expand Up @@ -25,6 +25,7 @@ def initialize(package_or_resource)
@referenced_cask_name = nil
@referenced_formula_name = nil
@regex = nil
@throttle = nil
@skip = false
@skip_msg = nil
@strategy = nil
Expand Down Expand Up @@ -87,6 +88,23 @@ def regex(pattern = T.unsafe(nil))
end
end

# 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: T.nilable(Integer),
).returns(T.nilable(Integer))
}
def throttle(rate = T.unsafe(nil))
case rate
when nil
@throttle
when Integer
@throttle = rate
end
end

# Sets the `@skip` instance variable to `true` and sets the `@skip_msg`
# instance variable if a `String` is provided. `@skip` is used to indicate
# that the formula/cask/resource should be skipped and the `skip_msg` very
Expand Down Expand Up @@ -168,6 +186,7 @@ def to_hash
"cask" => @referenced_cask_name,
"formula" => @referenced_formula_name,
"regex" => @regex,
"throttle" => @throttle,
"skip" => @skip,
"skip_msg" => @skip_msg,
"strategy" => @strategy,
Expand Down
22 changes: 22 additions & 0 deletions Library/Homebrew/livecheck/livecheck.rb
Expand Up @@ -810,6 +810,28 @@ def latest_version(
latest: Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) }),
}

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L815

Added line #L815 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 823 in Library/Homebrew/livecheck/livecheck.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L823

Added line #L823 was not covered by tests

if verbose

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L825

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

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

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/livecheck/livecheck.rb#L827

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

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

Expand Down
12 changes: 12 additions & 0 deletions Library/Homebrew/test/livecheck_spec.rb
Expand Up @@ -66,6 +66,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 "#skip" do
it "sets @skip to true when no argument is provided" do
expect(livecheckable_f.skip).to be true
Expand Down Expand Up @@ -140,6 +151,7 @@
"cask" => nil,
"formula" => nil,
"regex" => nil,
"throttle" => nil,
"skip" => false,
"skip_msg" => nil,
"strategy" => nil,
Expand Down

0 comments on commit 7ff01d8

Please sign in to comment.