Merge pull request #9074 from vladimyr/livecheck

livecheck: refactor url preprocessing
This commit is contained in:
Sam Ford 2020-11-26 14:46:57 -05:00 committed by GitHub
commit 9539485547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 31 deletions

View File

@ -1225,7 +1225,8 @@ class DownloadStrategyDetector
when %r{^https?://github\.com/[^/]+/[^/]+\.git$} when %r{^https?://github\.com/[^/]+/[^/]+\.git$}
GitHubGitDownloadStrategy GitHubGitDownloadStrategy
when %r{^https?://.+\.git$}, when %r{^https?://.+\.git$},
%r{^git://} %r{^git://},
%r{^https?://git\.sr\.ht/[^/]+/[^/]+$}
GitDownloadStrategy GitDownloadStrategy
when %r{^https?://www\.apache\.org/dyn/closer\.cgi}, when %r{^https?://www\.apache\.org/dyn/closer\.cgi},
%r{^https?://www\.apache\.org/dyn/closer\.lua} %r{^https?://www\.apache\.org/dyn/closer\.lua}

View File

@ -3,6 +3,7 @@
require "livecheck/strategy" require "livecheck/strategy"
require "ruby-progressbar" require "ruby-progressbar"
require "uri"
module Homebrew module Homebrew
# The {Livecheck} module consists of methods used by the `brew livecheck` # The {Livecheck} module consists of methods used by the `brew livecheck`
@ -13,18 +14,15 @@ module Homebrew
module Livecheck module Livecheck
module_function module_function
GITHUB_SPECIAL_CASES = %w[ GITEA_INSTANCES = %w[
api.github.com codeberg.org
/latest gitea.com
mednafen opendev.org
camlp5 tildegit.org
kotlin ].freeze
osrm-backend
prometheus GOGS_INSTANCES = %w[
pyenv-virtualenv lolg.it
sysdig
shairport-sync
yuicompressor
].freeze ].freeze
UNSTABLE_VERSION_KEYWORDS = %w[ UNSTABLE_VERSION_KEYWORDS = %w[
@ -316,26 +314,35 @@ module Homebrew
# Preprocesses and returns the URL used by livecheck. # Preprocesses and returns the URL used by livecheck.
# @return [String] # @return [String]
def preprocess_url(url) def preprocess_url(url)
# Check for GitHub repos on github.com, not AWS begin
url = url.sub("github.s3.amazonaws.com", "github.com") if url.include?("github") uri = URI.parse url
rescue URI::InvalidURIError
return url
end
# Use repo from GitHub or GitLab inferred from download URL host = uri.host == "github.s3.amazonaws.com" ? "github.com" : uri.host
if url.include?("github.com") && GITHUB_SPECIAL_CASES.none? { |sc| url.include? sc } path = uri.path.delete_prefix("/").delete_suffix(".git")
if url.include? "archive" scheme = uri.scheme
url = url.sub(%r{/archive/.*}, ".git") if url.include? "github"
elsif url.include? "releases"
url = url.sub(%r{/releases/.*}, ".git")
elsif url.include? "downloads"
url = "#{Pathname.new(url.sub(%r{/downloads(.*)}, "\\1")).dirname}.git"
elsif !url.end_with?(".git")
# Truncate the URL at the user/repo part, if possible
%r{(?<github_repo_url>(?:[a-z]+://)?github.com/[^/]+/[^/#]+)} =~ url
url = github_repo_url if github_repo_url.present?
url.delete_suffix!("/") if url.end_with?("/") if host.end_with?("github.com")
url += ".git" return url if path.match? %r{/releases/latest/?$}
end
elsif url.include?("/-/archive/") owner, repo = path.delete_prefix("downloads/").split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GITEA_INSTANCES)
return url if path.match? %r{/releases/latest/?$}
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GOGS_INSTANCES)
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
# sourcehut
elsif host.end_with?("git.sr.ht")
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}"
# GitLab (gitlab.com or self-hosted)
elsif path.include?("/-/archive/")
url = url.sub(%r{/-/archive/.*$}i, ".git") url = url.sub(%r{/-/archive/.*$}i, ".git")
end end

View File

@ -156,6 +156,12 @@ describe Homebrew::Livecheck do
describe "::preprocess_url" do describe "::preprocess_url" do
let(:github_git_url_with_extension) { "https://github.com/Homebrew/brew.git" } let(:github_git_url_with_extension) { "https://github.com/Homebrew/brew.git" }
it "returns the unmodified URL for an unparseable URL" do
# Modeled after the `head` URL in the `ncp` formula
expect(livecheck.preprocess_url(":something:cvs:@cvs.brew.sh:/cvs"))
.to eq(":something:cvs:@cvs.brew.sh:/cvs")
end
it "returns the unmodified URL for a GitHub URL ending in .git" do it "returns the unmodified URL for a GitHub URL ending in .git" do
expect(livecheck.preprocess_url(github_git_url_with_extension)) expect(livecheck.preprocess_url(github_git_url_with_extension))
.to eq(github_git_url_with_extension) .to eq(github_git_url_with_extension)
@ -200,5 +206,35 @@ describe Homebrew::Livecheck do
expect(livecheck.preprocess_url("https://brew.sh/Homebrew/brew/-/archive/1.0.0/brew-1.0.0.tar.gz")) expect(livecheck.preprocess_url("https://brew.sh/Homebrew/brew/-/archive/1.0.0/brew-1.0.0.tar.gz"))
.to eq("https://brew.sh/Homebrew/brew.git") .to eq("https://brew.sh/Homebrew/brew.git")
end end
it "returns the Git repository URL for a Codeberg archive URL" do
expect(livecheck.preprocess_url("https://codeberg.org/Homebrew/brew/archive/brew-1.0.0.tar.gz"))
.to eq("https://codeberg.org/Homebrew/brew.git")
end
it "returns the Git repository URL for a Gitea archive URL" do
expect(livecheck.preprocess_url("https://gitea.com/Homebrew/brew/archive/brew-1.0.0.tar.gz"))
.to eq("https://gitea.com/Homebrew/brew.git")
end
it "returns the Git repository URL for an Opendev archive URL" do
expect(livecheck.preprocess_url("https://opendev.org/Homebrew/brew/archive/brew-1.0.0.tar.gz"))
.to eq("https://opendev.org/Homebrew/brew.git")
end
it "returns the Git repository URL for a tildegit archive URL" do
expect(livecheck.preprocess_url("https://tildegit.org/Homebrew/brew/archive/brew-1.0.0.tar.gz"))
.to eq("https://tildegit.org/Homebrew/brew.git")
end
it "returns the Git repository URL for a LOL Git archive URL" do
expect(livecheck.preprocess_url("https://lolg.it/Homebrew/brew/archive/brew-1.0.0.tar.gz"))
.to eq("https://lolg.it/Homebrew/brew.git")
end
it "returns the Git repository URL for a sourcehut archive URL" do
expect(livecheck.preprocess_url("https://git.sr.ht/~Homebrew/brew/archive/1.0.0.tar.gz"))
.to eq("https://git.sr.ht/~Homebrew/brew")
end
end end
end end