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.

repository.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "fmt"
  7. "code.gitea.io/gitea/models"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/notification"
  12. repo_module "code.gitea.io/gitea/modules/repository"
  13. cfg "code.gitea.io/gitea/modules/setting"
  14. pull_service "code.gitea.io/gitea/services/pull"
  15. )
  16. // CreateRepository creates a repository for the user/organization.
  17. func CreateRepository(doer, owner *user_model.User, opts models.CreateRepoOptions) (*repo_model.Repository, error) {
  18. repo, err := repo_module.CreateRepository(doer, owner, opts)
  19. if err != nil {
  20. // No need to rollback here we should do this in CreateRepository...
  21. return nil, err
  22. }
  23. notification.NotifyCreateRepository(doer, owner, repo)
  24. return repo, nil
  25. }
  26. // DeleteRepository deletes a repository for a user or organization.
  27. func DeleteRepository(doer *user_model.User, repo *repo_model.Repository, notify bool) error {
  28. if err := pull_service.CloseRepoBranchesPulls(doer, repo); err != nil {
  29. log.Error("CloseRepoBranchesPulls failed: %v", err)
  30. }
  31. if notify {
  32. // If the repo itself has webhooks, we need to trigger them before deleting it...
  33. notification.NotifyDeleteRepository(doer, repo)
  34. }
  35. err := models.DeleteRepository(doer, repo.OwnerID, repo.ID)
  36. return err
  37. }
  38. // PushCreateRepo creates a repository when a new repository is pushed to an appropriate namespace
  39. func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_model.Repository, error) {
  40. if !authUser.IsAdmin {
  41. if owner.IsOrganization() {
  42. if ok, err := models.CanCreateOrgRepo(owner.ID, authUser.ID); err != nil {
  43. return nil, err
  44. } else if !ok {
  45. return nil, fmt.Errorf("cannot push-create repository for org")
  46. }
  47. } else if authUser.ID != owner.ID {
  48. return nil, fmt.Errorf("cannot push-create repository for another user")
  49. }
  50. }
  51. repo, err := CreateRepository(authUser, owner, models.CreateRepoOptions{
  52. Name: repoName,
  53. IsPrivate: cfg.Repository.DefaultPushCreatePrivate,
  54. })
  55. if err != nil {
  56. return nil, err
  57. }
  58. return repo, nil
  59. }
  60. // NewContext start repository service
  61. func NewContext() error {
  62. return initPushQueue()
  63. }