You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repository
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/notification"
  9. )
  10. // CreateRepository creates a repository for the user/organization.
  11. func CreateRepository(doer, owner *models.User, opts models.CreateRepoOptions) (*models.Repository, error) {
  12. repo, err := models.CreateRepository(doer, owner, opts)
  13. if err != nil {
  14. if repo != nil {
  15. if errDelete := models.DeleteRepository(doer, owner.ID, repo.ID); errDelete != nil {
  16. log.Error("Rollback deleteRepository: %v", errDelete)
  17. }
  18. }
  19. return nil, err
  20. }
  21. notification.NotifyCreateRepository(doer, owner, repo)
  22. return repo, nil
  23. }
  24. // ForkRepository forks a repository
  25. func ForkRepository(doer, u *models.User, oldRepo *models.Repository, name, desc string) (*models.Repository, error) {
  26. repo, err := models.ForkRepository(doer, u, oldRepo, name, desc)
  27. if err != nil {
  28. if repo != nil {
  29. if errDelete := models.DeleteRepository(doer, u.ID, repo.ID); errDelete != nil {
  30. log.Error("Rollback deleteRepository: %v", errDelete)
  31. }
  32. }
  33. return nil, err
  34. }
  35. notification.NotifyForkRepository(doer, oldRepo, repo)
  36. return repo, nil
  37. }
  38. // DeleteRepository deletes a repository for a user or organization.
  39. func DeleteRepository(doer *models.User, repo *models.Repository) error {
  40. if err := models.DeleteRepository(doer, repo.OwnerID, repo.ID); err != nil {
  41. return err
  42. }
  43. notification.NotifyDeleteRepository(doer, repo)
  44. return nil
  45. }