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.

218 lines
7.1 KiB

  1. // Copyright 2020 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 repo
  5. import (
  6. "bytes"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/graceful"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/migrations"
  18. "code.gitea.io/gitea/modules/notification"
  19. repo_module "code.gitea.io/gitea/modules/repository"
  20. "code.gitea.io/gitea/modules/setting"
  21. api "code.gitea.io/gitea/modules/structs"
  22. "code.gitea.io/gitea/modules/util"
  23. )
  24. // Migrate migrate remote git repository to gitea
  25. func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
  26. // swagger:operation POST /repos/migrate repository repoMigrate
  27. // ---
  28. // summary: Migrate a remote git repository
  29. // consumes:
  30. // - application/json
  31. // produces:
  32. // - application/json
  33. // parameters:
  34. // - name: body
  35. // in: body
  36. // schema:
  37. // "$ref": "#/definitions/MigrateRepoForm"
  38. // responses:
  39. // "201":
  40. // "$ref": "#/responses/Repository"
  41. // "403":
  42. // "$ref": "#/responses/forbidden"
  43. // "422":
  44. // "$ref": "#/responses/validationError"
  45. ctxUser := ctx.User
  46. // Not equal means context user is an organization,
  47. // or is another user/organization if current user is admin.
  48. if form.UID != ctxUser.ID {
  49. org, err := models.GetUserByID(form.UID)
  50. if err != nil {
  51. if models.IsErrUserNotExist(err) {
  52. ctx.Error(http.StatusUnprocessableEntity, "", err)
  53. } else {
  54. ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
  55. }
  56. return
  57. }
  58. ctxUser = org
  59. }
  60. if ctx.HasError() {
  61. ctx.Error(http.StatusUnprocessableEntity, "", ctx.GetErrMsg())
  62. return
  63. }
  64. if !ctx.User.IsAdmin {
  65. if !ctxUser.IsOrganization() && ctx.User.ID != ctxUser.ID {
  66. ctx.Error(http.StatusForbidden, "", "Given user is not an organization.")
  67. return
  68. }
  69. if ctxUser.IsOrganization() {
  70. // Check ownership of organization.
  71. isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID)
  72. if err != nil {
  73. ctx.Error(http.StatusInternalServerError, "IsOwnedBy", err)
  74. return
  75. } else if !isOwner {
  76. ctx.Error(http.StatusForbidden, "", "Given user is not owner of organization.")
  77. return
  78. }
  79. }
  80. }
  81. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  82. if err != nil {
  83. if models.IsErrInvalidCloneAddr(err) {
  84. addrErr := err.(models.ErrInvalidCloneAddr)
  85. switch {
  86. case addrErr.IsURLError:
  87. ctx.Error(http.StatusUnprocessableEntity, "", err)
  88. case addrErr.IsPermissionDenied:
  89. ctx.Error(http.StatusUnprocessableEntity, "", "You are not allowed to import local repositories.")
  90. case addrErr.IsInvalidPath:
  91. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid local path, it does not exist or not a directory.")
  92. default:
  93. ctx.Error(http.StatusInternalServerError, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
  94. }
  95. } else {
  96. ctx.Error(http.StatusInternalServerError, "ParseRemoteAddr", err)
  97. }
  98. return
  99. }
  100. var gitServiceType = api.PlainGitService
  101. u, err := url.Parse(remoteAddr)
  102. if err == nil && strings.EqualFold(u.Host, "github.com") {
  103. gitServiceType = api.GithubService
  104. }
  105. var opts = migrations.MigrateOptions{
  106. CloneAddr: remoteAddr,
  107. RepoName: form.RepoName,
  108. Description: form.Description,
  109. Private: form.Private || setting.Repository.ForcePrivate,
  110. Mirror: form.Mirror,
  111. AuthUsername: form.AuthUsername,
  112. AuthPassword: form.AuthPassword,
  113. Wiki: form.Wiki,
  114. Issues: form.Issues,
  115. Milestones: form.Milestones,
  116. Labels: form.Labels,
  117. Comments: true,
  118. PullRequests: form.PullRequests,
  119. Releases: form.Releases,
  120. GitServiceType: gitServiceType,
  121. }
  122. if opts.Mirror {
  123. opts.Issues = false
  124. opts.Milestones = false
  125. opts.Labels = false
  126. opts.Comments = false
  127. opts.PullRequests = false
  128. opts.Releases = false
  129. }
  130. repo, err := repo_module.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
  131. Name: opts.RepoName,
  132. Description: opts.Description,
  133. OriginalURL: form.CloneAddr,
  134. GitServiceType: gitServiceType,
  135. IsPrivate: opts.Private,
  136. IsMirror: opts.Mirror,
  137. Status: models.RepositoryBeingMigrated,
  138. })
  139. if err != nil {
  140. handleMigrateError(ctx, ctxUser, remoteAddr, err)
  141. return
  142. }
  143. opts.MigrateToRepoID = repo.ID
  144. defer func() {
  145. if e := recover(); e != nil {
  146. var buf bytes.Buffer
  147. fmt.Fprintf(&buf, "Handler crashed with error: %v", log.Stack(2))
  148. err = errors.New(buf.String())
  149. }
  150. if err == nil {
  151. repo.Status = models.RepositoryReady
  152. if err := models.UpdateRepositoryCols(repo, "status"); err == nil {
  153. notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
  154. return
  155. }
  156. }
  157. if repo != nil {
  158. if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
  159. log.Error("DeleteRepository: %v", errDelete)
  160. }
  161. }
  162. }()
  163. if _, err = migrations.MigrateRepository(graceful.GetManager().HammerContext(), ctx.User, ctxUser.Name, opts); err != nil {
  164. handleMigrateError(ctx, ctxUser, remoteAddr, err)
  165. return
  166. }
  167. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  168. ctx.JSON(http.StatusCreated, repo.APIFormat(models.AccessModeAdmin))
  169. }
  170. func handleMigrateError(ctx *context.APIContext, repoOwner *models.User, remoteAddr string, err error) {
  171. switch {
  172. case models.IsErrRepoAlreadyExist(err):
  173. ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
  174. case migrations.IsRateLimitError(err):
  175. ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit addressed rate limitation.")
  176. case migrations.IsTwoFactorAuthError(err):
  177. ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit required two factors authentication.")
  178. case models.IsErrReachLimitOfRepo(err):
  179. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
  180. case models.IsErrNameReserved(err):
  181. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name))
  182. case models.IsErrNameCharsNotAllowed(err):
  183. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' contains invalid characters.", err.(models.ErrNameCharsNotAllowed).Name))
  184. case models.IsErrNamePatternNotAllowed(err):
  185. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(models.ErrNamePatternNotAllowed).Pattern))
  186. default:
  187. err = util.URLSanitizedError(err, remoteAddr)
  188. if strings.Contains(err.Error(), "Authentication failed") ||
  189. strings.Contains(err.Error(), "Bad credentials") ||
  190. strings.Contains(err.Error(), "could not read Username") {
  191. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Authentication failed: %v.", err))
  192. } else if strings.Contains(err.Error(), "fatal:") {
  193. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Migration failed: %v.", err))
  194. } else {
  195. ctx.Error(http.StatusInternalServerError, "MigrateRepository", err)
  196. }
  197. }
  198. }