Skip to content

Commit

Permalink
pkg/repo: fix bad parsing when chartversion has a int after the dash …
Browse files Browse the repository at this point in the history
…but matches semver.org spec,like 0.0.1-SNAPSHOT-1
  • Loading branch information
scbizu committed Aug 9, 2020
1 parent 18656d8 commit f9f40d9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/repo/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/chartmuseum/storage"
helm_chart "helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
Expand Down Expand Up @@ -61,6 +62,7 @@ func ChartPackageFilenameFromContent(content []byte) (string, error) {
// ChartVersionFromStorageObject returns a chart version from a storage object
func ChartVersionFromStorageObject(object storage.Object) (*helm_repo.ChartVersion, error) {
if len(object.Content) == 0 {
// we cannot allow the chart content to be empty
chartVersion := emptyChartVersionFromPackageFilename(object.Path)
if chartVersion.Name == "" || chartVersion.Version == "" {
return nil, ErrorInvalidChartPackage
Expand Down Expand Up @@ -99,6 +101,9 @@ func chartFromContent(content []byte) (*helm_chart.Chart, error) {
return chart, err
}

// emptyChartVersionFromPackageFilename parses the filename to the chartname and chartversion
// it will range the filename from the tail , and return the version which matches the semver standard until to the head .
// If no valid version found, it will return the part after first `-` as the chartversion.
func emptyChartVersionFromPackageFilename(filename string) *helm_repo.ChartVersion {
noExt := strings.TrimSuffix(pathutil.Base(filename), fmt.Sprintf(".%s", ChartPackageFileExtension))
parts := strings.Split(noExt, "-")
Expand All @@ -110,7 +115,10 @@ func emptyChartVersionFromPackageFilename(filename string) *helm_repo.ChartVersi
if _, err := strconv.Atoi(string(parts[idx][0])); err == nil { // see if this part looks like a version (starts w int)
version = strings.Join(parts[idx:], "-")
name = strings.Join(parts[:idx], "-")
break
// read until find a valid semantic version
if _, err := semver.StrictNewVersion(version); err == nil {
break
}
}
}
if version == "" { // no parts looked like a real version, just take everything after last hyphen
Expand Down
36 changes: 36 additions & 0 deletions pkg/repo/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ func (suite *ChartTestSuite) TestChartVersionFromStorageObject() {
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT", chartVersion.Version, "chart version as expected")

snapshotObject2 := storage.Object{
Path: "mychart-1.0.4-SNAPSHOT-1.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(snapshotObject2)
suite.Nil(err)
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

multiHyphenObject := storage.Object{
Path: "my-long-hyphenated-chart-name-1.0.4.tgz",
Content: []byte{},
Expand All @@ -103,6 +113,16 @@ func (suite *ChartTestSuite) TestChartVersionFromStorageObject() {
suite.Equal("my-long-hyphenated-chart-name", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT", chartVersion.Version, "chart version as expected")

multiHyphenSnapshotObject = storage.Object{
Path: "my-long-hyphenated-chart-name-1.0.4-SNAPSHOT-1.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(multiHyphenSnapshotObject)
suite.Nil(err)
suite.Equal("my-long-hyphenated-chart-name", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

crapVersionObject := storage.Object{
Path: "my-long-hyphenated-chart-name-crapversion.tgz",
Content: []byte{},
Expand Down Expand Up @@ -135,6 +155,22 @@ func (suite *ChartTestSuite) TestChartVersionFromStorageObject() {
suite.Nil(err)
suite.Equal("mychart-1x", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-rc1-SNAPSHOT", chartVersion.Version, "chart version as expected")

hyphenDigitalObject.Path = "mychart-1x-1.0.4-rc1-SNAPSHOT-1.tgz"
chartVersion, err = ChartVersionFromStorageObject(hyphenDigitalObject)
suite.Nil(err)
suite.Equal("mychart-1x", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-rc1-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

noValidSemanticVersionObject := storage.Object{
Path: "mychart-1x-xxx.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(noValidSemanticVersionObject)
suite.Nil(err)
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1x-xxx", chartVersion.Version, "chart version as expected")
}

func (suite *ChartTestSuite) TestStorageObjectFromChartVersion() {
Expand Down

0 comments on commit f9f40d9

Please sign in to comment.