diff --git a/github_actions/lib/dependabot/github_actions/version.rb b/github_actions/lib/dependabot/github_actions/version.rb index e5b1a1b49c..edcc777f6c 100644 --- a/github_actions/lib/dependabot/github_actions/version.rb +++ b/github_actions/lib/dependabot/github_actions/version.rb @@ -31,6 +31,8 @@ def self.remove_leading_v(version) sig { override.params(version: VersionParameter).returns(T::Boolean) } def self.correct?(version) + return false if version.to_s.strip.empty? + version = Version.remove_leading_v(version) super end diff --git a/github_actions/spec/dependabot/github_actions/version_spec.rb b/github_actions/spec/dependabot/github_actions/version_spec.rb index 4ab4b22b57..fc61fcfb09 100644 --- a/github_actions/spec/dependabot/github_actions/version_spec.rb +++ b/github_actions/spec/dependabot/github_actions/version_spec.rb @@ -9,6 +9,10 @@ semver_without_v = "1.2.3" describe "#correct?" do + it "rejects nil" do + expect(described_class.correct?(nil)).to be(false) + end + it "accepts semver" do expect(described_class.correct?(semver_version)).to be(true) end diff --git a/terraform/lib/dependabot/terraform/version.rb b/terraform/lib/dependabot/terraform/version.rb index e8811c007e..ccd296e99b 100644 --- a/terraform/lib/dependabot/terraform/version.rb +++ b/terraform/lib/dependabot/terraform/version.rb @@ -19,6 +19,9 @@ class Version < Dependabot::Version sig { override.params(version: VersionParameter).void } def initialize(version) @version_string = T.let(version.to_s, String) + version = Version.remove_leading_v(version) + version = Version.remove_backport(version) + super end @@ -31,6 +34,30 @@ def self.new(version) def to_s @version_string end + + sig { override.params(version: VersionParameter).returns(T::Boolean) } + def self.correct?(version) + version = Version.remove_leading_v(version) + version = Version.remove_backport(version) + + return false if version.to_s.strip.empty? + + super + end + + sig { params(version: VersionParameter).returns(VersionParameter) } + def self.remove_leading_v(version) + return version.gsub(/^v/, "") if version.is_a?(String) + + version + end + + sig { params(version: VersionParameter).returns(VersionParameter) } + def self.remove_backport(version) + return version.split("+").first if version.is_a?(String) && version.include?("+") + + version + end end end end diff --git a/terraform/spec/dependabot/terraform/version_spec.rb b/terraform/spec/dependabot/terraform/version_spec.rb index 1d10f3d6bc..fdc14b04b5 100644 --- a/terraform/spec/dependabot/terraform/version_spec.rb +++ b/terraform/spec/dependabot/terraform/version_spec.rb @@ -29,5 +29,57 @@ it { is_expected.to eq "1.0.0-pre1" } end + + context "with a leading v" do + let(:version_string) { "v0.3.2" } + + it { is_expected.to eq "v0.3.2" } + end + + context "with a backported version" do + let(:version_string) { "1.17.2+backport-1" } + + it { is_expected.to eq "1.17.2+backport-1" } + end + end + + describe "#initialize" do + subject { version.inspect } + + context "with a leading v" do + let(:version_string) { "v0.3.2" } + let(:expected) { "#<#{described_class} \"0.3.2\">" } + + it { is_expected.to eq expected } + end + + context "with a backported version" do + let(:version_string) { "1.17.2+backport-1" } + let(:expected) { "#<#{described_class} \"1.17.2\">" } + + it { is_expected.to eq expected } + end + end + + describe ".correct?" do + subject { described_class.correct?(version_string) } + + valid = %w(1.0.0 v0.3.2 1.17.2+backport-1) + valid.each do |version| + context "with version #{version}" do + let(:version_string) { version } + + it { is_expected.to be(true) } + end + end + + invalid = ["", nil] + invalid.each do |version| + context "with version #{version}" do + let(:version_string) { version } + + it { is_expected.to be(false) } + end + end end end