Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions makepurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

// CleanVersion returns plain versions unchanged. For version constraints, it
// uses the vers library to extract the minimum bound. If parsing fails, it
// returns the original string.
// uses the vers library to extract the lowest included version. If no included
// minimum can be identified, it returns the original string.
func CleanVersion(version, scheme string) string {
if version == "" {
return ""
Expand All @@ -19,13 +19,12 @@ func CleanVersion(version, scheme string) string {
}

r, err := vers.ParseNative(version, scheme)
if err != nil || len(r.Intervals) == 0 {
if err != nil {
return version
}

// Return the minimum bound from the first interval
if r.Intervals[0].Min != "" {
return r.Intervals[0].Min
if minimum, ok := r.MinimumVersion(); ok {
return minimum
}

return version
Expand Down
4 changes: 4 additions & 0 deletions makepurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func TestCleanVersion(t *testing.T) {
{"~1.0.0", "npm", "1.0.0"},
{">=1.0.0", "npm", "1.0.0"},
{">=1.0.0 <2.0.0", "npm", "1.0.0"},
{">1.0.0", "npm", ">1.0.0"},
{"2.0.0 || 1.0.0", "npm", "1.0.0"},

// gem constraints
{"~> 1.0", "gem", "1.0"},
Expand All @@ -27,6 +29,7 @@ func TestCleanVersion(t *testing.T) {

// maven constraints
{"[1.0,2.0)", "maven", "1.0"},
{"(1.0,2.0]", "maven", "(1.0,2.0]"},

// cargo constraints
{"^1.0.0", "cargo", "1.0.0"},
Expand Down Expand Up @@ -66,6 +69,7 @@ func TestBuildPURLString(t *testing.T) {
want string
}{
{"simple npm", "npm", "lodash", "4.17.21", "", "pkg:npm/lodash@4.17.21"},
{"exclusive npm constraint", "npm", "lodash", ">1.0.0", "", "pkg:npm/lodash@%3E1.0.0"},
{"scoped npm", "npm", "@babel/core", "7.20.0", "", "pkg:npm/%40babel/core@7.20.0"}, // @ encoded in namespace
{"gem", "rubygems", "rails", "7.0.0", "", "pkg:gem/rails@7.0.0"},
{"pypi", "pypi", "requests", "2.28.0", "", "pkg:pypi/requests@2.28.0"},
Expand Down
Loading