Class: Dependabot::Docker::Version

Inherits:
Version
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/docker/version.rb

Overview

In the special case of Java, the version string may also contain optional “update number” and “identifier” components. See www.oracle.com/java/technologies/javase/versioning-naming.html for a description of Java versions.

Constant Summary collapse

DOCKER_VERSION_REGEX =

The regex has limits for the 0,255 and 1,255 repetitions to avoid infinite limits which makes codeql angry. A docker image cannot be longer than 255 characters anyways.

/^(?<prefix>[a-z._\-]{0,255})[_\-v]?(?<version>.{1,255})$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dependabot/docker/version.rb', line 23

def initialize(version)
  parsed_version = version.to_s.match(DOCKER_VERSION_REGEX)
  release_part, update_part = T.must(T.must(parsed_version)[:version]).split("_", 2)

  # The numeric_version is needed here to validate the version string (ex: 20.9.0-alpine3.18)
  # when the call is made via Dependabot Api to convert the image version to semver.
  release_part = Tag.new(T.must(release_part).chomp(".").chomp("-").chomp("_")).numeric_version

  @release_part = T.let(Dependabot::Version.new(T.must(release_part).tr("-", ".")), Dependabot::Version)
  @update_part = T.let(
    Dependabot::Version.new(update_part&.start_with?(/[0-9]/) ? update_part : 0),
    Dependabot::Version
  )

  super(@release_part)
end

Instance Attribute Details

#release_partObject (readonly)

Returns the value of attribute release_part.



68
69
70
# File 'lib/dependabot/docker/version.rb', line 68

def release_part
  @release_part
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dependabot/docker/version.rb', line 41

def self.correct?(version)
  return true if version.is_a?(Gem::Version)

  # We can't call new here because Gem::Version calls self.correct? in its initialize method
  # causing an infinite loop, so instead we check if the release_part of the version is correct
  parsed_version = version.to_s.match(DOCKER_VERSION_REGEX)
  return false if parsed_version.nil?

  release_part, = T.must(parsed_version[:version]).split("_", 2)
  release_part = Tag.new(T.must(release_part).chomp(".").chomp("-").chomp("_")).numeric_version || parsed_version
  super(release_part.to_s)
rescue ArgumentError
  # if we can't instantiate a version, it can't be correct
  false
end

Instance Method Details

#<=>(other) ⇒ Object



71
72
73
# File 'lib/dependabot/docker/version.rb', line 71

def <=>(other)
  sort_criteria <=> other.sort_criteria
end

#segmentsObject



63
64
65
# File 'lib/dependabot/docker/version.rb', line 63

def segments
  @release_part.segments
end

#sort_criteriaObject



76
77
78
# File 'lib/dependabot/docker/version.rb', line 76

def sort_criteria
  [@release_part, @update_part]
end

#to_semverObject



58
59
60
# File 'lib/dependabot/docker/version.rb', line 58

def to_semver
  @release_part.to_semver
end