Skip to content

Commit 3c57b05

Browse files
committed
Pass git-pkgs user-agent to all module API clients
Sets User-Agent to 'git-pkgs/{version}' when creating OSV and enrichment clients. Depends on new WithUserAgent options in the vulns, enrichment, and registries modules.
1 parent 227b7c6 commit 3c57b05

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

cmd/integrity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func runRegistryCheck(cmd *cobra.Command, deps []database.Dependency, format str
268268
}
269269

270270
// Create enrichment client
271-
client, err := enrichment.NewClient()
271+
client, err := enrichment.NewClient(enrichment.WithUserAgent("git-pkgs/" + version))
272272
if err != nil {
273273
return fmt.Errorf("creating enrichment client: %w", err)
274274
}

cmd/licenses.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import (
1919

2020
// NewEnrichmentClient is the constructor for the enrichment client.
2121
// Tests can replace this to avoid external API calls.
22-
var NewEnrichmentClient = enrichment.NewClient
22+
var NewEnrichmentClient = func(opts ...enrichment.Option) (enrichment.Client, error) {
23+
return enrichment.NewClient(opts...)
24+
}
2325

2426
func addLicensesCmd(parent *cobra.Command) {
2527
licensesCmd := &cobra.Command{
@@ -287,7 +289,7 @@ func getLicenseData(db *database.DB, purls []string, purlToDep map[string]databa
287289

288290
// Fetch uncached from API
289291
if len(uncachedPurls) > 0 {
290-
client, err := NewEnrichmentClient()
292+
client, err := NewEnrichmentClient(enrichment.WithUserAgent("git-pkgs/" + version))
291293
if err != nil {
292294
return nil, err
293295
}

cmd/licenses_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (m *mockEnrichmentClient) GetVersion(_ context.Context, _ string) (*enrichm
3939
// returns a mock, and returns a cleanup function to restore the original.
4040
func setMockEnrichment(packages map[string]*enrichment.PackageInfo) func() {
4141
orig := cmd.NewEnrichmentClient
42-
cmd.NewEnrichmentClient = func() (enrichment.Client, error) {
42+
cmd.NewEnrichmentClient = func(opts ...enrichment.Option) (enrichment.Client, error) {
4343
return &mockEnrichmentClient{packages: packages}, nil
4444
}
4545
return func() { cmd.NewEnrichmentClient = orig }

cmd/outdated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func getPackageData(db *database.DB, purls []string, purlToDep map[string]databa
211211

212212
// Fetch uncached from API
213213
if len(uncachedPurls) > 0 {
214-
client, err := enrichment.NewClient()
214+
client, err := enrichment.NewClient(enrichment.WithUserAgent("git-pkgs/" + version))
215215
if err != nil {
216216
return nil, err
217217
}
@@ -289,7 +289,7 @@ func findLatestAtDateCached(db *database.DB, ecosystem, name, purl string, atTim
289289
}
290290

291291
// Fall back to API
292-
client, err := enrichment.NewClient()
292+
client, err := enrichment.NewClient(enrichment.WithUserAgent("git-pkgs/" + version))
293293
if err != nil {
294294
return ""
295295
}

cmd/sbom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func getSBOMLicenseData(db *database.DB, purls []string, purlToDep map[string]da
206206

207207
// Fetch uncached from API
208208
if len(uncachedPurls) > 0 {
209-
client, err := enrichment.NewClient()
209+
client, err := enrichment.NewClient(enrichment.WithUserAgent("git-pkgs/" + version))
210210
if err != nil {
211211
return nil, err
212212
}

cmd/vulns.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func runVulnsSync(cmd *cobra.Command, args []string) error {
123123
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Syncing vulnerabilities for %d packages...\n", len(uniquePkgs))
124124
}
125125

126-
source := osv.New()
126+
source := osv.New(osv.WithUserAgent("git-pkgs/" + version))
127127
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
128128
defer cancel()
129129

@@ -377,7 +377,7 @@ func runVulnsScan(cmd *cobra.Command, args []string) error {
377377
}
378378

379379
func scanLive(deps []database.Dependency, minSeverity int) ([]VulnResult, error) {
380-
source := osv.New()
380+
source := osv.New(osv.WithUserAgent("git-pkgs/" + version))
381381
purls := make([]*purl.PURL, len(deps))
382382
for i, d := range deps {
383383
purls[i] = purl.MakePURL(d.Ecosystem, d.Name, d.Requirement)
@@ -708,7 +708,7 @@ func runVulnsShow(cmd *cobra.Command, args []string) error {
708708
ref, _ := cmd.Flags().GetString("ref")
709709
branchName, _ := cmd.Flags().GetString("branch")
710710

711-
source := osv.New()
711+
source := osv.New(osv.WithUserAgent("git-pkgs/" + version))
712712
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
713713
defer cancel()
714714

@@ -1451,7 +1451,7 @@ func runVulnsHistory(cmd *cobra.Command, args []string) error {
14511451
return fmt.Errorf("getting commits: %w", err)
14521452
}
14531453

1454-
source := osv.New()
1454+
source := osv.New(osv.WithUserAgent("git-pkgs/" + version))
14551455
var history []VulnHistoryEntry
14561456

14571457
for _, c := range commits {

0 commit comments

Comments
 (0)