forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeowners.go
More file actions
303 lines (259 loc) · 7.61 KB
/
codeowners.go
File metadata and controls
303 lines (259 loc) · 7.61 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package database
import (
"context"
"fmt"
"github.com/jackc/pgconn"
"github.com/keegancsmith/sqlf"
"github.com/lib/pq"
"google.golang.org/protobuf/proto"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
codeownerspb "github.com/sourcegraph/sourcegraph/internal/own/codeowners/v1"
"github.com/sourcegraph/sourcegraph/internal/own/types"
"github.com/sourcegraph/sourcegraph/internal/timeutil"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type CodeownersStore interface {
basestore.ShareableStore
Done(error) error
// CreateCodeownersFile creates a given Codeowners file in the database.
CreateCodeownersFile(ctx context.Context, codeowners *types.CodeownersFile) error
// UpdateCodeownersFile updates a manually ingested Codeowners file in the database, matched by repo.
UpdateCodeownersFile(ctx context.Context, codeowners *types.CodeownersFile) error
// GetCodeownersForRepo gets a manually ingested Codeowners file for the given repo if it exists.
GetCodeownersForRepo(ctx context.Context, id api.RepoID) (*types.CodeownersFile, error)
// DeleteCodeownersForRepos deletes manually ingested Codeowners files for the given repos if it exists.
DeleteCodeownersForRepos(ctx context.Context, ids ...api.RepoID) error
// ListCodeowners lists manually ingested Codeowners files given the options.
ListCodeowners(ctx context.Context, opts ListCodeownersOpts) ([]*types.CodeownersFile, int32, error)
// CountCodeownersFiles counts the number of manually ingested Codeowners files.
CountCodeownersFiles(context.Context) (int32, error)
}
type codeownersStore struct {
*basestore.Store
}
type CodeownersFileNotFoundError struct {
args any
}
func (e CodeownersFileNotFoundError) Error() string {
return fmt.Sprintf("codeowners file not found: %v", e.args)
}
func (CodeownersFileNotFoundError) NotFound() bool {
return true
}
var ErrCodeownersFileAlreadyExists = errors.New("codeowners file has already been ingested for this repository")
func (s *codeownersStore) CreateCodeownersFile(ctx context.Context, file *types.CodeownersFile) error {
return s.WithTransact(ctx, func(tx CodeownersStore) error {
if file.CreatedAt.IsZero() {
file.CreatedAt = timeutil.Now()
}
if file.UpdatedAt.IsZero() {
file.UpdatedAt = file.CreatedAt
}
protoBytes, err := proto.Marshal(file.Proto)
if err != nil {
return err
}
q := sqlf.Sprintf(
createCodeownersQueryFmtStr,
sqlf.Join(codeownersColumns, ","),
file.Contents,
protoBytes,
file.RepoID,
file.CreatedAt,
file.UpdatedAt,
)
if _, err := tx.Handle().ExecContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...); err != nil {
var e *pgconn.PgError
if errors.As(err, &e) {
switch e.ConstraintName {
case "codeowners_repo_id_key":
return ErrCodeownersFileAlreadyExists
}
}
return err
}
return nil
})
}
var codeownersColumns = []*sqlf.Query{
sqlf.Sprintf("contents"),
sqlf.Sprintf("contents_proto"),
sqlf.Sprintf("repo_id"),
sqlf.Sprintf("created_at"),
sqlf.Sprintf("updated_at"),
}
const createCodeownersQueryFmtStr = `
INSERT INTO codeowners
(%s)
VALUES (%s, %s, %s, %s, %s)
`
func (s *codeownersStore) UpdateCodeownersFile(ctx context.Context, file *types.CodeownersFile) error {
return s.WithTransact(ctx, func(tx CodeownersStore) error {
if file.UpdatedAt.IsZero() {
file.UpdatedAt = timeutil.Now()
}
conds := []*sqlf.Query{
sqlf.Sprintf("repo_id = %s", file.RepoID),
}
protoBytes, err := proto.Marshal(file.Proto)
if err != nil {
return err
}
q := sqlf.Sprintf(
updateCodeownersQueryFmtStr,
file.Contents,
protoBytes,
file.UpdatedAt,
sqlf.Join(conds, "AND"),
)
res, err := tx.Handle().ExecContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
if err != nil {
return err
}
rows, err := res.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return CodeownersFileNotFoundError{args: file.RepoID}
}
return nil
})
}
const updateCodeownersQueryFmtStr = `
UPDATE codeowners
SET
contents = %s,
contents_proto = %s,
updated_at = %s
WHERE
%s
`
func (s *codeownersStore) GetCodeownersForRepo(ctx context.Context, id api.RepoID) (*types.CodeownersFile, error) {
q := sqlf.Sprintf(
getCodeownersFileQueryFmtStr,
sqlf.Join(codeownersColumns, ", "),
sqlf.Sprintf("repo_id = %s", id),
)
codeownersFiles, err := scanCodeowners(s.Query(ctx, q))
if err != nil {
return nil, err
}
if len(codeownersFiles) != 1 {
return nil, CodeownersFileNotFoundError{args: id}
}
return codeownersFiles[0], nil
}
const getCodeownersFileQueryFmtStr = `
SELECT %s
FROM codeowners
WHERE %s
LIMIT 1
`
func (s *codeownersStore) DeleteCodeownersForRepos(ctx context.Context, ids ...api.RepoID) error {
return s.WithTransact(ctx, func(tx CodeownersStore) error {
conds := []*sqlf.Query{
sqlf.Sprintf("repo_id = ANY (%s)", pq.Array(ids)),
}
q := sqlf.Sprintf(deleteCodeownersFileQueryFmtStr, sqlf.Join(conds, "AND"))
res, err := tx.Handle().ExecContext(ctx, q.Query(sqlf.PostgresBindVar), q.Args()...)
if err != nil {
return err
}
rows, err := res.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return CodeownersFileNotFoundError{args: conds}
}
return nil
})
}
const deleteCodeownersFileQueryFmtStr = `
DELETE FROM codeowners
WHERE %s
`
type ListCodeownersOpts struct {
*LimitOffset
// Only return codeowners past this cursor (repoID).
Cursor int32
}
func (s *codeownersStore) ListCodeowners(ctx context.Context, opts ListCodeownersOpts) (_ []*types.CodeownersFile, next int32, err error) {
if opts.LimitOffset != nil && opts.Limit > 0 {
opts.Limit++
}
where := []*sqlf.Query{
sqlf.Sprintf("repo_id >= %s", opts.Cursor),
}
q := sqlf.Sprintf(
listCodeownersFilesQueryFmtStr,
sqlf.Join(codeownersColumns, ","),
sqlf.Join(where, "AND"),
opts.LimitOffset.SQL(),
)
codeownersFiles, err := scanCodeowners(s.Query(ctx, q))
if err != nil {
return nil, 0, err
}
if opts.LimitOffset != nil && opts.Limit > 0 && len(codeownersFiles) == opts.Limit {
next = int32(codeownersFiles[len(codeownersFiles)-1].RepoID)
codeownersFiles = codeownersFiles[:len(codeownersFiles)-1]
}
return codeownersFiles, next, nil
}
const listCodeownersFilesQueryFmtStr = `
SELECT %s
FROM codeowners
WHERE %s
ORDER BY
repo_id ASC
%s
`
func (s *codeownersStore) CountCodeownersFiles(ctx context.Context) (int32, error) {
q := sqlf.Sprintf(countCodeownersFilesQueryFmtStr)
count, _, err := basestore.ScanFirstInt(s.Query(ctx, q))
return int32(count), err
}
const countCodeownersFilesQueryFmtStr = `
SELECT COUNT(*)
FROM codeowners
`
func CodeownersWith(other basestore.ShareableStore) CodeownersStore {
return &codeownersStore{
Store: basestore.NewWithHandle(other.Handle()),
}
}
func (s *codeownersStore) With(other basestore.ShareableStore) CodeownersStore {
return &codeownersStore{
Store: s.Store.With(other),
}
}
func (s *codeownersStore) WithTransact(ctx context.Context, f func(store CodeownersStore) error) error {
return s.Store.WithTransact(ctx, func(tx *basestore.Store) error {
return f(&codeownersStore{
Store: tx,
})
})
}
var scanCodeowners = basestore.NewSliceScanner(func(s dbutil.Scanner) (*types.CodeownersFile, error) {
var c types.CodeownersFile
c.Proto = new(codeownerspb.File)
err := scanCodeownersRow(s, &c)
return &c, err
})
func scanCodeownersRow(sc dbutil.Scanner, c *types.CodeownersFile) error {
var protoBytes []byte
if err := sc.Scan(
&c.Contents,
&protoBytes,
&c.RepoID,
&c.CreatedAt,
&c.UpdatedAt,
); err != nil {
return err
}
return proto.Unmarshal(protoBytes, c.Proto)
}