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.

119 lines
3.3 KiB

  1. // Copyright 2019 Gitea. 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 task
  5. import (
  6. "bytes"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/graceful"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/migrations"
  14. "code.gitea.io/gitea/modules/notification"
  15. "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/modules/timeutil"
  17. "code.gitea.io/gitea/modules/util"
  18. )
  19. func handleCreateError(owner *models.User, err error, name string) error {
  20. switch {
  21. case models.IsErrReachLimitOfRepo(err):
  22. return fmt.Errorf("You have already reached your limit of %d repositories", owner.MaxCreationLimit())
  23. case models.IsErrRepoAlreadyExist(err):
  24. return errors.New("The repository name is already used")
  25. case models.IsErrNameReserved(err):
  26. return fmt.Errorf("The repository name '%s' is reserved", err.(models.ErrNameReserved).Name)
  27. case models.IsErrNamePatternNotAllowed(err):
  28. return fmt.Errorf("The pattern '%s' is not allowed in a repository name", err.(models.ErrNamePatternNotAllowed).Pattern)
  29. default:
  30. return err
  31. }
  32. }
  33. func runMigrateTask(t *models.Task) (err error) {
  34. defer func() {
  35. if e := recover(); e != nil {
  36. var buf bytes.Buffer
  37. fmt.Fprintf(&buf, "Handler crashed with error: %v", log.Stack(2))
  38. err = errors.New(buf.String())
  39. }
  40. if err == nil {
  41. err = models.FinishMigrateTask(t)
  42. if err == nil {
  43. notification.NotifyMigrateRepository(t.Doer, t.Owner, t.Repo)
  44. return
  45. }
  46. log.Error("FinishMigrateTask failed: %s", err.Error())
  47. }
  48. t.EndTime = timeutil.TimeStampNow()
  49. t.Status = structs.TaskStatusFailed
  50. t.Errors = err.Error()
  51. if err := t.UpdateCols("status", "errors", "end_time"); err != nil {
  52. log.Error("Task UpdateCols failed: %s", err.Error())
  53. }
  54. if t.Repo != nil {
  55. if errDelete := models.DeleteRepository(t.Doer, t.OwnerID, t.Repo.ID); errDelete != nil {
  56. log.Error("DeleteRepository: %v", errDelete)
  57. }
  58. }
  59. }()
  60. if err := t.LoadRepo(); err != nil {
  61. return err
  62. }
  63. // if repository is ready, then just finsih the task
  64. if t.Repo.Status == models.RepositoryReady {
  65. return nil
  66. }
  67. if err := t.LoadDoer(); err != nil {
  68. return err
  69. }
  70. if err := t.LoadOwner(); err != nil {
  71. return err
  72. }
  73. t.StartTime = timeutil.TimeStampNow()
  74. t.Status = structs.TaskStatusRunning
  75. if err := t.UpdateCols("start_time", "status"); err != nil {
  76. return err
  77. }
  78. var opts *structs.MigrateRepoOption
  79. opts, err = t.MigrateConfig()
  80. if err != nil {
  81. return err
  82. }
  83. opts.MigrateToRepoID = t.RepoID
  84. repo, err := migrations.MigrateRepository(graceful.GetManager().HammerContext(), t.Doer, t.Owner.Name, *opts)
  85. if err == nil {
  86. log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
  87. return nil
  88. }
  89. if models.IsErrRepoAlreadyExist(err) {
  90. return errors.New("The repository name is already used")
  91. }
  92. // remoteAddr may contain credentials, so we sanitize it
  93. err = util.URLSanitizedError(err, opts.CloneAddr)
  94. if strings.Contains(err.Error(), "Authentication failed") ||
  95. strings.Contains(err.Error(), "could not read Username") {
  96. return fmt.Errorf("Authentication failed: %v", err.Error())
  97. } else if strings.Contains(err.Error(), "fatal:") {
  98. return fmt.Errorf("Migration failed: %v", err.Error())
  99. }
  100. return handleCreateError(t.Owner, err, "MigratePost")
  101. }