Merge pull request #11112 from felipecrs/filter-dup-repos

perf(dep-up): do not update the same repo multiple times
pull/30576/head
Joe Julian 7 months ago committed by GitHub
commit 0fcd1d33b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -659,10 +659,28 @@ func (m *Manager) UpdateRepositories() error {
return nil
}
// Filter out duplicate repos by URL, including those with trailing slashes.
func dedupeRepos(repos []*repo.Entry) []*repo.Entry {
seen := make(map[string]*repo.Entry)
for _, r := range repos {
// Normalize URL by removing trailing slashes.
seenURL := strings.TrimSuffix(r.URL, "/")
seen[seenURL] = r
}
var unique []*repo.Entry
for _, r := range seen {
unique = append(unique, r)
}
return unique
}
func (m *Manager) parallelRepoUpdate(repos []*repo.Entry) error {
var wg sync.WaitGroup
for _, c := range repos {
localRepos := dedupeRepos(repos)
for _, c := range localRepos {
r, err := repo.NewChartRepository(c, m.Getters)
if err != nil {
return err

@ -26,6 +26,7 @@ import (
"helm.sh/helm/v4/pkg/chart/loader"
chartutil "helm.sh/helm/v4/pkg/chart/util"
"helm.sh/helm/v4/pkg/getter"
"helm.sh/helm/v4/pkg/repo"
"helm.sh/helm/v4/pkg/repo/repotest"
)
@ -593,3 +594,72 @@ func TestKey(t *testing.T) {
}
}
}
// Test dedupeRepos tests that the dedupeRepos function correctly deduplicates
func TestDedupeRepos(t *testing.T) {
tests := []struct {
name string
repos []*repo.Entry
want []*repo.Entry
}{
{
name: "no duplicates",
repos: []*repo.Entry{
{
URL: "https://example.com/charts",
},
{
URL: "https://example.com/charts2",
},
},
want: []*repo.Entry{
{
URL: "https://example.com/charts",
},
{
URL: "https://example.com/charts2",
},
},
},
{
name: "duplicates",
repos: []*repo.Entry{
{
URL: "https://example.com/charts",
},
{
URL: "https://example.com/charts",
},
},
want: []*repo.Entry{
{
URL: "https://example.com/charts",
},
},
},
{
name: "duplicates with trailing slash",
repos: []*repo.Entry{
{
URL: "https://example.com/charts",
},
{
URL: "https://example.com/charts/",
},
},
want: []*repo.Entry{
{
// the last one wins
URL: "https://example.com/charts/",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := dedupeRepos(tt.repos); !reflect.DeepEqual(got, tt.want) {
t.Errorf("received:\n%v\nwant:\n%v", got, tt.want)
}
})
}
}

Loading…
Cancel
Save