-
Notifications
You must be signed in to change notification settings - Fork 1
/
false_positives.go
66 lines (61 loc) · 1.72 KB
/
false_positives.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package worker
import (
"context"
"github.com/khulnasoft-lab/go-vulndb/internal/derrors"
"github.com/khulnasoft-lab/go-vulndb/internal/worker/store"
"golang.org/x/exp/event"
)
// updateFalsePositives makes sure the store reflects the list of false positives.
func updateFalsePositives(ctx context.Context, st store.Store) (err error) {
defer derrors.Wrap(&err, "updateFalsePositives")
ctx = event.Start(ctx, "updateFalsePositives")
defer event.End(ctx)
for i := 0; i < len(falsePositives); i += maxTransactionWrites {
j := i + maxTransactionWrites
if j >= len(falsePositives) {
j = len(falsePositives)
}
batch := falsePositives[i:j]
err := st.RunTransaction(ctx, func(ctx context.Context, tx store.Transaction) error {
oldRecords, err := readCVERecords(tx, batch)
if err != nil {
return err
}
for i, cr := range batch {
old := oldRecords[i]
var err error
if old == nil {
err = tx.CreateCVERecord(cr)
} else if old.CommitHash != cr.CommitHash && !old.CommitTime.IsZero() && old.CommitTime.Before(cr.CommitTime) {
// If the false positive data is more recent than what is in
// the store, then update the DB. But ignore records whose
// commit time hasn't been populated.
err = tx.SetCVERecord(cr)
}
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func readCVERecords(tx store.Transaction, crs []*store.CVERecord) ([]*store.CVERecord, error) {
var olds []*store.CVERecord
for _, cr := range crs {
dbcrs, err := tx.GetCVERecords(cr.ID, cr.ID)
if err != nil {
return nil, err
}
var old *store.CVERecord
if len(dbcrs) > 0 {
old = dbcrs[0]
}
olds = append(olds, old)
}
return olds, nil
}