2019-10-13 07:23:14 -06:00
|
|
|
// Copyright 2019 Gitea. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-13 07:23:14 -06:00
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
package admin
|
2019-10-13 07:23:14 -06:00
|
|
|
|
|
|
|
import (
|
2022-05-20 08:08:52 -06:00
|
|
|
"context"
|
2019-10-13 07:23:14 -06:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 10:03:58 -06:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-11-16 08:25:33 -07:00
|
|
|
"code.gitea.io/gitea/modules/migration"
|
2021-05-31 02:25:47 -06:00
|
|
|
"code.gitea.io/gitea/modules/secret"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-13 07:23:14 -06:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-05-31 02:25:47 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-10-13 07:23:14 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Task represents a task
|
|
|
|
type Task struct {
|
|
|
|
ID int64
|
2021-12-09 18:27:50 -07:00
|
|
|
DoerID int64 `xorm:"index"` // operator
|
|
|
|
Doer *user_model.User `xorm:"-"`
|
|
|
|
OwnerID int64 `xorm:"index"` // repo owner id, when creating, the repoID maybe zero
|
|
|
|
Owner *user_model.User `xorm:"-"`
|
|
|
|
RepoID int64 `xorm:"index"`
|
|
|
|
Repo *repo_model.Repository `xorm:"-"`
|
2019-10-13 07:23:14 -06:00
|
|
|
Type structs.TaskType
|
|
|
|
Status structs.TaskStatus `xorm:"index"`
|
|
|
|
StartTime timeutil.TimeStamp
|
|
|
|
EndTime timeutil.TimeStamp
|
|
|
|
PayloadContent string `xorm:"TEXT"`
|
2023-05-11 02:25:46 -06:00
|
|
|
Message string `xorm:"TEXT"` // if task failed, saved the error reason, it could be a JSON string of TranslatableMessage or a plain message
|
2019-10-13 07:23:14 -06:00
|
|
|
Created timeutil.TimeStamp `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Task))
|
|
|
|
}
|
|
|
|
|
2021-06-16 16:02:24 -06:00
|
|
|
// TranslatableMessage represents JSON struct that can be translated with a Locale
|
|
|
|
type TranslatableMessage struct {
|
|
|
|
Format string
|
2023-07-04 12:36:08 -06:00
|
|
|
Args []any `json:"omitempty"`
|
2021-06-16 16:02:24 -06:00
|
|
|
}
|
|
|
|
|
2019-10-13 07:23:14 -06:00
|
|
|
// LoadRepo loads repository of the task
|
2023-09-16 08:39:12 -06:00
|
|
|
func (task *Task) LoadRepo(ctx context.Context) error {
|
2019-10-13 07:23:14 -06:00
|
|
|
if task.Repo != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-09 18:27:50 -07:00
|
|
|
var repo repo_model.Repository
|
2022-05-20 08:08:52 -06:00
|
|
|
has, err := db.GetEngine(ctx).ID(task.RepoID).Get(&repo)
|
2019-10-13 07:23:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2021-12-09 18:27:50 -07:00
|
|
|
return repo_model.ErrRepoNotExist{
|
2019-10-13 07:23:14 -06:00
|
|
|
ID: task.RepoID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task.Repo = &repo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadDoer loads do user
|
2023-09-16 08:39:12 -06:00
|
|
|
func (task *Task) LoadDoer(ctx context.Context) error {
|
2019-10-13 07:23:14 -06:00
|
|
|
if task.Doer != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
var doer user_model.User
|
2023-09-16 08:39:12 -06:00
|
|
|
has, err := db.GetEngine(ctx).ID(task.DoerID).Get(&doer)
|
2019-10-13 07:23:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2021-11-24 02:49:20 -07:00
|
|
|
return user_model.ErrUserNotExist{
|
2019-10-13 07:23:14 -06:00
|
|
|
UID: task.DoerID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task.Doer = &doer
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadOwner loads owner user
|
2023-09-16 08:39:12 -06:00
|
|
|
func (task *Task) LoadOwner(ctx context.Context) error {
|
2019-10-13 07:23:14 -06:00
|
|
|
if task.Owner != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
var owner user_model.User
|
2023-09-16 08:39:12 -06:00
|
|
|
has, err := db.GetEngine(ctx).ID(task.OwnerID).Get(&owner)
|
2019-10-13 07:23:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2021-11-24 02:49:20 -07:00
|
|
|
return user_model.ErrUserNotExist{
|
2019-10-13 07:23:14 -06:00
|
|
|
UID: task.OwnerID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task.Owner = &owner
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCols updates some columns
|
2023-09-16 08:39:12 -06:00
|
|
|
func (task *Task) UpdateCols(ctx context.Context, cols ...string) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(task.ID).Cols(cols...).Update(task)
|
2019-10-13 07:23:14 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateConfig returns task config when migrate repository
|
2020-09-10 16:29:19 -06:00
|
|
|
func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
|
2019-10-13 07:23:14 -06:00
|
|
|
if task.Type == structs.TaskTypeMigrateRepo {
|
2020-09-10 16:29:19 -06:00
|
|
|
var opts migration.MigrateOptions
|
2019-10-13 07:23:14 -06:00
|
|
|
err := json.Unmarshal([]byte(task.PayloadContent), &opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-31 02:25:47 -06:00
|
|
|
|
|
|
|
// decrypt credentials
|
|
|
|
if opts.CloneAddrEncrypted != "" {
|
|
|
|
if opts.CloneAddr, err = secret.DecryptSecret(setting.SecretKey, opts.CloneAddrEncrypted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.AuthPasswordEncrypted != "" {
|
|
|
|
if opts.AuthPassword, err = secret.DecryptSecret(setting.SecretKey, opts.AuthPasswordEncrypted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.AuthTokenEncrypted != "" {
|
|
|
|
if opts.AuthToken, err = secret.DecryptSecret(setting.SecretKey, opts.AuthTokenEncrypted); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:23:14 -06:00
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrTaskDoesNotExist represents a "TaskDoesNotExist" kind of error.
|
|
|
|
type ErrTaskDoesNotExist struct {
|
|
|
|
ID int64
|
|
|
|
RepoID int64
|
|
|
|
Type structs.TaskType
|
|
|
|
}
|
|
|
|
|
2022-06-24 07:51:37 -06:00
|
|
|
// IsErrTaskDoesNotExist checks if an error is a ErrTaskDoesNotExist.
|
2019-10-13 07:23:14 -06:00
|
|
|
func IsErrTaskDoesNotExist(err error) bool {
|
|
|
|
_, ok := err.(ErrTaskDoesNotExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrTaskDoesNotExist) Error() string {
|
2022-06-24 07:51:37 -06:00
|
|
|
return fmt.Sprintf("task does not exist [id: %d, repo_id: %d, type: %d]",
|
2019-10-13 07:23:14 -06:00
|
|
|
err.ID, err.RepoID, err.Type)
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:50:37 -06:00
|
|
|
func (err ErrTaskDoesNotExist) Unwrap() error {
|
|
|
|
return util.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:23:14 -06:00
|
|
|
// GetMigratingTask returns the migrating task by repo's id
|
2023-09-16 08:39:12 -06:00
|
|
|
func GetMigratingTask(ctx context.Context, repoID int64) (*Task, error) {
|
2021-03-14 12:52:12 -06:00
|
|
|
task := Task{
|
2019-10-13 07:23:14 -06:00
|
|
|
RepoID: repoID,
|
|
|
|
Type: structs.TaskTypeMigrateRepo,
|
|
|
|
}
|
2023-09-16 08:39:12 -06:00
|
|
|
has, err := db.GetEngine(ctx).Get(&task)
|
2019-10-13 07:23:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrTaskDoesNotExist{0, repoID, task.Type}
|
|
|
|
}
|
|
|
|
return &task, nil
|
|
|
|
}
|
|
|
|
|
2020-01-12 05:11:17 -07:00
|
|
|
// CreateTask creates a task on database
|
2023-09-16 08:39:12 -06:00
|
|
|
func CreateTask(ctx context.Context, task *Task) error {
|
|
|
|
return db.Insert(ctx, task)
|
2019-10-13 07:23:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FinishMigrateTask updates database when migrate task finished
|
2023-09-16 08:39:12 -06:00
|
|
|
func FinishMigrateTask(ctx context.Context, task *Task) error {
|
2019-10-13 07:23:14 -06:00
|
|
|
task.Status = structs.TaskStatusFinished
|
|
|
|
task.EndTime = timeutil.TimeStampNow()
|
2021-05-31 02:25:47 -06:00
|
|
|
|
|
|
|
// delete credentials when we're done, they're a liability.
|
|
|
|
conf, err := task.MigrateConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf.AuthPassword = ""
|
|
|
|
conf.AuthToken = ""
|
2022-03-30 20:25:40 -06:00
|
|
|
conf.CloneAddr = util.SanitizeCredentialURLs(conf.CloneAddr)
|
2021-05-31 02:25:47 -06:00
|
|
|
conf.AuthPasswordEncrypted = ""
|
|
|
|
conf.AuthTokenEncrypted = ""
|
|
|
|
conf.CloneAddrEncrypted = ""
|
|
|
|
confBytes, err := json.Marshal(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task.PayloadContent = string(confBytes)
|
|
|
|
|
2023-09-16 08:39:12 -06:00
|
|
|
_, err = db.GetEngine(ctx).ID(task.ID).Cols("status", "end_time", "payload_content").Update(task)
|
2021-11-21 08:41:00 -07:00
|
|
|
return err
|
2019-10-13 07:23:14 -06:00
|
|
|
}
|