Module: SharedAudits Private

Defined in:
utils/shared_audits.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Auditing functions for rules common to both casks and formulae.

Constant Summary collapse

URL_TYPE_HOMEPAGE =

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.

"homepage URL"

Class Method Summary collapse

Class Method Details

.bitbucket(user, repo) ⇒ 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.

Parameters:

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'utils/shared_audits.rb', line 155

def self.bitbucket(user, repo)
  api_url = "https://api.bitbucket.org/2.0/repositories/#{user}/#{repo}"
  out, _, status = Utils::Curl.curl_output("--request", "GET", api_url)
  return unless status.success?

   = JSON.parse(out)
  return if .nil?

  return "Uses deprecated Mercurial support in Bitbucket" if ["scm"] == "hg"

  return "Bitbucket fork (not canonical repository)" unless ["parent"].nil?

  return "Bitbucket repository too new (<30 days old)" if Date.parse(["created_on"]) >= (Date.today - 30)

  forks_out, _, forks_status = Utils::Curl.curl_output("--request", "GET", "#{api_url}/forks")
  return unless forks_status.success?

  watcher_out, _, watcher_status = Utils::Curl.curl_output("--request", "GET", "#{api_url}/watchers")
  return unless watcher_status.success?

   = JSON.parse(forks_out)
  return if .nil?

   = JSON.parse(watcher_out)
  return if .nil?

  return if ["size"] >= 30 || ["size"] >= 75

  "Bitbucket repository not notable enough (<30 forks and <75 watchers)"
end

.check_deprecate_disable_reason(formula_or_cask) ⇒ 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.

Parameters:

Returns:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'utils/shared_audits.rb', line 198

def self.check_deprecate_disable_reason(formula_or_cask)
  return if !formula_or_cask.deprecated? && !formula_or_cask.disabled?

  reason = formula_or_cask.deprecated? ? formula_or_cask.deprecation_reason : formula_or_cask.disable_reason
  return unless reason.is_a?(Symbol)

  reasons = if formula_or_cask.is_a?(Formula)
    DeprecateDisable::FORMULA_DEPRECATE_DISABLE_REASONS
  else
    DeprecateDisable::CASK_DEPRECATE_DISABLE_REASONS
  end

  "#{reason} is not a valid deprecate! or disable! reason" unless reasons.include?(reason)
end

.eol_data(product, cycle) ⇒ Hash{String => 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.

Parameters:

Returns:



12
13
14
15
16
17
18
19
20
# File 'utils/shared_audits.rb', line 12

def self.eol_data(product, cycle)
  @eol_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))
  @eol_data["#{product}/#{cycle}"] ||= begin
    out, _, status = Utils::Curl.curl_output("--location", "https://endoflife.date/api/#{product}/#{cycle}.json")
    json = JSON.parse(out) if status.success?
    json = nil if json&.dig("message")&.include?("Product not found")
    json
  end
end

.github(user, repo) ⇒ 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.

Parameters:

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'utils/shared_audits.rb', line 121

def self.github(user, repo)
   = github_repo_data(user, repo)

  return if .nil?

  return "GitHub fork (not canonical repository)" if ["fork"]

  if (["forks_count"] < 30) && (["subscribers_count"] < 30) &&
     (["stargazers_count"] < 75)
    return "GitHub repository not notable enough (<30 forks, <30 watchers and <75 stars)"
  end

  return if Date.parse(["created_at"]) <= (Date.today - 30)

  "GitHub repository too new (<30 days old)"
end

.github_release(user, repo, tag, formula: nil, cask: nil) ⇒ 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.

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'utils/shared_audits.rb', line 55

def self.github_release(user, repo, tag, formula: nil, cask: nil)
  release = github_release_data(user, repo, tag)
  return unless release

  exception, name, version = if formula
    [formula.tap&.audit_exception(:github_prerelease_allowlist, formula.name), formula.name, formula.version]
  elsif cask
    [cask.tap&.audit_exception(:github_prerelease_allowlist, cask.token), cask.token, cask.version]
  end

  return "#{tag} is a GitHub pre-release." if release["prerelease"] && [version, "all"].exclude?(exception)

  if !release["prerelease"] && exception
    return "#{tag} is not a GitHub pre-release but '#{name}' is in the GitHub prerelease allowlist."
  end

  "#{tag} is a GitHub draft." if release["draft"]
end

.github_repo_data(user, repo) ⇒ Hash{String => 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.

Parameters:

Returns:



23
24
25
26
27
28
29
30
31
32
# File 'utils/shared_audits.rb', line 23

def self.github_repo_data(user, repo)
  @github_repo_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))
  @github_repo_data["#{user}/#{repo}"] ||= GitHub.repository(user, repo)

  @github_repo_data["#{user}/#{repo}"]
rescue GitHub::API::HTTPNotFoundError
  nil
rescue GitHub::API::AuthenticationFailedError => e
  raise unless e.message.match?(GitHub::API::GITHUB_IP_ALLOWLIST_ERROR)
end

.github_tag_from_url(url) ⇒ 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.

Parameters:

Returns:



187
188
189
190
# File 'utils/shared_audits.rb', line 187

def self.github_tag_from_url(url)
  tag = url[%r{^https://github\.com/[\w-]+/[\w.-]+/archive/refs/tags/(.+)\.(tar\.gz|zip)$}, 1]
  tag || url[%r{^https://github\.com/[\w-]+/[\w.-]+/releases/download/([^/]+)/}, 1]
end

.gitlab(user, repo) ⇒ 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.

Parameters:

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'utils/shared_audits.rb', line 139

def self.gitlab(user, repo)
   = gitlab_repo_data(user, repo)

  return if .nil?

  return "GitLab fork (not canonical repository)" if ["fork"]
  if (["forks_count"] < 30) && (["star_count"] < 75)
    return "GitLab repository not notable enough (<30 forks and <75 stars)"
  end

  return if Date.parse(["created_at"]) <= (Date.today - 30)

  "GitLab repository too new (<30 days old)"
end

.gitlab_release(user, repo, tag, formula: nil, cask: nil) ⇒ 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.

Parameters:

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'utils/shared_audits.rb', line 104

def self.gitlab_release(user, repo, tag, formula: nil, cask: nil)
  release = gitlab_release_data(user, repo, tag)
  return unless release

  return if DateTime.parse(release["released_at"]) <= DateTime.now

  exception, version = if formula
    [formula.tap&.audit_exception(:gitlab_prerelease_allowlist, formula.name), formula.version]
  elsif cask
    [cask.tap&.audit_exception(:gitlab_prerelease_allowlist, cask.token), cask.version]
  end
  return if [version, "all"].include?(exception)

  "#{tag} is a GitLab pre-release."
end

.gitlab_repo_data(user, repo) ⇒ Hash{String => 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.

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
# File 'utils/shared_audits.rb', line 75

def self.gitlab_repo_data(user, repo)
  @gitlab_repo_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))
  @gitlab_repo_data["#{user}/#{repo}"] ||= begin
    out, _, status = Utils::Curl.curl_output("https://gitlab.com/api/v4/projects/#{user}%2F#{repo}")
    json = JSON.parse(out) if status.success?
    json = nil if json&.dig("message")&.include?("404 Project Not Found")
    json
  end
end

.gitlab_tag_from_url(url) ⇒ 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.

Parameters:

Returns:



193
194
195
# File 'utils/shared_audits.rb', line 193

def self.gitlab_tag_from_url(url)
  url[%r{^https://gitlab\.com/(?:\w[\w.-]*/){2,}-/archive/([^/]+)/}, 1]
end