Class: Homebrew::Livecheck::Strategy::GithubLatest

Inherits:
Object
  • Object
show all
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.gz
  • https://github.com/example/example/archive/v1.2.3.tar.gz
  • https://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_latest in a livecheck block.

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 strategy block.

<<~GRAPHQL
  query($owner: String!, $name: String!) {
    repository(owner: $owner, name: $name) {
      latestRelease { tagName }
    }
  }
GRAPHQL

Class Method Summary collapse

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 "latest" release and identifies the version 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:



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.

Parameters:

  • url (String) —

    the URL used to generate values

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

    the type of GitHub data to fetch

Returns:



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.

Parameters:

  • url (String) —

    the URL to match against

Returns:

  • (Boolean)


57
58
59
# File 'livecheck/strategy/github_latest.rb', line 57

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