Repository: helm/helm (go)

Issue:
helm repo search finds versions with dashes in them
Output of `helm version`:
version.BuildInfo{Version:"v3.3.4", GitCommit:"a61ce5633af99708171414353ed49547cf05013d", GitTreeState:"clean", GoVersion:"go1.14.9"}

Output of `kubectl version`:
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T13:41:02Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.2", GitCommit:"52c56ce7a8272c798dbc29846288d7cd9fbae032", GitTreeState:"clean", BuildDate:"2020-06-03T10:06:47Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

Doing a `helm repo search test` gives:
```sh
$ helm search repo test
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
repoA/test              1.17.0-0565674  0565674         A Helm chart for test
```

I thought that only chart versions that had the form `x.x.x` would be shown and not `x.x.x-x`. The latter should only be found if using `--devel`. Did I misunderstand this, or is this a bug?

Pull request:
helm search supports semver pre version numbers starting with 0
fix #8924

**What this PR does / why we need it**:
If the current helm repo version number contains illegal semver rules (pre version starts with 0), helm repo search will not work properly

**Special notes for your reviewer**:

**If applicable**:
- [ ] this PR contains documentation
- [x] this PR contains unit tests
- [ ] this PR has been tested for backwards compatibility


Patch:
diff --git a/cmd/helm/search_repo.go b/cmd/helm/search_repo.go
index bf82a6051..a6ec16ea6 100644
--- a/cmd/helm/search_repo.go
+++ b/cmd/helm/search_repo.go
@@ -154,16 +154,26 @@ func (o *searchRepoOptions) applyConstraint(res []*search.Result) ([]*search.Res
 
 	data := res[:0]
 	foundNames := map[string]bool{}
+	appendSearchResults := func(res *search.Result) {
+		data = append(data, res)
+		if !o.versions {
+			foundNames[res.Name] = true // If user hasn't requested all versions, only show the latest that matches
+		}
+	}
 	for _, r := range res {
 		if _, found := foundNames[r.Name]; found {
 			continue
 		}
 		v, err := semver.NewVersion(r.Chart.Version)
-		if err != nil || constraint.Check(v) {
-			data = append(data, r)
-			if !o.versions {
-				foundNames[r.Name] = true // If user hasn't requested all versions, only show the latest that matches
+
+		if err != nil {
+			// If the current version number check appears ErrSegmentStartsZero or ErrInvalidPrerelease error and not devel mode, ingore
+			if (err == semver.ErrSegmentStartsZero || err == semver.ErrInvalidPrerelease) && !o.devel {
+				continue
 			}
+			appendSearchResults(r)
+		} else if constraint.Check(v) {
+			appendSearchResults(r)
 		}
 	}
 


Test patch:
diff --git a/cmd/helm/search_repo_test.go b/cmd/helm/search_repo_test.go
index 39c9c53f5..86519cd42 100644
--- a/cmd/helm/search_repo_test.go
+++ b/cmd/helm/search_repo_test.go
@@ -68,6 +68,14 @@ func TestSearchRepositoriesCmd(t *testing.T) {
 		name:   "search for 'maria', expect valid json output",
 		cmd:    "search repo maria --output json",
 		golden: "output/search-output-json.txt",
+	}, {
+		name:   "search for 'maria', expect one match with semver begin with zero development version",
+		cmd:    "search repo maria --devel",
+		golden: "output/search-semver-pre-zero-devel-release.txt",
+	}, {
+		name:   "search for 'nginx-ingress', expect one match with invalid development pre version",
+		cmd:    "search repo nginx-ingress --devel",
+		golden: "output/search-semver-pre-invalid-release.txt",
 	}, {
 		name:   "search for 'alpine', expect valid yaml output",
 		cmd:    "search repo alpine --output yaml",
diff --git a/cmd/helm/testdata/helmhome/helm/repository/testing-index.yaml b/cmd/helm/testdata/helmhome/helm/repository/testing-index.yaml
index e00e7de79..d76501e57 100644
--- a/cmd/helm/testdata/helmhome/helm/repository/testing-index.yaml
+++ b/cmd/helm/testdata/helmhome/helm/repository/testing-index.yaml
@@ -55,3 +55,33 @@ entries:
         - name: Bitnami
           email: containers@bitnami.com
       icon: ""
+    - name: mariadb
+      url: https://charts.helm.sh/stable/mariadb-0.3.0-0565674.tgz
+      checksum: 65229f6de44a2be9f215d11dbff311673fc8ba56
+      home: https://mariadb.org
+      sources:
+        - https://github.com/bitnami/bitnami-docker-mariadb
+      version: 0.3.0-0565674
+      description: Chart for MariaDB
+      keywords:
+        - mariadb
+        - mysql
+        - database
+        - sql
+      maintainers:
+        - name: Bitnami
+          email: containers@bitnami.com
+      icon: ""
+  nginx-ingress:
+    - name: nginx-ingress
+      url: https://github.com/kubernetes/ingress-nginx/ingress-a.b.c.sdfsdf.tgz
+      checksum: 25229f6de44a2be9f215d11dbff31167ddc8ba56
+      home: https://github.com/kubernetes/ingress-nginx
+      sources:
+        - https://github.com/kubernetes/ingress-nginx
+      version: a.b.c.sdfsdf
+      description: Chart for nginx-ingress
+      keywords:
+        - ingress
+        - nginx
+      icon: ""
diff --git a/cmd/helm/testdata/output/search-semver-pre-invalid-release.txt b/cmd/helm/testdata/output/search-semver-pre-invalid-release.txt
new file mode 100644
index 000000000..ea39e2978
--- /dev/null
+++ b/cmd/helm/testdata/output/search-semver-pre-invalid-release.txt
@@ -0,0 +1,2 @@
+NAME                 	CHART VERSION	APP VERSION	DESCRIPTION            
+testing/nginx-ingress	a.b.c.sdfsdf 	           	Chart for nginx-ingress
diff --git a/cmd/helm/testdata/output/search-semver-pre-zero-devel-release.txt b/cmd/helm/testdata/output/search-semver-pre-zero-devel-release.txt
new file mode 100644
index 000000000..971c6523e
--- /dev/null
+++ b/cmd/helm/testdata/output/search-semver-pre-zero-devel-release.txt
@@ -0,0 +1,2 @@
+NAME           	CHART VERSION	APP VERSION	DESCRIPTION      
+testing/mariadb	0.3.0-0565674	           	Chart for MariaDB