Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't filter when there's a single asset #94

Merged
merged 3 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 50 additions & 43 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,60 @@ func (g FilteredAsset) String() string { return g.Name }
// in case it can't determine it
func FilterAssets(repoName string, as []*Asset) (*FilteredAsset, error) {
matches := []*FilteredAsset{}
scores := map[string]int{}
scores[repoName] = 1
for _, os := range config.GetOS() {
scores[os] = 10
}
for _, arch := range config.GetArch() {
scores[arch] = 5
}
scoreKeys := []string{}
for key := range scores {
scoreKeys = append(scoreKeys, key)
}
for _, a := range as {
lowerName := strings.ToLower(a.Name)
lowerURLPathBasename := path.Base(strings.ToLower(a.URL))
filetype.GetType(lowerName)
highestScoreForAsset := 0
gf := &FilteredAsset{RepoName: repoName, Name: a.Name, URL: a.URL, score: 0}
for _, candidate := range []string{lowerName, lowerURLPathBasename} {
if bstrings.ContainsAny(candidate, scoreKeys) &&
isSupportedExt(candidate) {
for toMatch, score := range scores {
if strings.Contains(candidate, toMatch) {
gf.score += score
// if there's a single file don't filter by score
if len(as) == 1 {
a := as[0]
matches = append(matches, &FilteredAsset{RepoName: repoName, Name: a.Name, URL: a.URL, score: 0})
} else {
scores := map[string]int{}
scores[repoName] = 1
for _, os := range config.GetOS() {
scores[os] = 10
}
for _, arch := range config.GetArch() {
scores[arch] = 5
}
scoreKeys := []string{}
for key := range scores {
scoreKeys = append(scoreKeys, key)
}
for _, a := range as {
lowerName := strings.ToLower(a.Name)
lowerURLPathBasename := path.Base(strings.ToLower(a.URL))
filetype.GetType(lowerName)
highestScoreForAsset := 0
gf := &FilteredAsset{RepoName: repoName, Name: a.Name, URL: a.URL, score: 0}
for _, candidate := range []string{lowerName, lowerURLPathBasename} {
if bstrings.ContainsAny(candidate, scoreKeys) &&
isSupportedExt(candidate) {
for toMatch, score := range scores {
if strings.Contains(candidate, toMatch) {
gf.score += score
}
}
if gf.score > highestScoreForAsset {
highestScoreForAsset = gf.score
gf.Name = candidate
}
}
if gf.score > highestScoreForAsset {
highestScoreForAsset = gf.score
gf.Name = candidate
}
}
if highestScoreForAsset > 0 {
matches = append(matches, gf)
}
}
if highestScoreForAsset > 0 {
matches = append(matches, gf)
}
}
highestAssetScore := 0
for i := range matches {
if matches[i].score > highestAssetScore {
highestAssetScore = matches[i].score
highestAssetScore := 0
for i := range matches {
if matches[i].score > highestAssetScore {
highestAssetScore = matches[i].score
}
}
}
for i := len(matches) - 1; i >= 0; i-- {
if matches[i].score < highestAssetScore {
log.Debugf("Removing %v with score %v lower than %v", matches[i].Name, matches[i].score, highestAssetScore)
matches = append(matches[:i], matches[i+1:]...)
} else {
log.Debugf("Keeping %v (URL %v) with highest score %v", matches[i].Name, matches[i].URL, matches[i].score)
for i := len(matches) - 1; i >= 0; i-- {
if matches[i].score < highestAssetScore {
log.Debugf("Removing %v with score %v lower than %v", matches[i].Name, matches[i].score, highestAssetScore)
matches = append(matches[:i], matches[i+1:]...)
} else {
log.Debugf("Keeping %v (URL %v) with highest score %v", matches[i].Name, matches[i].URL, matches[i].score)
}
}
}

Expand Down Expand Up @@ -199,6 +205,7 @@ func processReader(repoName string, name string, r io.Reader) (string, io.Reader

type processorFunc func(repoName string, r io.Reader) (string, io.Reader, error)
var processor processorFunc

switch t {
case matchers.TypeGz:
processor = processGz
Expand Down
24 changes: 24 additions & 0 deletions pkg/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,27 @@ func TestFilterAssets(t *testing.T) {
}

}

func TestFilterSingleAsset(t *testing.T) {
type args struct {
repoName string
as []*Asset
}
cases := []struct {
in args
out string
}{
{args{"cli", []*Asset{
{Name: "dapr", URL: ""},
}}, "dapr"},
}

for _, c := range cases {
if n, err := FilterAssets(c.in.repoName, c.in.as); err != nil {
t.Fatalf("Error filtering asset [%v]", err)
} else if n.Name != c.out {
t.Fatalf("Error filtering %+v: %+v does not match %s", c.in, n, c.out)
}
}

}