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.
144 lines
4.2 KiB
144 lines
4.2 KiB
5 years ago
|
// Copyright 2019 Gitea. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package task
|
||
|
|
||
|
import (
|
||
4 years ago
|
"context"
|
||
5 years ago
|
"errors"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/graceful"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/json"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/migration"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/notification"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/process"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/structs"
|
||
|
"code.gitea.io/gitea/modules/timeutil"
|
||
|
"code.gitea.io/gitea/modules/util"
|
||
3 years ago
|
"code.gitea.io/gitea/services/migrations"
|
||
5 years ago
|
)
|
||
|
|
||
4 years ago
|
func handleCreateError(owner *models.User, err error) error {
|
||
5 years ago
|
switch {
|
||
|
case models.IsErrReachLimitOfRepo(err):
|
||
|
return fmt.Errorf("You have already reached your limit of %d repositories", owner.MaxCreationLimit())
|
||
|
case models.IsErrRepoAlreadyExist(err):
|
||
|
return errors.New("The repository name is already used")
|
||
|
case models.IsErrNameReserved(err):
|
||
|
return fmt.Errorf("The repository name '%s' is reserved", err.(models.ErrNameReserved).Name)
|
||
|
case models.IsErrNamePatternNotAllowed(err):
|
||
|
return fmt.Errorf("The pattern '%s' is not allowed in a repository name", err.(models.ErrNamePatternNotAllowed).Pattern)
|
||
|
default:
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func runMigrateTask(t *models.Task) (err error) {
|
||
|
defer func() {
|
||
|
if e := recover(); e != nil {
|
||
4 years ago
|
err = fmt.Errorf("PANIC whilst trying to do migrate task: %v", e)
|
||
|
log.Critical("PANIC during runMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d]: %v\nStacktrace: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, e, log.Stack(2))
|
||
5 years ago
|
}
|
||
|
|
||
|
if err == nil {
|
||
|
err = models.FinishMigrateTask(t)
|
||
|
if err == nil {
|
||
|
notification.NotifyMigrateRepository(t.Doer, t.Owner, t.Repo)
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
log.Error("FinishMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d] failed: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, err)
|
||
5 years ago
|
}
|
||
|
|
||
|
t.EndTime = timeutil.TimeStampNow()
|
||
|
t.Status = structs.TaskStatusFailed
|
||
3 years ago
|
t.Message = err.Error()
|
||
3 years ago
|
// Ensure that the repo loaded before we zero out the repo ID from the task - thus ensuring that we can delete it
|
||
|
_ = t.LoadRepo()
|
||
|
|
||
4 years ago
|
t.RepoID = 0
|
||
|
if err := t.UpdateCols("status", "errors", "repo_id", "end_time"); err != nil {
|
||
4 years ago
|
log.Error("Task UpdateCols failed: %v", err)
|
||
5 years ago
|
}
|
||
|
|
||
|
if t.Repo != nil {
|
||
|
if errDelete := models.DeleteRepository(t.Doer, t.OwnerID, t.Repo.ID); errDelete != nil {
|
||
|
log.Error("DeleteRepository: %v", errDelete)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
4 years ago
|
if err = t.LoadRepo(); err != nil {
|
||
|
return
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// if repository is ready, then just finish the task
|
||
5 years ago
|
if t.Repo.Status == models.RepositoryReady {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
if err = t.LoadDoer(); err != nil {
|
||
|
return
|
||
5 years ago
|
}
|
||
4 years ago
|
if err = t.LoadOwner(); err != nil {
|
||
|
return
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
var opts *migration.MigrateOptions
|
||
5 years ago
|
opts, err = t.MigrateConfig()
|
||
|
if err != nil {
|
||
4 years ago
|
return
|
||
5 years ago
|
}
|
||
|
|
||
|
opts.MigrateToRepoID = t.RepoID
|
||
4 years ago
|
|
||
|
ctx, cancel := context.WithCancel(graceful.GetManager().ShutdownContext())
|
||
|
defer cancel()
|
||
|
pm := process.GetManager()
|
||
|
pid := pm.Add(fmt.Sprintf("MigrateTask: %s/%s", t.Owner.Name, opts.RepoName), cancel)
|
||
|
defer pm.Remove(pid)
|
||
|
|
||
|
t.StartTime = timeutil.TimeStampNow()
|
||
|
t.Status = structs.TaskStatusRunning
|
||
|
if err = t.UpdateCols("start_time", "status"); err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
t.Repo, err = migrations.MigrateRepository(ctx, t.Doer, t.Owner.Name, *opts, func(format string, args ...interface{}) {
|
||
3 years ago
|
message := models.TranslatableMessage{
|
||
|
Format: format,
|
||
|
Args: args,
|
||
|
}
|
||
|
bs, _ := json.Marshal(message)
|
||
|
t.Message = string(bs)
|
||
|
_ = t.UpdateCols("message")
|
||
|
})
|
||
5 years ago
|
if err == nil {
|
||
3 years ago
|
log.Trace("Repository migrated [%d]: %s/%s", t.Repo.ID, t.Owner.Name, t.Repo.Name)
|
||
4 years ago
|
return
|
||
5 years ago
|
}
|
||
|
|
||
|
if models.IsErrRepoAlreadyExist(err) {
|
||
4 years ago
|
err = errors.New("The repository name is already used")
|
||
|
return
|
||
5 years ago
|
}
|
||
|
|
||
|
// remoteAddr may contain credentials, so we sanitize it
|
||
3 years ago
|
err = util.NewStringURLSanitizedError(err, opts.CloneAddr, true)
|
||
5 years ago
|
if strings.Contains(err.Error(), "Authentication failed") ||
|
||
|
strings.Contains(err.Error(), "could not read Username") {
|
||
|
return fmt.Errorf("Authentication failed: %v", err.Error())
|
||
|
} else if strings.Contains(err.Error(), "fatal:") {
|
||
|
return fmt.Errorf("Migration failed: %v", err.Error())
|
||
|
}
|
||
|
|
||
4 years ago
|
// do not be tempted to coalesce this line with the return
|
||
|
err = handleCreateError(t.Owner, err)
|
||
|
return
|
||
5 years ago
|
}
|