Class: Homebrew::Livecheck::Strategy::GithubReleases

Inherits:
Object
  • Object
show all
Extended by:
Cacheable, Homebrew::Livecheck::Strategic, T::Generic
Defined in:
livecheck/strategy/github_releases.rb

Overview

The GithubReleases strategy identifies versions of software at github.com by checking a repository's recent releases 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.gz
  • https://github.com/example/example/archive/v1.2.3.tar.gz
  • https://github.com/downloads/example/example/example-1.2.3.tar.gz

GithubReleases should only be used when the upstream repository has releases for suitable versions and the strategy is necessary or appropriate (e.g. the formula/cask uses a release asset and the GithubLatest strategy isn't sufficient to identify the newest version. The strategy can only be applied by using strategy :github_releases in a livecheck block.

The default regex identifies versions like 1.2.3/v1.2.3 in each 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

Cache =
type_template { { fixed: T::Hash[Symbol, T::Boolean] } }
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::GithubReleases so we can selectively apply the strategy using strategy :github_releases in a livecheck block.

0
URL_MATCH_REGEX =

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.

The Regexp used to determine if the strategy applies to the URL.

%r{
  ^https?://github\.com
  /(?:downloads/)?(?<username>[^/]+) # The GitHub username
  /(?<repository>[^/]+)              # The GitHub repository name
}ix
DEFAULT_REGEX =

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.

The default regex used to identify a version from a tag when a regex isn't provided.

/v?(\d+(?:\.\d+)+)/i
RELEASE_TAGS_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 recent release tags and their draft/pre-release status, which can be used for basic checks without a strategy block. This is intended to align with the REST API's pagination behaviour.

<<~GRAPHQL
  query($owner: String!, $name: String!) {
    repository(owner: $owner, name: $name) {
      releases(first: 30, orderBy: { field: CREATED_AT, direction: DESC }) {
        nodes { tagName isDraft isPrerelease }
      }
    }
  }
GRAPHQL

Class Method Summary collapse

Methods included from Cacheable

cache, clear_cache

Methods included from Homebrew::Livecheck::Strategic

find_versions, match?

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 recent releases and identifies versions from the JSON response.

Parameters:

  • url (String) —

    the URL of the content to check

  • regex (Regexp, nil) (defaults to: nil) —

    a regex for matching versions in content

  • content (String, nil) (defaults to: nil) —

    content to check instead of fetching

  • options (Options) (defaults to: Options.new) —

    options to modify behavior

  • block (Proc, nil)

Returns:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'livecheck/strategy/github_releases.rb', line 175

def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
  regex ||= DEFAULT_REGEX
  match_data = { matches: {}, regex:, url: }
  match_data[:cached] = true if content

  fetch_type =
    if block_given? || !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
      releases = GitHub::API.open_graphql(
        RELEASE_TAGS_QUERY,
        variables: {
          owner: generated[:username],
          name:  generated[:repository],
        },
      )&.dig("repository", "releases", "nodes")
      if releases.present?
        rest_releases = releases.compact.map do |release|
          {
            tag_name:   release["tagName"],
            draft:      release["isDraft"],
            prerelease: release["isPrerelease"],
          }
        end
        "#{JSON.pretty_generate(rest_releases)}\n"
      end
    else
      GitHub::API.open_rest(generated[:url], parse_json: false)
    end
    content = match_data[:content]
  end
  return match_data if content.blank?

  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.

Parameters:

  • url (String) —

    the URL used to generate values

  • fetch_type (Symbol) (defaults to: :rest_api) —

    the type of GitHub data to fetch

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'livecheck/strategy/github_releases.rb', line 105

def self.generate_input_values(url, fetch_type: :rest_api)
  values = {}

  match = url.delete_suffix(".git").match(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"
  else
    raise ArgumentError, "Unrecognized `fetch_type` argument"
  end
  values[:username] = match[:username]
  values[:repository] = match[:repository]

  values
end

.github_credentials? ⇒ 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 GitHub credentials are available, which are strictly required to use the GraphQL API.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
# File 'livecheck/strategy/github_releases.rb', line 86

def self.github_credentials?
  # `GitHub::API.credentials` doesn't memoise a `nil` value (when no
  # credentials are available), so it will re-check for credentials
  # the next time the method is called. We cache a value here to avoid
  # repeating this work within the GitHub strategies.
  cache.fetch(:github_credentials) do
    cache[:github_credentials] = GitHub::API.credentials.present?
  end
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.

Parameters:

  • url (String) —

    the URL to match against

Returns:

  • (Boolean)


76
77
78
# File 'livecheck/strategy/github_releases.rb', line 76

def self.match?(url)
  URL_MATCH_REGEX.match?(url)
end

.versions_from_content(content, regex, &block) ⇒ Array<String>

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.

Uses a regex to match versions from release JSON or, if a block is provided, passes the JSON to the block to handle matching. With either approach, an array of unique matches is returned.

Parameters:

  • content (String) —

    an array of releases or a single release

  • regex (Regexp) —

    a regex for matching versions in content

  • block (Proc, nil)

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'livecheck/strategy/github_releases.rb', line 139

def self.versions_from_content(content, regex, &block)
  return [] if content.blank?

  json = Json.parse_json(content)
  return [] if json.blank?

  if block.present?
    block_return_value = yield(json, regex)
    return Strategy.handle_block_return(block_return_value)
  end

  json = [json] unless json.is_a?(Array)
  json.compact_blank.filter_map do |release|
    next if release["draft"] || release["prerelease"]

    release["tag_name"]&.[](regex, 1)
  end.uniq
end