Skip to content

Commit d238a0a

Browse files
committed
Treat go.mod as a lockfile for commands that need resolved dependencies
Go has no traditional lockfile checked into repos. go.mod contains pinned versions and is the closest equivalent. SQL queries filtering on kind='lockfile' now also accept Go manifests so that stale, vulns sync, and vuln stats work for Go projects. Also fixes isResolvedDependency comparing against 'Go' when the stored ecosystem value is 'golang'.
1 parent 376652a commit d238a0a

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

cmd/analysis_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd_test
33
import (
44
"bytes"
55
"encoding/json"
6+
"os"
67
"strings"
78
"testing"
89

@@ -863,4 +864,53 @@ func TestStaleCommand(t *testing.T) {
863864
t.Error("expected 'name' field in stale JSON")
864865
}
865866
})
867+
868+
t.Run("includes go.mod dependencies", func(t *testing.T) {
869+
repoDir := createTestRepo(t)
870+
871+
goMod, err := os.ReadFile("testdata/ades-go.mod")
872+
if err != nil {
873+
t.Fatalf("failed to read fixture: %v", err)
874+
}
875+
addFileAndCommit(t, repoDir, "go.mod", string(goMod), "Add go.mod")
876+
877+
cleanup := chdir(t, repoDir)
878+
defer cleanup()
879+
880+
rootCmd := cmd.NewRootCmd()
881+
rootCmd.SetArgs([]string{"init"})
882+
if err := rootCmd.Execute(); err != nil {
883+
t.Fatalf("init failed: %v", err)
884+
}
885+
886+
var stdout bytes.Buffer
887+
rootCmd = cmd.NewRootCmd()
888+
rootCmd.SetArgs([]string{"stale", "--days", "0", "--format", "json"})
889+
rootCmd.SetOut(&stdout)
890+
891+
if err := rootCmd.Execute(); err != nil {
892+
t.Fatalf("stale failed: %v", err)
893+
}
894+
895+
var result []map[string]interface{}
896+
if err := json.Unmarshal(stdout.Bytes(), &result); err != nil {
897+
t.Fatalf("failed to parse JSON: %v", err)
898+
}
899+
900+
if len(result) == 0 {
901+
t.Fatal("expected Go dependencies in stale output, got none")
902+
}
903+
904+
// Verify a known go.mod dependency appears
905+
found := false
906+
for _, entry := range result {
907+
if name, ok := entry["name"].(string); ok && name == "golang.org/x/mod" {
908+
found = true
909+
break
910+
}
911+
}
912+
if !found {
913+
t.Error("expected golang.org/x/mod in stale output")
914+
}
915+
})
866916
}

cmd/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func shortSHA(sha string) string {
5050
}
5151

5252
func isResolvedDependency(d database.Dependency) bool {
53-
return d.Requirement != "" && (d.ManifestKind == "lockfile" || d.Ecosystem == "Go")
53+
return d.Requirement != "" && (d.ManifestKind == "lockfile" || d.Ecosystem == "golang")
5454
}
5555

5656
func filterByEcosystem(deps []database.Dependency, ecosystem string) []database.Dependency {

internal/database/queries.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ func (db *DB) GetStaleDependencies(branchID int64, ecosystem string, days int) (
683683
JOIN branch_commits bc ON bc.commit_id = ds.commit_id
684684
WHERE bc.branch_id = ?
685685
AND bc.position = (SELECT MAX(position) FROM branch_commits WHERE branch_id = ?)
686-
AND m.kind = 'lockfile'
686+
AND (m.kind = 'lockfile' OR (m.kind = 'manifest' AND m.ecosystem = 'golang'))
687687
),
688688
last_changed AS (
689689
SELECT dc.name, m.path, MAX(c.committed_at) as last_changed
@@ -1581,7 +1581,7 @@ func (db *DB) GetVulnSyncStatus(branchID int64) ([]VulnSyncStatus, error) {
15811581
JOIN branch_commits bc ON bc.commit_id = ds.commit_id
15821582
JOIN manifests m ON m.id = ds.manifest_id
15831583
WHERE bc.branch_id = ?
1584-
AND m.kind = 'lockfile'
1584+
AND (m.kind = 'lockfile' OR (m.kind = 'manifest' AND m.ecosystem = 'golang'))
15851585
AND ds.ecosystem IS NOT NULL AND ds.ecosystem != ''
15861586
ORDER BY ds.ecosystem, ds.name
15871587
`, branchID)
@@ -1705,7 +1705,7 @@ func (db *DB) GetVulnerabilityStats(branchID int64) (map[string]int, error) {
17051705
JOIN manifests m ON m.id = ds.manifest_id
17061706
WHERE bc.branch_id = ?
17071707
AND bc.position = (SELECT MAX(position) FROM branch_commits WHERE branch_id = ?)
1708-
AND m.kind = 'lockfile'
1708+
AND (m.kind = 'lockfile' OR (m.kind = 'manifest' AND m.ecosystem = 'golang'))
17091709
GROUP BY v.severity
17101710
`, branchID, branchID)
17111711
if err != nil {

0 commit comments

Comments
 (0)