(ctx context.Context, id uint)
| 485 | } |
| 486 | |
| 487 | func (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 | } |
nothing calls this directly
no test coverage detected