Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

repository.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/git"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. "code.gitea.io/gitea/models/organization"
  11. packages_model "code.gitea.io/gitea/models/packages"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. system_model "code.gitea.io/gitea/models/system"
  14. "code.gitea.io/gitea/models/unit"
  15. user_model "code.gitea.io/gitea/models/user"
  16. "code.gitea.io/gitea/modules/graceful"
  17. "code.gitea.io/gitea/modules/log"
  18. repo_module "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/structs"
  21. notify_service "code.gitea.io/gitea/services/notify"
  22. pull_service "code.gitea.io/gitea/services/pull"
  23. )
  24. // WebSearchRepository represents a repository returned by web search
  25. type WebSearchRepository struct {
  26. Repository *structs.Repository `json:"repository"`
  27. LatestCommitStatus *git.CommitStatus `json:"latest_commit_status"`
  28. LocaleLatestCommitStatus string `json:"locale_latest_commit_status"`
  29. }
  30. // WebSearchResults results of a successful web search
  31. type WebSearchResults struct {
  32. OK bool `json:"ok"`
  33. Data []*WebSearchRepository `json:"data"`
  34. }
  35. // CreateRepository creates a repository for the user/organization.
  36. func CreateRepository(ctx context.Context, doer, owner *user_model.User, opts CreateRepoOptions) (*repo_model.Repository, error) {
  37. repo, err := CreateRepositoryDirectly(ctx, doer, owner, opts)
  38. if err != nil {
  39. // No need to rollback here we should do this in CreateRepository...
  40. return nil, err
  41. }
  42. notify_service.CreateRepository(ctx, doer, owner, repo)
  43. return repo, nil
  44. }
  45. // DeleteRepository deletes a repository for a user or organization.
  46. func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, notify bool) error {
  47. if err := pull_service.CloseRepoBranchesPulls(ctx, doer, repo); err != nil {
  48. log.Error("CloseRepoBranchesPulls failed: %v", err)
  49. }
  50. if notify {
  51. // If the repo itself has webhooks, we need to trigger them before deleting it...
  52. notify_service.DeleteRepository(ctx, doer, repo)
  53. }
  54. if err := DeleteRepositoryDirectly(ctx, doer, repo.ID); err != nil {
  55. return err
  56. }
  57. return packages_model.UnlinkRepositoryFromAllPackages(ctx, repo.ID)
  58. }
  59. // PushCreateRepo creates a repository when a new repository is pushed to an appropriate namespace
  60. func PushCreateRepo(ctx context.Context, authUser, owner *user_model.User, repoName string) (*repo_model.Repository, error) {
  61. if !authUser.IsAdmin {
  62. if owner.IsOrganization() {
  63. if ok, err := organization.CanCreateOrgRepo(ctx, owner.ID, authUser.ID); err != nil {
  64. return nil, err
  65. } else if !ok {
  66. return nil, fmt.Errorf("cannot push-create repository for org")
  67. }
  68. } else if authUser.ID != owner.ID {
  69. return nil, fmt.Errorf("cannot push-create repository for another user")
  70. }
  71. }
  72. repo, err := CreateRepository(ctx, authUser, owner, CreateRepoOptions{
  73. Name: repoName,
  74. IsPrivate: setting.Repository.DefaultPushCreatePrivate,
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. return repo, nil
  80. }
  81. // Init start repository service
  82. func Init(ctx context.Context) error {
  83. if err := repo_module.LoadRepoConfig(); err != nil {
  84. return err
  85. }
  86. system_model.RemoveAllWithNotice(ctx, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
  87. system_model.RemoveAllWithNotice(ctx, "Clean up temporary repositories", repo_module.LocalCopyPath())
  88. if err := initPushQueue(); err != nil {
  89. return err
  90. }
  91. return initBranchSyncQueue(graceful.GetManager().ShutdownContext())
  92. }
  93. // UpdateRepository updates a repository
  94. func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) {
  95. ctx, committer, err := db.TxContext(ctx)
  96. if err != nil {
  97. return err
  98. }
  99. defer committer.Close()
  100. if err = repo_module.UpdateRepository(ctx, repo, visibilityChanged); err != nil {
  101. return fmt.Errorf("updateRepository: %w", err)
  102. }
  103. return committer.Commit()
  104. }
  105. // LinkedRepository returns the linked repo if any
  106. func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) {
  107. if a.IssueID != 0 {
  108. iss, err := issues_model.GetIssueByID(ctx, a.IssueID)
  109. if err != nil {
  110. return nil, unit.TypeIssues, err
  111. }
  112. repo, err := repo_model.GetRepositoryByID(ctx, iss.RepoID)
  113. unitType := unit.TypeIssues
  114. if iss.IsPull {
  115. unitType = unit.TypePullRequests
  116. }
  117. return repo, unitType, err
  118. } else if a.ReleaseID != 0 {
  119. rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
  120. if err != nil {
  121. return nil, unit.TypeReleases, err
  122. }
  123. repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID)
  124. return repo, unit.TypeReleases, err
  125. }
  126. return nil, -1, nil
  127. }