Class: Homebrew::Livecheck::Strategy::GithubLatest
- Extended by:
- Homebrew::Livecheck::Strategic
- Defined in:
- livecheck/strategy/github_latest.rb
Overview
The GithubLatest strategy identifies versions of software at github.com by checking a repository's "latest" release using the GitHub API.
GitHub URLs take a few different formats:
https://github.com/example/example/releases/download/1.2.3/example-1.2.3.tar.gzhttps://github.com/example/example/archive/v1.2.3.tar.gzhttps://github.com/downloads/example/example/example-1.2.3.tar.gz
GithubLatest should only be used when the upstream repository has a
"latest" release for a suitable version and Git can't adequately
identify the latest version (e.g. tags using a stable version format are
marked as pre-release, not every tag receives a release, etc.) or
there's a notable time gap between a tag and release. The strategy can
only be applied by using strategy :github_latest in a livecheck
block.
The default regex identifies versions like 1.2.3/v1.2.3 in a
release's tag or title. This is a common tag format but a modified
regex can be provided in a livecheck block to override the default
if a repository uses a different format (e.g. 1.2.3d, 1.2.3-4,
etc.).
Constant Summary collapse
- PRIORITY =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
A priority of zero causes livecheck to skip the strategy. We do this for Homebrew::Livecheck::Strategy::GithubLatest so we can selectively apply the strategy using
strategy :github_latestin alivecheckblock. 0- LATEST_RELEASE_TAG_QUERY =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
A GraphQL query to fetch a repository's "latest" release tag, which can be used for basic checks without a
strategyblock. <<~GRAPHQL query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { latestRelease { tagName } } } GRAPHQL
Class Method Summary collapse
-
.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) ⇒ Hash{Symbol => T.anything}
private
Generates the GitHub API URL for the repository's "latest" release and identifies the version from the JSON response.
-
.generate_input_values(url, fetch_type: :rest_api) ⇒ Hash{Symbol => T.untyped}
private
Extracts information from a provided URL and uses it to generate various input values used by the strategy to check for new versions.
-
.match?(url) ⇒ Boolean
private
Whether the strategy can be applied to the provided URL.
Methods included from Homebrew::Livecheck::Strategic
Class Method Details
.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) ⇒ Hash{Symbol => T.anything}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Generates the GitHub API URL for the repository's "latest" release and identifies the version from the JSON response.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'livecheck/strategy/github_latest.rb', line 107 def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) regex ||= GithubReleases::DEFAULT_REGEX match_data = { matches: {}, regex:, url: } match_data[:cached] = true if content fetch_type = if block_given? || !GithubReleases.github_credentials? :rest_api else :graphql_api end generated = generate_input_values(url, fetch_type:) return match_data if generated.blank? match_data[:url] = generated[:url] unless match_data[:cached] if Homebrew::EnvConfig.no_github_api? raise "#{Utils.demodulize(name)} cannot be used when `HOMEBREW_NO_GITHUB_API` is set" end match_data[:content] = if fetch_type == :graphql_api # Use the GraphQL API to fetch minimal data and structure the JSON # to match REST API responses tag_name = GitHub::API.open_graphql( LATEST_RELEASE_TAG_QUERY, variables: { owner: generated[:username], name: generated[:repository], }, )&.dig("repository", "latestRelease", "tagName") "#{JSON.pretty_generate({ tag_name: })}\n" if tag_name else GitHub::API.open_rest(generated[:url], parse_json: false) end content = match_data[:content] end return match_data if content.blank? GithubReleases.versions_from_content(content, regex, &block).each do |match_text| match_data[:matches][match_text] = Version.new(match_text) end match_data end |
.generate_input_values(url, fetch_type: :rest_api) ⇒ Hash{Symbol => T.untyped}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
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
livecheck block.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'livecheck/strategy/github_latest.rb', line 70 def self.generate_input_values(url, fetch_type: :rest_api) values = {} match = url.delete_suffix(".git").match(GithubReleases::URL_MATCH_REGEX) return values if match.blank? values[:url] = case fetch_type when :graphql_api "#{GitHub::API_URL}/graphql" when :rest_api "#{GitHub::API_URL}/repos/#{match[:username]}/#{match[:repository]}/releases/latest" else raise ArgumentError, "Unrecognized `fetch_type` argument" end values[:username] = match[:username] values[:repository] = match[:repository] values end |
.match?(url) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Whether the strategy can be applied to the provided URL.
57 58 59 |
# File 'livecheck/strategy/github_latest.rb', line 57 def self.match?(url) GithubReleases.match?(url) end |