Skip to content

Commit 6e88ffd

Browse files
authored
Merge pull request #21505 from Homebrew/livecheck/expand-cached-content-support
livecheck: add content parameter to strategies, expand test coverage
2 parents 589bfc7 + 0a72b1a commit 6e88ffd

46 files changed

Lines changed: 1476 additions & 467 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Library/Homebrew/livecheck/strategic.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ def match?(url); end
2222
#
2323
# @param url the URL of the content to check
2424
# @param regex a regex for matching versions in content
25-
# @param provided_content content to check instead of
26-
# fetching
25+
# @param content content to check instead of fetching
2726
# @param options options to modify behavior
2827
# @param block a block to match the content
2928
sig {
3029
abstract.params(
31-
url: String,
32-
regex: T.nilable(Regexp),
33-
provided_content: T.nilable(String),
34-
options: Options,
35-
block: T.nilable(Proc),
30+
url: String,
31+
regex: T.nilable(Regexp),
32+
content: T.nilable(String),
33+
options: Options,
34+
block: T.nilable(Proc),
3635
).returns(T::Hash[Symbol, T.anything])
3736
}
38-
def find_versions(url:, regex: nil, provided_content: nil, options: Options.new, &block); end
37+
def find_versions(url:, regex: nil, content: nil, options: Options.new, &block); end
3938
end
4039
end
4140
end

Library/Homebrew/livecheck/strategy/apache.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def self.generate_input_values(url)
7070
regex_prefix = Regexp.escape(match[:prefix] || "").gsub("\\-", "-")
7171

7272
# Use `\.t` instead of specific tarball extensions (e.g. .tar.gz)
73-
suffix = match[:suffix]&.sub(Strategy::TARBALL_EXTENSION_REGEX, ".t")
74-
regex_suffix = Regexp.escape(suffix || "").gsub("\\-", "-")
73+
suffix = T.must(match[:suffix]).sub(Strategy::TARBALL_EXTENSION_REGEX, ".t")
74+
regex_suffix = Regexp.escape(suffix).gsub("\\-", "-")
7575

7676
# Example directory regex: `%r{href=["']?v?(\d+(?:\.\d+)+)/}i`
7777
# Example file regexes:
@@ -86,23 +86,26 @@ def self.generate_input_values(url)
8686
# to {PageMatch.find_versions} to identify versions in the content.
8787
#
8888
# @param url [String] the URL of the content to check
89-
# @param regex [Regexp] a regex used for matching versions in content
89+
# @param regex [Regexp, nil] a regex for matching versions in content
90+
# @param content [String, nil] content to check instead of fetching
9091
# @param options [Options] options to modify behavior
9192
# @return [Hash]
9293
sig {
93-
override(allow_incompatible: true).params(
94+
override.params(
9495
url: String,
9596
regex: T.nilable(Regexp),
97+
content: T.nilable(String),
9698
options: Options,
9799
block: T.nilable(Proc),
98100
).returns(T::Hash[Symbol, T.anything])
99101
}
100-
def self.find_versions(url:, regex: nil, options: Options.new, &block)
102+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
101103
generated = generate_input_values(url)
102104

103105
PageMatch.find_versions(
104106
url: generated[:url],
105107
regex: regex || generated[:regex],
108+
content:,
106109
options:,
107110
&block
108111
)

Library/Homebrew/livecheck/strategy/bitbucket.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,26 @@ def self.generate_input_values(url)
9494
# to {PageMatch.find_versions} to identify versions in the content.
9595
#
9696
# @param url [String] the URL of the content to check
97-
# @param regex [Regexp] a regex used for matching versions in content
97+
# @param regex [Regexp, nil] a regex for matching versions in content
98+
# @param content [String, nil] content to check instead of fetching
9899
# @param options [Options] options to modify behavior
99100
# @return [Hash]
100101
sig {
101-
override(allow_incompatible: true).params(
102+
override.params(
102103
url: String,
103104
regex: T.nilable(Regexp),
105+
content: T.nilable(String),
104106
options: Options,
105107
block: T.nilable(Proc),
106108
).returns(T::Hash[Symbol, T.anything])
107109
}
108-
def self.find_versions(url:, regex: nil, options: Options.new, &block)
110+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
109111
generated = generate_input_values(url)
110112

111113
PageMatch.find_versions(
112114
url: generated[:url],
113115
regex: regex || generated[:regex],
116+
content:,
114117
options:,
115118
&block
116119
)

Library/Homebrew/livecheck/strategy/cpan.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,26 @@ def self.generate_input_values(url)
7373
# to {PageMatch.find_versions} to identify versions in the content.
7474
#
7575
# @param url [String] the URL of the content to check
76-
# @param regex [Regexp] a regex used for matching versions in content
76+
# @param regex [Regexp, nil] a regex for matching versions in content
77+
# @param content [String, nil] content to check instead of fetching
7778
# @param options [Options] options to modify behavior
7879
# @return [Hash]
7980
sig {
80-
override(allow_incompatible: true).params(
81+
override.params(
8182
url: String,
8283
regex: T.nilable(Regexp),
84+
content: T.nilable(String),
8385
options: Options,
8486
block: T.nilable(Proc),
8587
).returns(T::Hash[Symbol, T.anything])
8688
}
87-
def self.find_versions(url:, regex: nil, options: Options.new, &block)
89+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
8890
generated = generate_input_values(url)
8991

9092
PageMatch.find_versions(
9193
url: generated[:url],
9294
regex: regex || generated[:regex],
95+
content:,
9396
options:,
9497
&block
9598
)

Library/Homebrew/livecheck/strategy/crate.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,32 @@ def self.generate_input_values(url)
7373
#
7474
# @param url [String] the URL of the content to check
7575
# @param regex [Regexp, nil] a regex for matching versions in content
76-
# @param provided_content [String, nil] content to check instead of
77-
# fetching
76+
# @param content [String, nil] content to check instead of fetching
7877
# @param options [Options] options to modify behavior
7978
# @return [Hash]
8079
sig {
8180
override.params(
82-
url: String,
83-
regex: T.nilable(Regexp),
84-
provided_content: T.nilable(String),
85-
options: Options,
86-
block: T.nilable(Proc),
81+
url: String,
82+
regex: T.nilable(Regexp),
83+
content: T.nilable(String),
84+
options: Options,
85+
block: T.nilable(Proc),
8786
).returns(T::Hash[Symbol, T.anything])
8887
}
89-
def self.find_versions(url:, regex: nil, provided_content: nil, options: Options.new, &block)
88+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
9089
match_data = { matches: {}, regex:, url: }
91-
match_data[:cached] = true if provided_content.is_a?(String)
90+
match_data[:cached] = true if content
9291

9392
generated = generate_input_values(url)
9493
return match_data if generated.blank?
9594

9695
match_data[:url] = generated[:url]
9796

98-
content = if provided_content
99-
provided_content
100-
else
97+
unless match_data[:cached]
10198
match_data.merge!(Strategy.page_content(match_data[:url], options:))
102-
match_data[:content]
99+
content = match_data[:content]
103100
end
104-
return match_data unless content
101+
return match_data if content.blank?
105102

106103
Json.versions_from_content(content, regex || DEFAULT_REGEX, &block || DEFAULT_BLOCK).each do |match_text|
107104
match_data[:matches][match_text] = Version.new(match_text)

Library/Homebrew/livecheck/strategy/electron_builder.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ def self.match?(url)
3434
# Checks the YAML content at the URL for new versions.
3535
#
3636
# @param url [String] the URL of the content to check
37-
# @param regex [Regexp, nil] a regex used for matching versions
38-
# @param provided_content [String, nil] content to use in place of
39-
# fetching via `Strategy#page_content`
37+
# @param regex [Regexp, nil] a regex for matching versions in content
38+
# @param content [String, nil] content to check instead of fetching
4039
# @param options [Options] options to modify behavior
4140
# @return [Hash]
4241
sig {
4342
override.params(
44-
url: String,
45-
regex: T.nilable(Regexp),
46-
provided_content: T.nilable(String),
47-
options: Options,
48-
block: T.nilable(Proc),
43+
url: String,
44+
regex: T.nilable(Regexp),
45+
content: T.nilable(String),
46+
options: Options,
47+
block: T.nilable(Proc),
4948
).returns(T::Hash[Symbol, T.anything])
5049
}
51-
def self.find_versions(url:, regex: nil, provided_content: nil, options: Options.new, &block)
50+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
5251
if regex.present? && !block_given?
5352
raise ArgumentError,
5453
"#{Utils.demodulize(name)} only supports a regex when using a `strategy` block"
@@ -57,7 +56,7 @@ def self.find_versions(url:, regex: nil, provided_content: nil, options: Options
5756
Yaml.find_versions(
5857
url:,
5958
regex:,
60-
provided_content:,
59+
content:,
6160
options:,
6261
&block || proc { |yaml| yaml["version"] }
6362
)

Library/Homebrew/livecheck/strategy/extract_plist.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ def self.find_versions(cask:, url: nil, regex: nil, content: nil, options: Optio
162162
end
163163

164164
match_data = { matches: {}, regex:, url: }
165-
match_data[:cached] = true if content
166165

167-
if match_data[:cached]
168-
items = Json.parse_json(T.must(content)).transform_values do |obj|
166+
items = if content
167+
match_data[:cached] = true
168+
Json.parse_json(content).transform_values do |obj|
169169
short_version = obj.dig("bundle_version", "short_version")
170170
version = obj.dig("bundle_version", "version")
171171
Item.new(bundle_version: BundleVersion.new(short_version, version))
@@ -177,7 +177,7 @@ def self.find_versions(cask:, url: nil, regex: nil, content: nil, options: Optio
177177
UnversionedCaskChecker.new(cask)
178178
end
179179

180-
items = unversioned_cask_checker.all_versions.transform_values { |v| Item.new(bundle_version: v) }
180+
unversioned_cask_checker.all_versions.transform_values { |v| Item.new(bundle_version: v) }
181181
end
182182
return match_data if items.blank?
183183

Library/Homebrew/livecheck/strategy/git.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def self.tags_from_content(content)
153153
# group around the version text.
154154
#
155155
# @param content [String] the content to check
156-
# @param regex [Regexp, nil] a regex to identify versions
156+
# @param regex [Regexp, nil] a regex for matching versions in content
157157
# @return [Array]
158158
sig {
159159
params(
@@ -187,29 +187,26 @@ def self.versions_from_content(content, regex = nil, &block)
187187
#
188188
# @param url [String] the URL of the Git repository to check
189189
# @param regex [Regexp, nil] a regex for matching versions in content
190-
# @param provided_content [String, nil] content to check instead of
191-
# fetching
190+
# @param content [String, nil] content to check instead of fetching
192191
# @param options [Options] options to modify behavior
193192
# @return [Hash]
194193
sig {
195194
override.params(
196-
url: String,
197-
regex: T.nilable(Regexp),
198-
provided_content: T.nilable(String),
199-
options: Options,
200-
block: T.nilable(Proc),
195+
url: String,
196+
regex: T.nilable(Regexp),
197+
content: T.nilable(String),
198+
options: Options,
199+
block: T.nilable(Proc),
201200
).returns(T::Hash[Symbol, T.anything])
202201
}
203-
def self.find_versions(url:, regex: nil, provided_content: nil, options: Options.new, &block)
202+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
204203
match_data = { matches: {}, regex:, url: }
204+
match_data[:cached] = true if content
205205
return match_data if url.blank?
206206

207-
content = if provided_content.is_a?(String)
208-
match_data[:cached] = true
209-
provided_content
210-
else
207+
unless match_data[:cached]
211208
match_data.merge!(ls_remote_tags(url))
212-
match_data[:content]
209+
content = match_data[:content]
213210
end
214211
return match_data if content.blank?
215212

Library/Homebrew/livecheck/strategy/github_latest.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def self.generate_input_values(url)
6262
match = url.delete_suffix(".git").match(GithubReleases::URL_MATCH_REGEX)
6363
return values if match.blank?
6464

65-
values[:url] = "https://api.github.com/repos/#{match[:username]}/#{match[:repository]}/releases/latest"
65+
values[:url] = "#{GitHub::API_URL}/repos/#{match[:username]}/#{match[:repository]}/releases/latest"
6666
values[:username] = match[:username]
6767
values[:repository] = match[:repository]
6868

@@ -73,27 +73,36 @@ def self.generate_input_values(url)
7373
# and identifies the version from the JSON response.
7474
#
7575
# @param url [String] the URL of the content to check
76-
# @param regex [Regexp] a regex used for matching versions in content
76+
# @param regex [Regexp] a regex for matching versions in content
77+
# @param content [Hash, nil] content to check instead of fetching
7778
# @param options [Options] options to modify behavior
7879
# @return [Hash]
7980
sig {
80-
override(allow_incompatible: true).params(
81+
override.params(
8182
url: String,
82-
regex: Regexp,
83+
regex: T.nilable(Regexp),
84+
content: T.nilable(String),
8385
options: Options,
8486
block: T.nilable(Proc),
8587
).returns(T::Hash[Symbol, T.anything])
8688
}
87-
def self.find_versions(url:, regex: GithubReleases::DEFAULT_REGEX, options: Options.new, &block)
89+
def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
90+
regex ||= GithubReleases::DEFAULT_REGEX
8891
match_data = { matches: {}, regex:, url: }
92+
match_data[:cached] = true if content
8993

9094
generated = generate_input_values(url)
9195
return match_data if generated.blank?
9296

9397
match_data[:url] = generated[:url]
9498

95-
release = GitHub.get_latest_release(generated[:username], generated[:repository])
96-
GithubReleases.versions_from_content(release, regex, &block).each do |match_text|
99+
unless match_data[:cached]
100+
match_data[:content] = GitHub::API.open_rest(generated[:url], parse_json: false)
101+
content = match_data[:content]
102+
end
103+
return match_data if content.blank?
104+
105+
GithubReleases.versions_from_content(content, regex, &block).each do |match_text|
97106
match_data[:matches][match_text] = Version.new(match_text)
98107
end
99108

0 commit comments

Comments
 (0)