Skip to content

Commit

Permalink
refactor: extract gitutil.IsGitURL helper
Browse files Browse the repository at this point in the history
Addresses helm#11258 (comment)

Signed-off-by: Dominykas Blyžė <hello@dominykas.com>
  • Loading branch information
dominykas committed Feb 6, 2024
1 parent c53f14a commit f28781f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
15 changes: 7 additions & 8 deletions pkg/gitutils/gitutils.go → internal/gitutil/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

/*
Package engine implements the Go text template engine as needed for Helm.
When Helm renders templates it does so with additional functions and different
modes (e.g., strict, lint mode). This package handles the helm specific
implementation.
*/
package gitutils
package gitutil

import (
"os"
"strings"

"github.com/Masterminds/vcs"
)
Expand All @@ -46,3 +40,8 @@ func HasGitReference(gitRepo, ref, repoName string) (bool, error) {
defer os.RemoveAll(local)
return repo.IsReference(ref), nil
}

func IsGitURL(url string) bool {

return strings.HasPrefix(url, "git://")
}
39 changes: 39 additions & 0 deletions internal/gitutil/gitutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gitutil

import (
"testing"
)

func TestIsGitUrl(t *testing.T) {
// Test table: Given url, IsGitURL should return expect.
tests := []struct {
url string
expect bool
}{
{"oci://example.com/example/chart", false},
{"git://example.com/example/chart", true},
{"git://https://example.com/example/chart", true},
}

for _, test := range tests {
if IsGitURL(test.url) != test.expect {
t.Errorf("Expected %t for %s", test.expect, test.url)
}
}
}
6 changes: 3 additions & 3 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/pkg/errors"

"helm.sh/helm/v3/internal/gitutil"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/gitutils"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/provenance"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
)

var hasGitReference = gitutils.HasGitReference
var hasGitReference = gitutil.HasGitReference

// Resolver resolves dependencies from semantic version ranges to a particular version.
type Resolver struct {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
continue
}

if strings.HasPrefix(d.Repository, "git://") {
if gitutil.IsGitURL(d.Repository) {

found, err := hasGitReference(strings.TrimPrefix(d.Repository, "git://"), d.Version, d.Name)

Expand Down
5 changes: 3 additions & 2 deletions pkg/downloader/chart_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
giturls "github.com/whilp/git-urls"

"helm.sh/helm/v3/internal/fileutil"
"helm.sh/helm/v3/internal/gitutil"
"helm.sh/helm/v3/internal/urlutil"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/helmpath"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven

scheme := ""

if strings.HasPrefix(ref, "git://") {
if gitutil.IsGitURL(ref) {
scheme = "git"
} else {
scheme = u.Scheme
Expand Down Expand Up @@ -207,7 +208,7 @@ func (c *ChartDownloader) getOciURI(ref, version string, u *url.URL) (*url.URL,
// - If version is empty, this will return the URL for the latest version
// - If no version can be found, an error is returned
func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, error) {
if strings.HasPrefix(ref, "git://") {
if gitutil.IsGitURL(ref) {
gitURL := strings.TrimPrefix(ref, "git://")
u, err := giturls.Parse(gitURL)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/downloader/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pkg/errors"
"sigs.k8s.io/yaml"

"helm.sh/helm/v3/internal/gitutil"
"helm.sh/helm/v3/internal/resolver"
"helm.sh/helm/v3/internal/third_party/dep/fs"
"helm.sh/helm/v3/internal/urlutil"
Expand Down Expand Up @@ -353,7 +354,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error {
getter.WithTagName(version))
}

if strings.HasPrefix(churl, "git://") {
if gitutil.IsGitURL(churl) {
version = dep.Version

dl.Options = append(dl.Options, getter.WithTagName(version))
Expand Down Expand Up @@ -489,7 +490,7 @@ Loop:
}

// If repo is from git url, continue
if strings.HasPrefix(dd.Repository, "git://") {
if gitutil.IsGitURL(dd.Repository) {
continue
}

Expand Down Expand Up @@ -613,7 +614,7 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string,
// if dep chart is from a git url, assume it is valid for now.
// if the repo does not exist then it will later error when we try to fetch branches and tags.
// we could check for the repo existence here, but trying to avoid another git request.
if strings.HasPrefix(dd.Repository, "git://") {
if gitutil.IsGitURL(dd.Repository) {
if m.Debug {
fmt.Fprintf(m.Out, "Repository from git url: %s\n", strings.TrimPrefix(dd.Repository, "git:"))
}
Expand Down Expand Up @@ -731,7 +732,7 @@ func (m *Manager) parallelRepoUpdate(repos []*repo.Entry) error {
//
// If it finds a URL that is "relative", it will prepend the repoURL.
func (m *Manager) findChartURL(name, version, repoURL string, repos map[string]*repo.ChartRepository) (url, username, password string, insecureskiptlsverify, passcredentialsall bool, caFile, certFile, keyFile string, err error) {
if strings.HasPrefix(repoURL, "git://") {
if gitutil.IsGitURL(repoURL) {
return repoURL, "", "", false, false, "", "", "", nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/getter/gitgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/Masterminds/vcs"

"helm.sh/helm/v3/internal/fileutil"
"helm.sh/helm/v3/internal/gitutil"
)

// GitGetter is the default HTTP(/S) backend handler
Expand Down

0 comments on commit f28781f

Please sign in to comment.