diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-12 20:11:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-12 20:11:17 +0800 |
commit | b465d0d78793da6e67890a7cb9d3ae1b807c53ca (patch) | |
tree | 1cdfb83699ad2fd31d5835bb2ac1e1b579eaf784 /modules/task | |
parent | 5765212c6dbcaeb27779707af3ca57775e535bd9 (diff) | |
download | gitea-b465d0d78793da6e67890a7cb9d3ae1b807c53ca.tar.gz gitea-b465d0d78793da6e67890a7cb9d3ae1b807c53ca.zip |
Move create/fork repository from models to modules/repository (#9489)
* Move create/fork repository from models to modules/repository
* fix wrong reference
* fix test
* fix test
* fix lint
* Fix DBContext
* remove duplicated TestMain
* fix lint
* fix conflicts
Diffstat (limited to 'modules/task')
-rw-r--r-- | modules/task/task.go | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/modules/task/task.go b/modules/task/task.go index 416f0c696a..72f111ecc7 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -5,6 +5,7 @@ package task import ( + "encoding/json" "fmt" "code.gitea.io/gitea/models" @@ -12,7 +13,9 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/queue" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/timeutil" ) // taskQueue is a global queue of tasks @@ -52,10 +55,56 @@ func handle(data ...queue.Data) { // MigrateRepository add migration repository to task func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error { - task, err := models.CreateMigrateTask(doer, u, opts) + task, err := CreateMigrateTask(doer, u, opts) if err != nil { return err } return taskQueue.Push(task) } + +// CreateMigrateTask creates a migrate task +func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.Task, error) { + bs, err := json.Marshal(&opts) + if err != nil { + return nil, err + } + + var task = models.Task{ + DoerID: doer.ID, + OwnerID: u.ID, + Type: structs.TaskTypeMigrateRepo, + Status: structs.TaskStatusQueue, + PayloadContent: string(bs), + } + + if err := models.CreateTask(&task); err != nil { + return nil, err + } + + repo, err := repo_module.CreateRepository(doer, u, models.CreateRepoOptions{ + Name: opts.RepoName, + Description: opts.Description, + OriginalURL: opts.OriginalURL, + GitServiceType: opts.GitServiceType, + IsPrivate: opts.Private, + IsMirror: opts.Mirror, + Status: models.RepositoryBeingMigrated, + }) + if err != nil { + task.EndTime = timeutil.TimeStampNow() + task.Status = structs.TaskStatusFailed + err2 := task.UpdateCols("end_time", "status") + if err2 != nil { + log.Error("UpdateCols Failed: %v", err2.Error()) + } + return nil, err + } + + task.RepoID = repo.ID + if err = task.UpdateCols("repo_id"); err != nil { + return nil, err + } + + return &task, nil +} |