Skip to content

Commit

Permalink
utils/shared_audits: Bump to Sorbet typed: strict
Browse files Browse the repository at this point in the history
  • Loading branch information
issyl0 committed Aug 10, 2024
1 parent cc11ab5 commit 92cab83
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
14 changes: 7 additions & 7 deletions Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ def cask_plist_min_os
def audit_github_prerelease_version
odebug "Auditing GitHub prerelease"
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?
return if user.nil? || repo.nil?

tag = SharedAudits.github_tag_from_url(cask.url)
tag ||= cask.version
Expand All @@ -765,7 +765,7 @@ def audit_github_prerelease_version
sig { void }
def audit_gitlab_prerelease_version
user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?
return if user.nil? || repo.nil?

odebug "Auditing GitLab prerelease"

Expand All @@ -781,7 +781,7 @@ def audit_github_repository_archived
return if cask.deprecated? || cask.disabled?

user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?
return if user.nil? || repo.nil?

metadata = SharedAudits.github_repo_data(user, repo)
return if metadata.nil?
Expand All @@ -795,7 +795,7 @@ def audit_gitlab_repository_archived
return if cask.deprecated? || cask.disabled?

user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?
return if user.nil? || repo.nil?

odebug "Auditing GitLab repo archived"

Expand All @@ -810,7 +810,7 @@ def audit_github_repository
return unless new_cask?

user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*})
return if user.nil?
return if user.nil? || repo.nil?

odebug "Auditing GitHub repo"

Expand All @@ -823,7 +823,7 @@ def audit_gitlab_repository
return unless new_cask?

user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*})
return if user.nil?
return if user.nil? || repo.nil?

odebug "Auditing GitLab repo"

Expand All @@ -836,7 +836,7 @@ def audit_bitbucket_repository
return unless new_cask?

user, repo = get_repo_data(%r{https?://bitbucket\.org/([^/]+)/([^/]+)/?.*})
return if user.nil?
return if user.nil? || repo.nil?

odebug "Auditing Bitbucket repo"

Expand Down
16 changes: 9 additions & 7 deletions Library/Homebrew/formula_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,13 @@ def audit_eol
metadata = SharedAudits.eol_data(name, formula.version.major)
metadata ||= SharedAudits.eol_data(name, formula.version.major_minor)

return if metadata.blank? || metadata["eol"] == false
return if metadata.blank? || (eol_date = metadata["eol"]).blank?

see_url = "see #{Formatter.url("https://endoflife.date/#{name}")}"
if metadata["eol"] == true
problem "Product is EOL, #{see_url}"
return
end
message = "Product is EOL"

Check warning on line 587 in Library/Homebrew/formula_auditor.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_auditor.rb#L587

Added line #L587 was not covered by tests
message += " since #{eol_date}" if Date.parse(eol_date.to_s) <= Date.today
message += ", see #{Formatter.url("https://endoflife.date/#{name}")}"

Check warning on line 589 in Library/Homebrew/formula_auditor.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_auditor.rb#L589

Added line #L589 was not covered by tests

problem "Product is EOL since #{metadata["eol"]}, #{see_url}" if Date.parse(metadata["eol"]) <= Date.today
problem message

Check warning on line 591 in Library/Homebrew/formula_auditor.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_auditor.rb#L591

Added line #L591 was not covered by tests
end

def audit_wayback_url
Expand Down Expand Up @@ -786,6 +784,8 @@ def audit_specs
tag ||= stable.version

if @online
return if owner.nil? || repo.nil?

error = SharedAudits.gitlab_release(owner, repo, tag, formula:)
problem error if error
end
Expand All @@ -796,6 +796,8 @@ def audit_specs
tag ||= formula.stable.specs[:tag]

if @online
return if owner.nil? || repo.nil?

error = SharedAudits.github_release(owner, repo, tag, formula:)
problem error if error
end
Expand Down
36 changes: 30 additions & 6 deletions Library/Homebrew/utils/shared_audits.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "utils/curl"
Expand All @@ -10,8 +10,9 @@ module SharedAudits

module_function

sig { params(product: String, cycle: String).returns(T.nilable(T::Hash[String, T::Hash[Symbol, T.untyped]])) }
def eol_data(product, cycle)
@eol_data ||= {}
@eol_data ||= T.let({}, T.nilable(T::Hash[String, T::Hash[String, T.untyped]]))

Check warning on line 15 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L15

Added line #L15 was not covered by tests
@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?
Expand All @@ -20,8 +21,9 @@ def eol_data(product, cycle)
end
end

sig { params(user: String, repo: String).returns(T.nilable(T::Hash[String, T.untyped])) }
def github_repo_data(user, repo)
@github_repo_data ||= {}
@github_repo_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))

Check warning on line 26 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L26

Added line #L26 was not covered by tests
@github_repo_data["#{user}/#{repo}"] ||= GitHub.repository(user, repo)

@github_repo_data["#{user}/#{repo}"]
Expand All @@ -31,10 +33,11 @@ def github_repo_data(user, repo)
raise unless e.message.match?(GitHub::API::GITHUB_IP_ALLOWLIST_ERROR)
end

sig { params(user: String, repo: String, tag: String).returns(T.nilable(T::Hash[String, T.untyped])) }
def github_release_data(user, repo, tag)
id = "#{user}/#{repo}/#{tag}"
url = "#{GitHub::API_URL}/repos/#{user}/#{repo}/releases/tags/#{tag}"
@github_release_data ||= {}
@github_release_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))

Check warning on line 40 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L40

Added line #L40 was not covered by tests
@github_release_data[id] ||= GitHub::API.open_rest(url)

@github_release_data[id]
Expand All @@ -44,6 +47,13 @@ def github_release_data(user, repo, tag)
raise unless e.message.match?(GitHub::API::GITHUB_IP_ALLOWLIST_ERROR)
end

sig {
params(

Check warning on line 51 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L51

Added line #L51 was not covered by tests
user: String, repo: String, tag: String, formula: T.nilable(Formula), cask: T.nilable(Cask::Cask),
).returns(
T.nilable(String),
)
}
def github_release(user, repo, tag, formula: nil, cask: nil)
release = github_release_data(user, repo, tag)
return unless release
Expand All @@ -63,8 +73,9 @@ def github_release(user, repo, tag, formula: nil, cask: nil)
"#{tag} is a GitHub draft." if release["draft"]
end

sig { params(user: String, repo: String).returns(T.nilable(T::Hash[String, T.untyped])) }
def gitlab_repo_data(user, repo)
@gitlab_repo_data ||= {}
@gitlab_repo_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))

Check warning on line 78 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L78

Added line #L78 was not covered by tests
@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?
Expand All @@ -73,9 +84,10 @@ def gitlab_repo_data(user, repo)
end
end

sig { params(user: String, repo: String, tag: String).returns(T.nilable(T::Hash[String, T.untyped])) }
def gitlab_release_data(user, repo, tag)
id = "#{user}/#{repo}/#{tag}"
@gitlab_release_data ||= {}
@gitlab_release_data ||= T.let({}, T.nilable(T::Hash[String, T.untyped]))

Check warning on line 90 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L90

Added line #L90 was not covered by tests
@gitlab_release_data[id] ||= begin
out, _, status = Utils::Curl.curl_output(
"https://gitlab.com/api/v4/projects/#{user}%2F#{repo}/releases/#{tag}", "--fail"
Expand All @@ -84,6 +96,13 @@ def gitlab_release_data(user, repo, tag)
end
end

sig {
params(

Check warning on line 100 in Library/Homebrew/utils/shared_audits.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/shared_audits.rb#L100

Added line #L100 was not covered by tests
user: String, repo: String, tag: String, formula: T.nilable(Formula), cask: T.nilable(Cask::Cask),
).returns(
T.nilable(String),
)
}
def gitlab_release(user, repo, tag, formula: nil, cask: nil)
release = gitlab_release_data(user, repo, tag)
return unless release
Expand All @@ -100,6 +119,7 @@ def gitlab_release(user, repo, tag, formula: nil, cask: nil)
"#{tag} is a GitLab pre-release."
end

sig { params(user: String, repo: String).returns(T.nilable(String)) }
def github(user, repo)
metadata = github_repo_data(user, repo)

Expand All @@ -117,6 +137,7 @@ def github(user, repo)
"GitHub repository too new (<30 days old)"
end

sig { params(user: String, repo: String).returns(T.nilable(String)) }
def gitlab(user, repo)
metadata = gitlab_repo_data(user, repo)

Expand All @@ -132,6 +153,7 @@ def gitlab(user, repo)
"GitLab repository too new (<30 days old)"
end

sig { params(user: String, repo: String).returns(T.nilable(String)) }
def bitbucket(user, repo)
api_url = "https://api.bitbucket.org/2.0/repositories/#{user}/#{repo}"
out, _, status = Utils::Curl.curl_output("--request", "GET", api_url)
Expand Down Expand Up @@ -163,6 +185,7 @@ def bitbucket(user, repo)
"Bitbucket repository not notable enough (<30 forks and <75 watchers)"
end

sig { params(url: String).returns(T.nilable(String)) }
def github_tag_from_url(url)
url = url.to_s
tag = url.match(%r{^https://github\.com/[\w-]+/[\w-]+/archive/refs/tags/([^/]+)\.(tar\.gz|zip)$})
Expand All @@ -174,6 +197,7 @@ def github_tag_from_url(url)
tag
end

sig { params(url: String).returns(T.nilable(String)) }
def gitlab_tag_from_url(url)
url = url.to_s
url.match(%r{^https://gitlab\.com/[\w-]+/[\w-]+/-/archive/([^/]+)/})
Expand Down

0 comments on commit 92cab83

Please sign in to comment.