livecheck: improve host checks to instead check domains

This commit is contained in:
Bo Anderson 2022-05-20 01:43:00 +01:00
parent 54b45c2c8b
commit 38ae98cbcd
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

@ -492,34 +492,35 @@ module Homebrew
sig { params(url: String).returns(String) } sig { params(url: String).returns(String) }
def preprocess_url(url) def preprocess_url(url)
begin begin
uri = URI.parse url uri = Addressable::URI.parse url
rescue URI::InvalidURIError rescue Addressable::URI::InvalidURIError
return url return url
end end
host = uri.host host = uri.host
domain = uri.domain
path = uri.path path = uri.path
return url if host.nil? || path.nil? return url if host.nil? || path.nil?
host = "github.com" if host == "github.s3.amazonaws.com" domain = host = "github.com" if host == "github.s3.amazonaws.com"
path = path.delete_prefix("/").delete_suffix(".git") path = path.delete_prefix("/").delete_suffix(".git")
scheme = uri.scheme scheme = uri.scheme
if host.end_with?("github.com") if domain == "github.com"
return url if path.match? %r{/releases/latest/?$} return url if path.match? %r{/releases/latest/?$}
owner, repo = path.delete_prefix("downloads/").split("/") owner, repo = path.delete_prefix("downloads/").split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git" url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GITEA_INSTANCES) elsif GITEA_INSTANCES.include?(domain)
return url if path.match? %r{/releases/latest/?$} return url if path.match? %r{/releases/latest/?$}
owner, repo = path.split("/") owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git" url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GOGS_INSTANCES) elsif GOGS_INSTANCES.include?(domain)
owner, repo = path.split("/") owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git" url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
# sourcehut # sourcehut
elsif host.end_with?("git.sr.ht") elsif host == "git.sr.ht"
owner, repo = path.split("/") owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}" url = "#{scheme}://#{host}/#{owner}/#{repo}"
# GitLab (gitlab.com or self-hosted) # GitLab (gitlab.com or self-hosted)