mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(metadata): treat .mod and .info names as metadata for Go only
GetStats, the package list and the stats snapshots dropped any registry's names that end in .mod or .info. An npm or PyPI package with such a name vanished from the counts. The filter now matches IsMetadataEntry.
This commit is contained in:
@@ -50,8 +50,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) {
|
||||
// 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"}
|
||||
nameCond, nameArgs := notGoMetadataName()
|
||||
clause = "version NOT IN ? AND " + nameCond
|
||||
args = append([]interface{}{metadata.MetadataVersions}, nameArgs...)
|
||||
if opts.Registry != "" {
|
||||
id, idErr := s.getRegistryID(opts.Registry)
|
||||
if idErr != nil {
|
||||
|
||||
@@ -554,14 +554,8 @@ func (s *GORMStoreV2) ClearArtifactExpiry(ctx context.Context) (int64, error) {
|
||||
query := s.db.WithContext(ctx).Model(&PackageModel{}).
|
||||
Where("expires_at IS NOT NULL").
|
||||
Where("version NOT IN ?", metadata.MetadataVersions)
|
||||
// The name suffixes mark metadata for Go modules only.
|
||||
suffixConds := make([]string, 0, len(metadata.MetadataNameSuffixes))
|
||||
args := []interface{}{metadata.GoRegistry}
|
||||
for _, suffix := range metadata.MetadataNameSuffixes {
|
||||
suffixConds = append(suffixConds, "name LIKE ?")
|
||||
args = append(args, "%"+suffix)
|
||||
}
|
||||
query = query.Where("NOT (registry_id IN (SELECT id FROM registries WHERE name = ?) AND ("+strings.Join(suffixConds, " OR ")+"))", args...)
|
||||
cond, args := notGoMetadataName()
|
||||
query = query.Where(cond, args...)
|
||||
result := query.UpdateColumn("expires_at", nil)
|
||||
if result.Error != nil {
|
||||
return 0, errors.Wrap(result.Error, errors.ErrCodeStorageFailure, "failed to clear artifact expiry")
|
||||
@@ -629,7 +623,8 @@ func (s *GORMStoreV2) GetStats(ctx context.Context, registry string) (*metadata.
|
||||
query = query.Where("version NOT IN (?)", []string{"list", "latest", "metadata", "page"})
|
||||
|
||||
// Filter out Go module metadata files (.mod, .info) - only count actual packages (.zip)
|
||||
query = query.Where("name NOT LIKE ?", "%.mod").Where("name NOT LIKE ?", "%.info")
|
||||
cond, condArgs := notGoMetadataName()
|
||||
query = query.Where(cond, condArgs...)
|
||||
|
||||
// Filter by registry if specified
|
||||
if registry != "" {
|
||||
|
||||
@@ -20,3 +20,22 @@ func TestGroupListSkipsGoMetadataFiles(t *testing.T) {
|
||||
require.Equal(t, "example.com/m", page.Packages[0].Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataSuffixesAreGoOnly(t *testing.T) {
|
||||
store := trendingStore(t)
|
||||
ctx := context.Background()
|
||||
trendingPackage(t, store, "npm", "left.mod", "1.0.0")
|
||||
trendingPackage(t, store, "npm", "right.info", "1.0.0")
|
||||
trendingPackage(t, store, "pypi", "foo.mod", "foo.mod-1.0.tar.gz")
|
||||
trendingPackage(t, store, "go", "example.com/m/@v/v1.0.0.zip", "v1.0.0")
|
||||
trendingPackage(t, store, "go", "example.com/m/@v/v1.0.0.mod", "v1.0.0")
|
||||
trendingPackage(t, store, "go", "example.com/m/@v/v1.0.0.info", "v1.0.0")
|
||||
|
||||
stats, err := store.GetStats(ctx, "")
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 4, stats.TotalPackages)
|
||||
|
||||
page, err := store.ListPackageGroups(ctx, metadata.GroupListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, page.Packages, 4)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package gormstore
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/errors"
|
||||
@@ -20,8 +21,20 @@ const (
|
||||
// artifactPackages limits a query on packages to cache artifacts: no metadata
|
||||
// pages and no Go .mod/.info files, as GetStats counts them.
|
||||
func artifactPackages(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("version NOT IN ?", metadata.MetadataVersions).
|
||||
Where("name NOT LIKE ?", "%.mod").Where("name NOT LIKE ?", "%.info")
|
||||
cond, args := notGoMetadataName()
|
||||
return db.Where("version NOT IN ?", metadata.MetadataVersions).Where(cond, args...)
|
||||
}
|
||||
|
||||
// notGoMetadataName is the condition that drops Go .mod/.info rows; the suffixes mean
|
||||
// metadata for Go only, as metadata.IsMetadataEntry has it.
|
||||
func notGoMetadataName() (string, []interface{}) {
|
||||
conds := make([]string, 0, len(metadata.MetadataNameSuffixes))
|
||||
args := []interface{}{metadata.GoRegistry}
|
||||
for _, suffix := range metadata.MetadataNameSuffixes {
|
||||
conds = append(conds, "name LIKE ?")
|
||||
args = append(args, "%"+suffix)
|
||||
}
|
||||
return "NOT (registry_id IN (SELECT id FROM registries WHERE name = ?) AND (" + strings.Join(conds, " OR ") + "))", args
|
||||
}
|
||||
|
||||
func dayString(t time.Time) string { return t.UTC().Format(metadata.StatsSnapshotDateLayout) }
|
||||
|
||||
@@ -76,8 +76,11 @@ func checkBackfillStatsSnapshots(t *testing.T, store *GORMStoreV2) {
|
||||
|
||||
seedAt(t, store, "a", "1.0.0", 10, daysAgo(5))
|
||||
seedAt(t, store, "b", "1.0.0", 20, daysAgo(4))
|
||||
seedAt(t, store, "a", "list", 5, daysAgo(5)) // metadata page: never counted
|
||||
seedAt(t, store, "mod/@v/v1.mod", "v1", 7, daysAgo(5)) // Go .mod file: never counted
|
||||
seedAt(t, store, "a", "list", 5, daysAgo(5)) // metadata page: never counted
|
||||
require.NoError(t, store.SavePackage(ctx, &metadata.Package{ // Go .mod file: never counted
|
||||
Registry: "go", Name: "mod/@v/v1.mod", Version: "v1", StorageKey: "go/mod/@v/v1.mod",
|
||||
Size: 7, CachedAt: daysAgo(5), LastAccessed: daysAgo(5),
|
||||
}))
|
||||
seedAt(t, store, "early", "1.0.0", 40, daysAgo(2)) // cached and deleted the same day
|
||||
scanAt(t, store, "b", "1.0.0", daysAgo(4), "high") // first scan: vulnerability history starts here
|
||||
scanAt(t, store, "a", "1.0.0", daysAgo(3), "critical", "low") // critical wins over low
|
||||
|
||||
Reference in New Issue
Block a user