Skip to content

Commit

Permalink
feat: use API for GitHub latest release strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Apr 22, 2023
1 parent 302b224 commit 49501c2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
63 changes: 56 additions & 7 deletions Library/Homebrew/livecheck/strategy/github_latest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class GithubLatest

# The default regex used to identify a version from a tag when a regex
# isn't provided.
DEFAULT_REGEX = %r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i.freeze
DEFAULT_REGEX = /v?(\d+(?:\.\d+)+)/i.freeze

# Keys in the JSON that could contain the version.
VERSION_KEYS = ["name", "tag_name"].freeze

# Whether the strategy can be applied to the provided URL.
#
Expand All @@ -61,6 +64,42 @@ def self.match?(url)
URL_MATCH_REGEX.match?(url)
end

# Uses the regex to match release information in content or, if a block is
# provided, passes the page content to the block to handle matching.
# With either approach, an array of unique matches is returned.
#
# @param content [Array] list of releases
# @param regex [Regexp] a regex used for matching versions in the content
# @param block [Proc, nil] a block to match the content
# @return [Array]
sig {
params(
content: T::Array[T::Hash[String, T.untyped]],
regex: Regexp,
block: T.nilable(Proc),
).returns(T::Array[String])
}
def self.versions_from_content(content, regex, &block)
if block.present?
block_return_value = if regex.present?
yield(content, regex)
else
yield(content)
end
return Strategy.handle_block_return(block_return_value)
end

content.reject(&:blank?).map do |release|
next if release["draft"] || release["prerelease"]

value = T.let(nil, T.untyped)
VERSION_KEYS.find do |key|
value = release[key]&.match(regex)&.to_s
end
value
end.compact.uniq
end

# Extracts information from a provided URL and uses it to generate
# various input values used by the strategy to check for new versions.
# Some of these values act as defaults and can be overridden in a
Expand All @@ -77,6 +116,8 @@ def self.generate_input_values(url)

# Example URL: `https://github.com/example/example/releases/latest`
values[:url] = "https://github.com/#{match[:username]}/#{match[:repository]}/releases/latest"
values[:username] = match[:username]
values[:repository] = match[:repository]

values
end
Expand All @@ -89,18 +130,26 @@ def self.generate_input_values(url)
# @return [Hash]
sig {
params(
url: String,
regex: T.nilable(Regexp),
unused: T.nilable(T::Hash[Symbol, T.untyped]),
block: T.nilable(Proc),
url: String,
regex: Regexp,
_unused: T.nilable(T::Hash[Symbol, T.untyped]),
block: T.nilable(Proc),
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url:, regex: nil, **unused, &block)
def self.find_versions(url:, regex: DEFAULT_REGEX, **_unused, &block)
match_data = { matches: {}, regex: regex }
generated = generate_input_values(url)
return match_data if generated.blank?

release = GitHub.get_latest_release(generated[:username], generated[:repository])
versions_from_content([release], regex, &block).each do |match_text|
match_data[:matches][match_text] = Version.new(match_text)
end

PageMatch.find_versions(url: generated[:url], regex: regex || DEFAULT_REGEX, **unused, &block)
match_data
end
end
end
GitHubLatest = Homebrew::Livecheck::Strategy::GithubLatest
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

let(:generated) do
{
url: "https://github.com/abc/def/releases/latest",
username: "abc",
url: "https://github.com/abc/def/releases/latest",
repository: "def",
}
end

Expand Down

0 comments on commit 49501c2

Please sign in to comment.