diff --git a/Library/Homebrew/test/utils/shared_audits_spec.rb b/Library/Homebrew/test/utils/shared_audits_spec.rb index 819536c355..995fe9c79a 100644 --- a/Library/Homebrew/test/utils/shared_audits_spec.rb +++ b/Library/Homebrew/test/utils/shared_audits_spec.rb @@ -3,6 +3,28 @@ require "utils/shared_audits" RSpec.describe SharedAudits do + describe "::github_tag_from_url" do + it "finds tags in archive urls" do + url = "https://github.com/a/b/archive/refs/tags/v1.2.3.tar.gz" + expect(described_class.github_tag_from_url(url)).to eq("v1.2.3") + end + + it "finds tags in release urls" do + url = "https://github.com/a/b/releases/download/1.2.3/b-1.2.3.tar.bz2" + expect(described_class.github_tag_from_url(url)).to eq("1.2.3") + end + + it "finds tags with slashes" do + url = "https://github.com/a/b/archive/refs/tags/c/d/e/f/g-v1.2.3.tar.gz" + expect(described_class.github_tag_from_url(url)).to eq("c/d/e/f/g-v1.2.3") + end + + it "finds tags in orgs/repos with special characters" do + url = "https://github.com/a-b/c-d_e.f/archive/refs/tags/2.5.tar.gz" + expect(described_class.github_tag_from_url(url)).to eq("2.5") + end + end + describe "::gitlab_tag_from_url" do it "doesn't find tags in invalid urls" do url = "https://gitlab.com/a/-/archive/v1.2.3/a-v1.2.3.tar.gz" diff --git a/Library/Homebrew/utils/shared_audits.rb b/Library/Homebrew/utils/shared_audits.rb index ff11c429eb..1c0d574ab5 100644 --- a/Library/Homebrew/utils/shared_audits.rb +++ b/Library/Homebrew/utils/shared_audits.rb @@ -185,14 +185,8 @@ module SharedAudits sig { params(url: String).returns(T.nilable(String)) } def self.github_tag_from_url(url) - url = url.to_s - tag = url.match(%r{^https://github\.com/[\w-]+/[\w-]+/archive/refs/tags/([^/]+)\.(tar\.gz|zip)$}) - .to_a - .second - tag ||= url.match(%r{^https://github\.com/[\w-]+/[\w-]+/releases/download/([^/]+)/}) - .to_a - .second - tag + tag = url[%r{^https://github\.com/[\w-]+/[\w.-]+/archive/refs/tags/(.+)\.(tar\.gz|zip)$}, 1] + tag || url[%r{^https://github\.com/[\w-]+/[\w.-]+/releases/download/([^/]+)/}, 1] end sig { params(url: String).returns(T.nilable(String)) }