Skip to content

Commit da9a61c

Browse files
authored
feat: add stars field to ArtifactHub package structs and update scoring logic (#355)
1 parent 0affeff commit da9a61c

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

pkg/helm/artifacthub.go

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type ArtifactHubHelmPackage struct {
110110
AvailableVersions []AvailableVersion `json:"available_versions"`
111111
Maintainers []Maintainer `json:"maintainers"`
112112
Links []Link `json:"links"`
113+
Stars int `json:"stars"`
113114
}
114115

115116
// AvailableVersion is a sub struct of ArtifactHubHelmPackage and provides a version that is available for a given helm chart.

pkg/helm/artifacthub_cached.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type ArtifactHubCachedPackage struct {
6161
Links []Link `json:"links"`
6262
Maintainers []Maintainer `json:"maintainers"`
6363
Deprecated bool `json:"deprecated"`
64+
Stars int `json:"stars"`
6465
}
6566

6667
// ArtifactHubCachedRepository is a sub-struct of the Package struct, and represents the repository containing the package.
@@ -126,6 +127,7 @@ func (ac *ArtifactHubCachedPackageClient) List() ([]ArtifactHubHelmPackage, erro
126127
HomeURL: cachedPackage.HomeURL,
127128
Links: cachedPackage.Links,
128129
Official: cachedPackage.Official,
130+
Stars: cachedPackage.Stars,
129131
Repository: ArtifactHubRepository{
130132
Name: cachedPackage.Repository.Name,
131133
URL: cachedPackage.Repository.URL,

pkg/helm/findscore.go

+40-6
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,53 @@ import (
2323
"k8s.io/klog/v2"
2424
)
2525

26+
const useStarCountThreshold = 10
27+
28+
type packageKey struct {
29+
Name string
30+
Repository string
31+
}
32+
2633
// FindBestArtifactHubMatch takes the helm releases found in the cluster and attempts to match those to a package in artifacthub
2734
func FindBestArtifactHubMatch(clusterRelease *release.Release, ahubPackages []ArtifactHubHelmPackage) *output.ReleaseOutput {
28-
var highScore float32
29-
var highScorePackage ArtifactHubHelmPackage
35+
packagesByName := map[packageKey]ArtifactHubHelmPackage{}
36+
packageScores := map[packageKey]float32{}
37+
packageStars := map[packageKey]int{}
38+
var useStars bool
3039
for _, p := range ahubPackages {
31-
var score float32
3240
if p.Name != clusterRelease.Chart.Metadata.Name {
3341
continue
3442
}
35-
score = scoreChartSimilarity(clusterRelease, p)
43+
44+
key := packageKey{Name: p.Name, Repository: p.Repository.Name}
45+
packageScores[key] = scoreChartSimilarity(clusterRelease, p)
46+
packagesByName[key] = p
47+
packageStars[key] = p.Stars
48+
49+
if p.Stars >= useStarCountThreshold {
50+
useStars = true // If any package has more than 10 stars, we add a point to the highest star package
51+
}
52+
}
53+
54+
var highestStarPackageName *packageKey
55+
var highStars int
56+
for p, stars := range packageStars {
57+
if stars > highStars {
58+
highStars = stars
59+
highestStarPackageName = &p
60+
}
61+
}
62+
63+
var highScore float32
64+
var highScorePackage ArtifactHubHelmPackage
65+
for k, score := range packageScores {
66+
if useStars && highestStarPackageName != nil && k == *highestStarPackageName {
67+
klog.V(10).Infof("adding a point to the highest star package: %s:%s", k.Repository, k.Name)
68+
score++ // Add a point to the highest star package
69+
}
3670
if score > highScore {
3771
highScore = score
38-
highScorePackage = p
72+
highScorePackage = packagesByName[k]
3973
}
4074
}
4175
klog.V(10).Infof("highScore for '%s': %f, highScorePackage Repo: %s", clusterRelease.Chart.Metadata.Name, highScore, highScorePackage.Repository.Name)
@@ -122,7 +156,7 @@ func scoreChartSimilarity(release *release.Release, pkg ArtifactHubHelmPackage)
122156
klog.V(10).Infof("+1.5 score for %s, preferred repo (ahub package repo %s)", release.Chart.Metadata.Name, pkg.Repository.Name)
123157
ret += 1.5
124158
}
125-
klog.V(10).Infof("calculated score repo: %s, release: %s, score: %f\n\n", pkg.Repository.Name, release.Name, ret)
159+
klog.V(10).Infof("calculated score repo: %s, release: %s, stars: %d, score: %f\n\n", pkg.Repository.Name, release.Name, pkg.Stars, ret)
126160
return ret
127161
}
128162

0 commit comments

Comments
 (0)