livecheck: add GithubLatest strategy

This commit is contained in:
nandahkrishna 2020-12-02 18:04:22 +05:30
parent 1834a33d08
commit f96b8e7138
No known key found for this signature in database
GPG Key ID: 067E5FCD58ADF3AA
3 changed files with 86 additions and 0 deletions

View File

@ -78,6 +78,7 @@ end
require_relative "strategy/apache"
require_relative "strategy/bitbucket"
require_relative "strategy/git"
require_relative "strategy/github_latest"
require_relative "strategy/gnome"
require_relative "strategy/gnu"
require_relative "strategy/hackage"

View File

@ -0,0 +1,59 @@
# typed: false
# frozen_string_literal: true
module Homebrew
module Livecheck
module Strategy
# The {GithubLatest} strategy identifies versions of software at
# github.com by checking a repository's latest release page.
#
# GitHub URLs take a few differemt formats:
#
# * `https://github.com/example/example/releases/download/1.2.3/example-1.2.3.zip`
# * `https://github.com/example/example/archive/v1.2.3.tar.gz`
#
# This strategy is used when latest releases are marked for software hosted
# on GitHub. It is necessary to use `strategy :github_latest` in a `livecheck`
# block for Livecheck to use this strategy.
#
# The default regex identifies versions from `href` attributes containing the
# tag name.
#
# @api public
class GithubLatest
NICE_NAME = "GitHub Latest"
# The `Regexp` used to determine if the strategy applies to the URL.
URL_MATCH_REGEX = /github.com/i.freeze
# Whether the strategy can be applied to the provided URL.
#
# @param url [String] the URL to match against
# @return [Boolean]
def self.match?(url)
URL_MATCH_REGEX.match?(url)
end
# Generates a URL and regex (if one isn't provided) and passes them
# to {PageMatch.find_versions} to identify versions in the content.
#
# @param url [String] the URL of the content to check
# @param regex [Regexp] a regex used for matching versions in content
# @return [Hash]
def self.find_versions(url, regex = nil)
%r{github\.com/(?<username>[\da-z\-]+)/(?<repository>[\da-z_\-]+)}i =~ url
# The page containing the latest release
page_url = "https://github.com/#{username}/#{repository}/releases/latest"
# The default regex applies to most repositories, but may have to be
# replaced with a specific regex when the tag names contain the package
# name or other characters apart from the version.
regex ||= %r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i
Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex)
end
end
end
end
end

View File

@ -0,0 +1,26 @@
# typed: false
# frozen_string_literal: true
require "livecheck/strategy/github_latest"
describe Homebrew::Livecheck::Strategy::GithubLatest do
subject(:github_latest) { described_class }
let(:github_download_url) { "https://github.com/example/example/releases/download/1.2.3/example-1.2.3.zip" }
let(:github_archive_url) { "https://github.com/example/example/archive/v1.2.3.tar.gz" }
let(:non_github_url) { "https://brew.sh/test" }
describe "::match?" do
it "returns true if the argument provided is a GitHub Download URL" do
expect(github_latest.match?(github_download_url)).to be true
end
it "returns true if the argument provided is a GitHub Archive URL" do
expect(github_latest.match?(github_archive_url)).to be true
end
it "returns false if the argument provided is not a GitHub URL" do
expect(github_latest.match?(non_github_url)).to be false
end
end
end