MCPcopy Create free account
hub / github.com/cloudbase/garm / DeleteGithubCredentials

Method DeleteGithubCredentials

database/sql/github.go:487–536  ·  view source on GitHub ↗
(ctx context.Context, id uint)

Source from the content-addressed store, hash-verified

485}
486
487func (s *sqlDatabase) DeleteGithubCredentials(ctx context.Context, id uint) (err error) {
488 var name string
489 defer func() {
490 if err == nil {
491 s.sendNotify(common.GithubCredentialsEntityType, common.DeleteOperation, params.ForgeCredentials{ID: id, Name: name, ForgeType: params.GithubEndpointType})
492 }
493 }()
494 err = s.conn.Transaction(func(tx *gorm.DB) error {
495 q := tx.Where("id = ?", id).
496 Preload("Repositories").
497 Preload("Organizations").
498 Preload("Enterprises")
499 if !auth.IsAdmin(ctx) {
500 userID, err := getUIDFromContext(ctx)
501 if err != nil {
502 return fmt.Errorf("error deleting github credentials: %w", err)
503 }
504 q = q.Where("user_id = ?", userID)
505 }
506
507 var creds GithubCredentials
508 err := q.First(&creds).Error
509 if err != nil {
510 if errors.Is(err, gorm.ErrRecordNotFound) {
511 return nil
512 }
513 return fmt.Errorf("error fetching github credentials: %w", err)
514 }
515 name = creds.Name
516
517 if len(creds.Repositories) > 0 {
518 return fmt.Errorf("cannot delete credentials with repositories: %w", runnerErrors.ErrBadRequest)
519 }
520 if len(creds.Organizations) > 0 {
521 return fmt.Errorf("cannot delete credentials with organizations: %w", runnerErrors.ErrBadRequest)
522 }
523 if len(creds.Enterprises) > 0 {
524 return fmt.Errorf("cannot delete credentials with enterprises: %w", runnerErrors.ErrBadRequest)
525 }
526
527 if err := tx.Unscoped().Delete(&creds).Error; err != nil {
528 return fmt.Errorf("error deleting github credentials: %w", err)
529 }
530 return nil
531 })
532 if err != nil {
533 return fmt.Errorf("error deleting github credentials: %w", err)
534 }
535 return nil
536}

Callers

nothing calls this directly

Calls 4

sendNotifyMethod · 0.95
IsAdminFunction · 0.92
getUIDFromContextFunction · 0.85
DeleteMethod · 0.65

Tested by

no test coverage detected