Repository navigation
Expand file tree
/
Copy pathimport_finding.go
More file actions
212 lines (180 loc) · 5.97 KB
/
Copy pathimport_finding.go
File metadata and controls
212 lines (180 loc) · 5.97 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package datastore
import (
"context"
"errors"
"fmt"
"path/filepath"
"time"
"cloud.google.com/go/datastore"
"cloud.google.com/go/storage"
"github.com/google/osv.dev/go/internal/models"
"google.golang.org/api/iterator"
)
type ImportFinding struct {
Key *datastore.Key `datastore:"__key__"`
BugID string `datastore:"bug_id"`
Source string `datastore:"source"`
Findings []models.ImportFindings `datastore:"findings"`
FirstSeen time.Time `datastore:"first_seen"`
LastAttempt time.Time `datastore:"last_attempt"`
}
type ImportFindingsStore struct {
dsClient *datastore.Client
storageClient *storage.Client
bucketName string
prefix string
}
var _ models.ImportFindingsStore = (*ImportFindingsStore)(nil)
func NewImportFindingsStore(dsClient *datastore.Client, storageClient *storage.Client, bucketName, prefix string) *ImportFindingsStore {
return &ImportFindingsStore{
dsClient: dsClient,
storageClient: storageClient,
bucketName: bucketName,
prefix: prefix,
}
}
func (s *ImportFindingsStore) Clear(ctx context.Context, id string) error {
key := datastore.NameKey("ImportFinding", id, nil)
return s.dsClient.Delete(ctx, key)
}
func (s *ImportFindingsStore) ListIDs(ctx context.Context) ([]string, error) {
query := datastore.NewQuery("ImportFinding").KeysOnly()
keys, err := s.dsClient.GetAll(ctx, query, nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch existing findings: %w", err)
}
ids := make([]string, len(keys))
for i, key := range keys {
ids[i] = key.Name
}
return ids, nil
}
func (s *ImportFindingsStore) GetMulti(ctx context.Context, bugIDs []string) ([]*models.ImportFinding, error) {
keys := make([]*datastore.Key, len(bugIDs))
for i, id := range bugIDs {
keys[i] = datastore.NameKey("ImportFinding", id, nil)
}
entities := make([]*ImportFinding, len(bugIDs))
err := s.dsClient.GetMulti(ctx, keys, entities)
if err != nil {
if multiErr, ok := errors.AsType[datastore.MultiError](err); ok {
for i, e := range multiErr {
if errors.Is(e, datastore.ErrNoSuchEntity) {
entities[i] = nil
} else if e != nil {
return nil, fmt.Errorf("failed to get multi: %w", err)
}
}
} else {
return nil, fmt.Errorf("failed to get multi: %w", err)
}
}
findings := make([]*models.ImportFinding, len(bugIDs))
for i, entity := range entities {
if entity != nil {
findings[i] = entity.toModel()
}
}
return findings, nil
}
func (s *ImportFindingsStore) PutMulti(ctx context.Context, findings []*models.ImportFinding) error {
keys := make([]*datastore.Key, len(findings))
entities := make([]*ImportFinding, len(findings))
for i, f := range findings {
keys[i] = datastore.NameKey("ImportFinding", f.BugID, nil)
entities[i] = newImportFindingFromModel(f)
}
if _, err := s.dsClient.PutMulti(ctx, keys, entities); err != nil {
return fmt.Errorf("failed to put multi: %w", err)
}
return nil
}
func (s *ImportFindingsStore) DeleteMulti(ctx context.Context, bugIDs []string) error {
keys := make([]*datastore.Key, len(bugIDs))
for i, id := range bugIDs {
keys[i] = datastore.NameKey("ImportFinding", id, nil)
}
if err := s.dsClient.DeleteMulti(ctx, keys); err != nil {
return fmt.Errorf("failed to delete multi: %w", err)
}
return nil
}
func (s *ImportFindingsStore) UploadResult(ctx context.Context, source string, data []byte) error {
bucket := s.storageClient.Bucket(s.bucketName)
targetPath := filepath.Join(s.prefix, source, "result.json")
w := bucket.Object(targetPath).NewWriter(ctx)
w.ContentType = "application/json"
if _, err := w.Write(data); err != nil {
w.Close()
return fmt.Errorf("failed to write object to GCS: %w", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("failed to close GCS writer: %w", err)
}
return nil
}
func (s *ImportFindingsStore) ListResultSources(ctx context.Context) ([]string, error) {
bucket := s.storageClient.Bucket(s.bucketName)
var objects []string
it := bucket.Objects(ctx, &storage.Query{Prefix: s.prefix})
for {
attrs, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return nil, fmt.Errorf("failed to list objects in GCS: %w", err)
}
objects = append(objects, attrs.Name)
}
return objects, nil
}
func (s *ImportFindingsStore) DeleteResult(ctx context.Context, path string) error {
bucket := s.storageClient.Bucket(s.bucketName)
if err := bucket.Object(path).Delete(ctx); err != nil {
return fmt.Errorf("failed to delete object in GCS: %w", err)
}
return nil
}
func (s *ImportFindingsStore) ListAllFromSource(ctx context.Context, source string) ([]*models.ImportFinding, error) {
query := datastore.NewQuery("ImportFinding").FilterField("source", "=", source)
var entities []*ImportFinding
if _, err := s.dsClient.GetAll(ctx, query, &entities); err != nil {
return nil, fmt.Errorf("failed to fetch import findings: %w", err)
}
findings := make([]*models.ImportFinding, len(entities))
for i, entity := range entities {
findings[i] = entity.toModel()
}
return findings, nil
}
func (s *ImportFindingsStore) ListAll(ctx context.Context) ([]*models.ImportFinding, error) {
query := datastore.NewQuery("ImportFinding")
var entities []*ImportFinding
if _, err := s.dsClient.GetAll(ctx, query, &entities); err != nil {
return nil, fmt.Errorf("failed to fetch all import findings: %w", err)
}
findings := make([]*models.ImportFinding, len(entities))
for i, entity := range entities {
findings[i] = entity.toModel()
}
return findings, nil
}
func (f *ImportFinding) toModel() *models.ImportFinding {
return &models.ImportFinding{
BugID: f.BugID,
Source: f.Source,
Findings: f.Findings,
FirstSeen: f.FirstSeen,
LastAttempt: f.LastAttempt,
}
}
func newImportFindingFromModel(f *models.ImportFinding) *ImportFinding {
return &ImportFinding{
BugID: f.BugID,
Source: f.Source,
Findings: f.Findings,
FirstSeen: f.FirstSeen,
LastAttempt: f.LastAttempt,
}
}