Fix repo prefix searchbug

pull/467/head
jackgr 9 years ago
parent 00ad7d1cad
commit 20c1ed3f79

@ -92,10 +92,12 @@ func (rs *inmemRepoService) GetRepoByChartURL(URL string) (IRepo, error) {
rs.RLock() rs.RLock()
defer rs.RUnlock() defer rs.RUnlock()
cSplit := strings.Split(URL, "/")
var found IRepo var found IRepo
for _, r := range rs.repositories { for _, r := range rs.repositories {
rURL := r.GetURL() rURL := r.GetURL()
if strings.HasPrefix(URL, rURL) { rSplit := strings.Split(rURL, "/")
if hasPrefix(cSplit, rSplit) {
if found == nil || len(found.GetURL()) < len(rURL) { if found == nil || len(found.GetURL()) < len(rURL) {
found = r found = r
} }
@ -109,6 +111,20 @@ func (rs *inmemRepoService) GetRepoByChartURL(URL string) (IRepo, error) {
return found, nil return found, nil
} }
func hasPrefix(cSplit, rSplit []string) bool {
if len(rSplit) > len(cSplit) {
return false
}
for i := range rSplit {
if rSplit[i] != cSplit[i] {
return false
}
}
return true
}
// DeleteRepo removes a known repository from the list // DeleteRepo removes a known repository from the list
func (rs *inmemRepoService) DeleteRepo(URL string) error { func (rs *inmemRepoService) DeleteRepo(URL string) error {
rs.Lock() rs.Lock()

@ -54,7 +54,7 @@ func TestService(t *testing.T) {
t.Fatalf("invalid repo returned; want: %#v, have %#v.", tr, r1) t.Fatalf("invalid repo returned; want: %#v, have %#v.", tr, r1)
} }
URL := GCSPublicRepoURL + TestArchiveName URL := GCSPublicRepoURL + "/" + TestArchiveName
r2, err := rs.GetRepoByChartURL(URL) r2, err := rs.GetRepoByChartURL(URL)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

@ -25,7 +25,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strings"
"sync" "sync"
) )
@ -117,35 +116,21 @@ func (rp *repoProvider) createRepo(cr IChartRepo) (IChartRepo, error) {
return cr, nil return cr, nil
} }
// GetRepoByChartURL returns the repository whose URL is a prefix of the given URL. // GetRepoByChartURL returns the repository that backs a given chart URL.
func (rp *repoProvider) GetRepoByChartURL(URL string) (IChartRepo, error) { func (rp *repoProvider) GetRepoByChartURL(URL string) (IChartRepo, error) {
rp.Lock() rp.Lock()
defer rp.Unlock() defer rp.Unlock()
if r := rp.findRepoByChartURL(URL); r != nil {
return r, nil
}
cr, err := rp.rs.GetRepoByChartURL(URL) cr, err := rp.rs.GetRepoByChartURL(URL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return rp.createRepoByType(cr) if r, ok := rp.repos[cr.GetURL()]; ok {
} return r, nil
func (rp *repoProvider) findRepoByChartURL(URL string) IChartRepo {
var found IChartRepo
for _, r := range rp.repos {
rURL := r.GetURL()
if strings.HasPrefix(URL, rURL) {
if found == nil || len(found.GetURL()) < len(rURL) {
found = r
}
}
} }
return found return rp.createRepoByType(cr)
} }
// GetChartByReference maps the supplied chart reference into a fully qualified // GetChartByReference maps the supplied chart reference into a fully qualified

@ -62,7 +62,7 @@ func TestRepoProvider(t *testing.T) {
} }
wantRepo := haveRepo wantRepo := haveRepo
URL := GCSPublicRepoURL + TestArchiveName URL := GCSPublicRepoURL + "/" + TestArchiveName
haveRepo, err = rp.GetRepoByChartURL(URL) haveRepo, err = rp.GetRepoByChartURL(URL)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

Loading…
Cancel
Save