livecheck: fix github & gitlab url processing

- support both `github.com/downloads/<owner>/<repo>` and
  `github.s3.amazonaws.com/<owner>/<repo>` URL patterns
- support self-hosted GitLab installations (with project groups)
- support _well-known_ Gitea and Gogs instances
This commit is contained in:
Dario Vladovic 2020-11-11 02:32:44 +01:00 committed by Sam Ford
parent 1f103f88d0
commit 74fd700445
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE

View File

@ -14,6 +14,17 @@ module Homebrew
module Livecheck module Livecheck
module_function module_function
GITEA_INSTANCES = %w[
codeberg.org
gitea.com
opendev.org
tildegit.org
].freeze
GOGS_INSTANCES = %w[
lolg.it
].freeze
UNSTABLE_VERSION_KEYWORDS = %w[ UNSTABLE_VERSION_KEYWORDS = %w[
alpha alpha
beta beta
@ -304,22 +315,30 @@ module Homebrew
# @return [String] # @return [String]
def preprocess_url(url) def preprocess_url(url)
uri = URI.parse url uri = URI.parse url
host = uri.host host = uri.host == "github.s3.amazonaws.com" ? "github.com" : uri.host
path = uri.path.delete_prefix("/").delete_suffix(".git") path = uri.path.delete_prefix("/").delete_suffix(".git")
scheme = uri.scheme
if host == "github.s3.amazonaws.com" if host == "github.com"
owner, repo = path.sub(%r{^downloads/}, "").split("/") return url if path.match? %r{/releases/latest/?$}
return "https://github.com/#{owner}/#{repo}.git"
end
if ["github.com", "tildegit.org", "gitlab.com"].include? host owner, repo = path.delete_prefix("downloads/").split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif GITEA_INSTANCES.include? host
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 = "https://#{host}/#{owner}/#{repo}.git" url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif GOGS_INSTANCES.include? host
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
# sourcehut
elsif host == "git.sr.ht" elsif host == "git.sr.ht"
owner, repo = path.split("/") owner, repo = path.split("/")
url = "https://#{host}/#{owner}/#{repo}" url = "#{scheme}://#{host}/#{owner}/#{repo}"
# gitlab
elsif path.include?("/-/archive/")
url = url.sub(%r{/-/archive/.*$}i, ".git")
end end
url url