Skip to content

Commit 488b538

Browse files
committed
Plumb contexts everywhere
Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
1 parent 8bc7984 commit 488b538

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+248
-241
lines changed

pkg/advisory/create.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"fmt"
56
"sort"
67

@@ -16,7 +17,7 @@ type CreateOptions struct {
1617

1718
// Create creates a new advisory in the `advisories` section of the document at
1819
// the provided path.
19-
func Create(req Request, opts CreateOptions) error {
20+
func Create(ctx context.Context, req Request, opts CreateOptions) error {
2021
err := req.Validate()
2122
if err != nil {
2223
return err
@@ -28,7 +29,7 @@ func Create(req Request, opts CreateOptions) error {
2829
switch count {
2930
case 0:
3031
// i.e. no advisories file for this package yet
31-
return createAdvisoryConfig(opts.AdvisoryDocs, req)
32+
return createAdvisoryConfig(ctx, opts.AdvisoryDocs, req)
3233

3334
case 1:
3435
newAdvisoryID := req.VulnerabilityID
@@ -52,13 +53,13 @@ func Create(req Request, opts CreateOptions) error {
5253

5354
return advisories, nil
5455
})
55-
err := documents.Update(u)
56+
err := documents.Update(ctx, u)
5657
if err != nil {
5758
return fmt.Errorf("unable to create advisory %q for %q: %w", newAdvisoryID, req.Package, err)
5859
}
5960

6061
// Update the schema version to the latest version.
61-
err = documents.Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
62+
err = documents.Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
6263
if err != nil {
6364
return fmt.Errorf("unable to update schema version for %q: %w", req.Package, err)
6465
}
@@ -69,15 +70,15 @@ func Create(req Request, opts CreateOptions) error {
6970
return fmt.Errorf("cannot create advisory: found %d advisory documents for package %q", count, req.Package)
7071
}
7172

72-
func createAdvisoryConfig(documents *configs.Index[v2.Document], req Request) error {
73+
func createAdvisoryConfig(ctx context.Context, documents *configs.Index[v2.Document], req Request) error {
7374
newAdvisoryID := req.VulnerabilityID
7475
newAdvisory := v2.Advisory{
7576
ID: newAdvisoryID,
7677
Aliases: req.Aliases,
7778
Events: []v2.Event{req.Event},
7879
}
7980

80-
err := documents.Create(fmt.Sprintf("%s.advisories.yaml", req.Package), v2.Document{
81+
err := documents.Create(ctx, fmt.Sprintf("%s.advisories.yaml", req.Package), v2.Document{
8182
SchemaVersion: v2.SchemaVersion,
8283
Package: v2.Package{
8384
Name: req.Package,

pkg/advisory/create_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"os"
56
"testing"
67
"time"
@@ -190,10 +191,10 @@ func TestCreate(t *testing.T) {
190191
t.Run(tt.name, func(t *testing.T) {
191192
// We want a fresh memfs for each test case.
192193
fsys := memfs.New(dirFS)
193-
advisoryDocs, err := v2.NewIndex(fsys)
194+
advisoryDocs, err := v2.NewIndex(context.Background(), fsys)
194195
require.NoError(t, err)
195196

196-
err = Create(tt.req, CreateOptions{
197+
err = Create(context.Background(), tt.req, CreateOptions{
197198
AdvisoryDocs: advisoryDocs,
198199
})
199200

pkg/advisory/diff_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"path/filepath"
56
"testing"
67
"time"
@@ -248,9 +249,9 @@ func TestIndexDiff(t *testing.T) {
248249
bDir := filepath.Join("testdata", "diff", tt.name, "b")
249250
aFsys := rwos.DirFS(aDir)
250251
bFsys := rwos.DirFS(bDir)
251-
aIndex, err := v2.NewIndex(aFsys)
252+
aIndex, err := v2.NewIndex(context.Background(), aFsys)
252253
require.NoError(t, err)
253-
bIndex, err := v2.NewIndex(bFsys)
254+
bIndex, err := v2.NewIndex(context.Background(), bFsys)
254255
require.NoError(t, err)
255256

256257
diff := IndexDiff(aIndex, bIndex)

pkg/advisory/discover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (opts DiscoverOptions) discoverMatchesForPackage(ctx context.Context, pkg s
101101

102102
for i := range matches {
103103
match := matches[i]
104-
err := Create(Request{
104+
err := Create(ctx, Request{
105105
Package: pkg,
106106
VulnerabilityID: match.Vulnerability.ID,
107107
Aliases: nil,

pkg/advisory/discover_aliases.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func (opts *DiscoverAliasesOptions) discoverAliasesForAdvisoriesWithCVEIDs(ctx c
5454

5555
return advisories, nil
5656
})
57-
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(u)
57+
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, u)
5858
if err != nil {
5959
return err
6060
}
6161

6262
// Update the schema version to the latest version.
63-
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
63+
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
6464
if err != nil {
6565
return fmt.Errorf("unable to update schema version for %q: %w", doc.Name(), err)
6666
}
@@ -120,13 +120,13 @@ func (opts *DiscoverAliasesOptions) discoverAliasesForAdvisoriesWithGHSAIDs(ctx
120120

121121
return advisories, nil
122122
})
123-
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(u)
123+
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, u)
124124
if err != nil {
125125
return err
126126
}
127127

128128
// Update the schema version to the latest version.
129-
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
129+
err = opts.AdvisoryDocs.Select().WhereName(doc.Name()).Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
130130
if err != nil {
131131
return fmt.Errorf("unable to update schema version for %q: %w", doc.Name(), err)
132132
}

pkg/advisory/discover_aliases_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestDiscoverAliases(t *testing.T) {
140140
// back to disk.
141141
fsys := memfs.New(os.DirFS("testdata/discover_aliases/advisories"))
142142

143-
advisoryDocs, err := v2.NewIndex(fsys)
143+
advisoryDocs, err := v2.NewIndex(context.Background(), fsys)
144144
if err != nil {
145145
t.Fatalf("unable to create advisory docs index: %v", err)
146146
}

pkg/advisory/export_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"io"
56
"os"
67
"testing"
@@ -36,7 +37,7 @@ func Test_ExportFuncs(t *testing.T) {
3637
for _, tt := range cases {
3738
t.Run(tt.name, func(t *testing.T) {
3839
advisoryFsys := rwos.DirFS(testdataDir)
39-
advisoryDocs, err := v2.NewIndex(advisoryFsys)
40+
advisoryDocs, err := v2.NewIndex(context.Background(), advisoryFsys)
4041
require.NoError(t, err)
4142
indices := []*configs.Index[v2.Document]{advisoryDocs}
4243

pkg/advisory/secdb_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -65,7 +66,7 @@ func TestBuildSecurityDatabase(t *testing.T) {
6566
indices := make([]*configs.Index[v2.Document], 0, len(tt.advisoryDirs))
6667
for _, dir := range tt.advisoryDirs {
6768
advisoryFsys := rwos.DirFS(dir)
68-
advisoryCfgs, err := v2.NewIndex(advisoryFsys)
69+
advisoryCfgs, err := v2.NewIndex(context.Background(), advisoryFsys)
6970
require.NoError(t, err)
7071
indices = append(indices, advisoryCfgs)
7172
}

pkg/advisory/update.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"fmt"
56
"sort"
67

@@ -16,7 +17,7 @@ type UpdateOptions struct {
1617

1718
// Update adds a new event to an existing advisory (named by the vuln parameter)
1819
// in the document at the provided path.
19-
func Update(req Request, opts UpdateOptions) error {
20+
func Update(ctx context.Context, req Request, opts UpdateOptions) error {
2021
vulnID := req.VulnerabilityID
2122

2223
documents := opts.AdvisoryDocs.Select().WhereName(req.Package)
@@ -40,13 +41,13 @@ func Update(req Request, opts UpdateOptions) error {
4041

4142
return advisories, nil
4243
})
43-
err := documents.Update(u)
44+
err := documents.Update(ctx, u)
4445
if err != nil {
4546
return fmt.Errorf("unable to add entry for advisory %q in %q: %w", vulnID, req.Package, err)
4647
}
4748

4849
// Update the schema version to the latest version.
49-
err = documents.Update(v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
50+
err = documents.Update(ctx, v2.NewSchemaVersionSectionUpdater(v2.SchemaVersion))
5051
if err != nil {
5152
return fmt.Errorf("unable to update schema version for %q: %w", req.Package, err)
5253
}

pkg/advisory/update_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package advisory
22

33
import (
4+
"context"
45
"os"
56
"testing"
67
"time"
@@ -122,10 +123,10 @@ func TestUpdate(t *testing.T) {
122123
t.Run(tt.name, func(t *testing.T) {
123124
// We want a fresh memfs for each test case.
124125
memFS := memfs.New(dirFS)
125-
advisoryDocs, err := v2.NewIndex(memFS)
126+
advisoryDocs, err := v2.NewIndex(context.Background(), memFS)
126127
require.NoError(t, err)
127128

128-
err = Update(tt.req, UpdateOptions{
129+
err = Update(context.Background(), tt.req, UpdateOptions{
129130
AdvisoryDocs: advisoryDocs,
130131
})
131132

0 commit comments

Comments
 (0)