mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(packages): list only cache artifacts in the package rows
The rows layout listed Go .mod and .info files. They are never scanned, so they showed as pending and made the page total larger than /api/stats. The group query now applies the same artifact filter as GetStats.
This commit is contained in:
@@ -148,3 +148,32 @@ func TestRowsLayoutSortsByRisk(t *testing.T) {
|
||||
t.Errorf("risk desc names = %v, want the clean package last", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRowsLayoutExcludesGoMetadataFiles(t *testing.T) {
|
||||
a := newVerdictApp(t)
|
||||
before, _ := getJSON(t, a, "/api/packages?layout=rows")
|
||||
now := time.Now()
|
||||
for _, name := range []string{"example.com/m/@v/v1.0.0.mod", "example.com/m/@v/v1.0.0.info", "example.com/m/@v/v1.0.0.zip", "example.com/only/@v/v0.1.0.mod"} {
|
||||
if err := a.metadata.SavePackage(context.Background(), &metadata.Package{
|
||||
Registry: "go", Name: name, Version: "v1.0.0", StorageKey: "go/" + name, Size: 1, CachedAt: now, LastAccessed: now,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
out, _ := getJSON(t, a, "/api/packages?layout=rows®istry=go")
|
||||
if got := rowNames(out); !slices.Equal(got, []string{"example.com/m"}) {
|
||||
t.Errorf("go rows = %v, want only the .zip", got)
|
||||
}
|
||||
if out["total"].(float64) != 1 {
|
||||
t.Errorf("total = %v, want 1", out["total"])
|
||||
}
|
||||
if pending := out["counts"].(map[string]any)["verdicts"].(map[string]any)["pending"].(float64); pending != 1 {
|
||||
t.Errorf("pending count = %v, want 1", pending)
|
||||
}
|
||||
|
||||
after, _ := getJSON(t, a, "/api/packages?layout=rows")
|
||||
if after["total"].(float64) != before["total"].(float64)+1 {
|
||||
t.Errorf("total = %v, want %v", after["total"], before["total"].(float64)+1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,9 @@ func (s *GORMStoreV2) registryNames() map[int32]string {
|
||||
|
||||
// groupFilter builds the WHERE clause shared by the group count, the group page and the row fetch.
|
||||
func (s *GORMStoreV2) groupFilter(opts metadata.GroupListOptions) (clause string, args []interface{}, empty bool, err error) {
|
||||
clause = "version NOT IN ?"
|
||||
args = []interface{}{metadata.MetadataVersions}
|
||||
// The artifact filter GetStats applies, so list totals and /api/stats agree.
|
||||
clause = "version NOT IN ? AND name NOT LIKE ? AND name NOT LIKE ?"
|
||||
args = []interface{}{metadata.MetadataVersions, "%.mod", "%.info"}
|
||||
if opts.Registry != "" {
|
||||
id, idErr := s.getRegistryID(opts.Registry)
|
||||
if idErr != nil {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package gormstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGroupListSkipsGoMetadataFiles(t *testing.T) {
|
||||
store := trendingStore(t)
|
||||
for _, name := range []string{"example.com/m/@v/v1.0.0.mod", "example.com/m/@v/v1.0.0.info", "example.com/m/@v/v1.0.0.zip"} {
|
||||
trendingPackage(t, store, "go", name, "v1.0.0")
|
||||
}
|
||||
for _, size := range []int{0, 10} {
|
||||
page, err := store.ListPackageGroups(context.Background(), metadata.GroupListOptions{PageSize: size, Page: 1})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, page.Packages, 1, "page size %d", size)
|
||||
require.Equal(t, "example.com/m", page.Packages[0].Name)
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ type groupAgg struct {
|
||||
func PageGroups(all []*Package, opts GroupListOptions) *GroupPage {
|
||||
groups := map[string]*groupAgg{}
|
||||
for _, p := range all {
|
||||
if IsMetadataVersion(p.Version) || (opts.Registry != "" && p.Registry != opts.Registry) || !MatchesQuery(p, opts.Query) {
|
||||
if IsMetadataEntry(p.Registry, p.Name, p.Version) || (opts.Registry != "" && p.Registry != opts.Registry) || !MatchesQuery(p, opts.Query) {
|
||||
continue
|
||||
}
|
||||
name := CleanName(p.Name)
|
||||
|
||||
Reference in New Issue
Block a user