-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhandlers_repositories.go
500 lines (417 loc) · 15.7 KB
/
handlers_repositories.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2023 Stacklok, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlplane
import (
"context"
"database/sql"
"errors"
"strings"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/providers"
github "github.com/stacklok/minder/internal/providers/github"
"github.com/stacklok/minder/internal/reconcilers"
"github.com/stacklok/minder/internal/util"
cursorutil "github.com/stacklok/minder/internal/util/cursor"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
// maxFetchLimit is the maximum number of repositories that can be fetched from the database in one call
const maxFetchLimit = 100
// RegisterRepository adds repositories to the database and registers a webhook
// Once a user had enrolled in a project (they have a valid token), they can register
// repositories to be monitored by the minder by provisioning a webhook on the
// repository(ies).
func (s *Server) RegisterRepository(ctx context.Context,
in *pb.RegisterRepositoryRequest) (*pb.RegisterRepositoryResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, projectID)
if err != nil {
return nil, providerError(err)
}
pbOpts := []providers.ProviderBuilderOption{
providers.WithProviderMetrics(s.provMt),
providers.WithRestClientCache(s.restClientCache),
}
p, err := providers.GetProviderBuilder(ctx, provider, projectID, s.store, s.cryptoEngine, pbOpts...)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot get provider builder: %v", err)
}
// Unmarshal the in.GetRepositories() into a struct Repository
if in.GetRepository() == nil || in.GetRepository().Name == "" {
return nil, util.UserVisibleError(codes.InvalidArgument, "no repository provided")
}
repo := in.GetRepository()
result, err := s.registerWebhookForRepository(ctx, p, projectID, repo)
if err != nil {
return nil, util.UserVisibleError(codes.Internal, "cannot register webhook: %v", err)
}
r := result.Repository
response := &pb.RegisterRepositoryResponse{
Result: result,
}
// Convert each result to a pb.Repository object
if result.Status.Error != nil {
return response, nil
}
// update the database
dbRepo, err := s.store.CreateRepository(ctx, db.CreateRepositoryParams{
Provider: provider.Name,
ProjectID: projectID,
RepoOwner: r.Owner,
RepoName: r.Name,
RepoID: r.RepoId,
IsPrivate: r.IsPrivate,
IsFork: r.IsFork,
WebhookID: sql.NullInt64{
Int64: r.HookId,
Valid: true,
},
CloneUrl: r.CloneUrl,
WebhookUrl: r.HookUrl,
DeployUrl: r.DeployUrl,
DefaultBranch: sql.NullString{
String: r.DefaultBranch,
Valid: true,
},
})
// even if we set the webhook, if we couldn't create it in the database, we'll return an error
if err != nil {
log.Printf("error creating repository '%s/%s' in database: %v", r.Owner, r.Name, err)
result.Status.Success = false
errorStr := "error creating repository in database"
result.Status.Error = &errorStr
return response, nil
}
repoDBID := dbRepo.ID.String()
r.Id = &repoDBID
// publish a reconciling event for the registered repositories
log.Printf("publishing register event for repository: %s/%s", r.Owner, r.Name)
msg, err := reconcilers.NewRepoReconcilerMessage(provider.Name, r.RepoId, projectID)
if err != nil {
log.Printf("error creating reconciler event: %v", err)
return response, nil
}
// This is a non-fatal error, so we'll just log it and continue with the next ones
if err := s.evt.Publish(reconcilers.InternalReconcilerEventTopic, msg); err != nil {
log.Printf("error publishing reconciler event: %v", err)
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = provider.Name
logger.BusinessRecord(ctx).Project = projectID
logger.BusinessRecord(ctx).Repository = dbRepo.ID
return response, nil
}
// ListRepositories returns a list of repositories for a given project
// This function will typically be called by the client to get a list of
// repositories that are registered present in the minder database
func (s *Server) ListRepositories(ctx context.Context,
in *pb.ListRepositoriesRequest) (*pb.ListRepositoriesResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, projectID)
if err != nil {
return nil, providerError(err)
}
reqRepoCursor, err := cursorutil.NewRepoCursor(in.GetCursor())
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, err.Error())
}
repoId := sql.NullInt64{}
if reqRepoCursor.ProjectId == projectID.String() && reqRepoCursor.Provider == provider.Name {
repoId = sql.NullInt64{Valid: true, Int64: reqRepoCursor.RepoId}
}
limit := sql.NullInt32{Valid: false, Int32: 0}
reqLimit := in.GetLimit()
if reqLimit > 0 {
if reqLimit > maxFetchLimit {
return nil, util.UserVisibleError(codes.InvalidArgument, "limit too high, max is %d", maxFetchLimit)
}
limit = sql.NullInt32{Valid: true, Int32: reqLimit + 1}
}
repos, err := s.store.ListRepositoriesByProjectID(ctx, db.ListRepositoriesByProjectIDParams{
Provider: provider.Name,
ProjectID: projectID,
RepoID: repoId,
Limit: limit,
})
if err != nil {
return nil, err
}
var resp pb.ListRepositoriesResponse
var results []*pb.Repository
for _, repo := range repos {
repo := repo
projID := repo.ProjectID.String()
r := util.PBRepositoryFromDB(repo)
r.Context = &pb.Context{
Project: &projID,
Provider: &repo.Provider,
}
results = append(results, r)
}
var respRepoCursor *cursorutil.RepoCursor
if limit.Valid && len(repos) == int(limit.Int32) {
lastRepo := repos[len(repos)-1]
respRepoCursor = &cursorutil.RepoCursor{
ProjectId: projectID.String(),
Provider: provider.Name,
RepoId: lastRepo.RepoID,
}
// remove the (limit + 1)th element from the results
results = results[:len(results)-1]
}
resp.Results = results
resp.Cursor = respRepoCursor.String()
// Telemetry logging
logger.BusinessRecord(ctx).Provider = provider.Name
logger.BusinessRecord(ctx).Project = projectID
return &resp, nil
}
// GetRepositoryById returns a repository for a given repository id
func (s *Server) GetRepositoryById(ctx context.Context,
in *pb.GetRepositoryByIdRequest) (*pb.GetRepositoryByIdResponse, error) {
parsedRepositoryID, err := uuid.Parse(in.RepositoryId)
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository ID")
}
// read the repository
repo, err := s.store.GetRepositoryByID(ctx, parsedRepositoryID)
if errors.Is(err, sql.ErrNoRows) {
return nil, status.Errorf(codes.NotFound, "repository not found")
} else if err != nil {
return nil, status.Errorf(codes.Internal, "cannot read repository: %v", err)
}
projID := repo.ProjectID.String()
r := util.PBRepositoryFromDB(repo)
r.Context = &pb.Context{
Project: &projID,
Provider: &repo.Provider,
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = repo.Provider
logger.BusinessRecord(ctx).Project = repo.ProjectID
logger.BusinessRecord(ctx).Repository = repo.ID
return &pb.GetRepositoryByIdResponse{Repository: r}, nil
}
// GetRepositoryByName returns information about a repository.
// This function will typically be called by the client to get a
// repository which is already registered and present in the minder database
// The API is called with a project id
func (s *Server) GetRepositoryByName(ctx context.Context,
in *pb.GetRepositoryByNameRequest) (*pb.GetRepositoryByNameResponse, error) {
// split repo name in owner and name
fragments := strings.Split(in.Name, "/")
if len(fragments) != 2 {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository name, needs to have the format: owner/name")
}
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, projectID)
if err != nil {
return nil, providerError(err)
}
repo, err := s.store.GetRepositoryByRepoName(ctx,
db.GetRepositoryByRepoNameParams{Provider: provider.Name, RepoOwner: fragments[0], RepoName: fragments[1]})
if errors.Is(err, sql.ErrNoRows) {
return nil, status.Errorf(codes.NotFound, "repository not found")
} else if err != nil {
return nil, err
}
projID := repo.ProjectID.String()
r := util.PBRepositoryFromDB(repo)
r.Context = &pb.Context{
Project: &projID,
Provider: &repo.Provider,
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = repo.Provider
logger.BusinessRecord(ctx).Project = repo.ProjectID
logger.BusinessRecord(ctx).Repository = repo.ID
return &pb.GetRepositoryByNameResponse{Repository: r}, nil
}
// DeleteRepositoryById deletes a repository by name
func (s *Server) DeleteRepositoryById(ctx context.Context,
in *pb.DeleteRepositoryByIdRequest) (*pb.DeleteRepositoryByIdResponse, error) {
parsedRepositoryID, err := uuid.Parse(in.RepositoryId)
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository ID")
}
// read the repository
repo, err := s.store.GetRepositoryByID(ctx, parsedRepositoryID)
if errors.Is(err, sql.ErrNoRows) {
return nil, status.Errorf(codes.NotFound, "repository not found")
} else if err != nil {
return nil, status.Errorf(codes.Internal, "cannot read repository: %v", err)
}
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, repo.ProjectID)
if err != nil {
return nil, providerError(err)
}
err = s.deleteRepositoryAndWebhook(ctx, repo, repo.ProjectID, provider)
if err != nil {
return nil, err
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = repo.Provider
logger.BusinessRecord(ctx).Project = repo.ProjectID
logger.BusinessRecord(ctx).Repository = repo.ID
// return the response with the id of the deleted repository
return &pb.DeleteRepositoryByIdResponse{
RepositoryId: in.RepositoryId,
}, nil
}
// DeleteRepositoryByName deletes a repository by name
func (s *Server) DeleteRepositoryByName(ctx context.Context,
in *pb.DeleteRepositoryByNameRequest) (*pb.DeleteRepositoryByNameResponse, error) {
// split repo name in owner and name
fragments := strings.Split(in.Name, "/")
if len(fragments) != 2 {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid repository name, needs to have the format: owner/name")
}
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, projectID)
if err != nil {
return nil, providerError(err)
}
repo, err := s.store.GetRepositoryByRepoName(ctx,
db.GetRepositoryByRepoNameParams{Provider: provider.Name, RepoOwner: fragments[0], RepoName: fragments[1]})
if errors.Is(err, sql.ErrNoRows) {
return nil, status.Errorf(codes.NotFound, "repository not found")
} else if err != nil {
return nil, err
}
err = s.deleteRepositoryAndWebhook(ctx, repo, projectID, provider)
if err != nil {
return nil, err
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = repo.Provider
logger.BusinessRecord(ctx).Project = repo.ProjectID
logger.BusinessRecord(ctx).Repository = repo.ID
// return the response with the name of the deleted repository
return &pb.DeleteRepositoryByNameResponse{
Name: in.Name,
}, nil
}
// ListRemoteRepositoriesFromProvider returns a list of repositories from a provider
func (s *Server) ListRemoteRepositoriesFromProvider(
ctx context.Context,
in *pb.ListRemoteRepositoriesFromProviderRequest,
) (*pb.ListRemoteRepositoriesFromProviderResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
provider, err := getProviderFromRequestOrDefault(ctx, s.store, in, projectID)
if err != nil {
return nil, providerError(err)
}
zerolog.Ctx(ctx).Debug().
Str("provider", provider.Name).
Str("projectID", projectID.String()).
Msg("listing repositories")
// FIXME: this is a hack to get the owner filter from the request
_, owner_filter, err := s.getProviderAccessToken(ctx, provider.Name, projectID)
if err != nil {
return nil, util.UserVisibleError(codes.PermissionDenied,
"cannot get access token for provider: did you run `minder provider enroll`?")
}
pbOpts := []providers.ProviderBuilderOption{
providers.WithProviderMetrics(s.provMt),
providers.WithRestClientCache(s.restClientCache),
}
p, err := providers.GetProviderBuilder(ctx, provider, projectID, s.store, s.cryptoEngine, pbOpts...)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot get provider builder: %v", err)
}
if !p.Implements(db.ProviderTypeRepoLister) {
return nil, util.UserVisibleError(codes.Unimplemented, "provider does not implement repository listing")
}
client, err := p.GetRepoLister()
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot create github client: %v", err)
}
tmoutCtx, cancel := context.WithTimeout(ctx, github.ExpensiveRestCallTimeout)
defer cancel()
var remoteRepos []*pb.Repository
isOrg := (owner_filter != "")
if isOrg {
zerolog.Ctx(ctx).Debug().Msgf("listing repositories for organization")
remoteRepos, err = client.ListOrganizationRepsitories(tmoutCtx, owner_filter)
if err != nil {
return nil, util.UserVisibleError(codes.Internal, "cannot list repositories: %v", err)
}
} else {
zerolog.Ctx(ctx).Debug().Msgf("listing repositories for the user")
remoteRepos, err = client.ListUserRepositories(tmoutCtx, owner_filter)
if err != nil {
return nil, util.UserVisibleError(codes.Internal, "cannot list repositories: %v", err)
}
}
out := &pb.ListRemoteRepositoriesFromProviderResponse{
Results: make([]*pb.UpstreamRepositoryRef, 0, len(remoteRepos)),
}
allowsPrivateRepos := projectAllowsPrivateRepos(ctx, s.store, projectID)
if !allowsPrivateRepos {
zerolog.Ctx(ctx).Info().Msg("filtering out private repositories")
} else {
zerolog.Ctx(ctx).Info().Msg("including private repositories")
}
for idx, rem := range remoteRepos {
// Skip private repositories
if rem.IsPrivate && !allowsPrivateRepos {
continue
}
remoteRepo := remoteRepos[idx]
repo := &pb.UpstreamRepositoryRef{
Owner: remoteRepo.Owner,
Name: remoteRepo.Name,
RepoId: remoteRepo.RepoId,
}
out.Results = append(out.Results, repo)
}
// Telemetry logging
logger.BusinessRecord(ctx).Provider = provider.Name
logger.BusinessRecord(ctx).Project = projectID
return out, nil
}
func (s *Server) deleteRepositoryAndWebhook(
ctx context.Context,
repo db.Repository,
projectID uuid.UUID,
provider db.Provider,
) error {
tx, err := s.store.BeginTransaction()
if err != nil {
return status.Errorf(codes.Internal, "error deleting repository")
}
defer s.store.Rollback(tx)
qtx := s.store.GetQuerierWithTransaction(tx)
if err := qtx.DeleteRepository(ctx, repo.ID); err != nil {
return status.Errorf(codes.Internal, "error deleting repository: %v", err)
}
if err := s.deleteWebhookFromRepository(ctx, provider, projectID, repo); err != nil {
return err
}
if err := s.store.Commit(tx); err != nil {
return status.Errorf(codes.Internal, "error deleting repository")
}
return nil
}