From b5c9eafaf49813395d440a099f9dad1c5e948704 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 19 Sep 2020 18:09:08 +0200 Subject: [PATCH 01/13] Add Cancle button for Migration --- routers/repo/migrate.go | 25 +++++++++++++++++++++++++ routers/routes/macaron.go | 1 + templates/repo/migrate/migrating.tmpl | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index d843a043a7a53..8d19a759482e5 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -6,17 +6,20 @@ package repo import ( + "net/http" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/task" "code.gitea.io/gitea/modules/util" + repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -188,3 +191,25 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) { handleMigrateError(ctx, ctxUser, err, "MigratePost", tpl, &form) } + +// CancelMigration cancels a running migration +func CancelMigration(ctx *context.Context) { + if !ctx.Repo.IsOwner() { + ctx.Error(http.StatusNotFound) + return + } + + if ctx.Repo.Repository.Status != models.RepositoryBeingMigrated { + ctx.Error(http.StatusConflict, "repo already migrated") + return + } + + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { + ctx.ServerError("DeleteRepository", err) + return + } + log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + + ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) + ctx.Redirect(ctx.Repo.Owner.DashboardLink()) +} diff --git a/routers/routes/macaron.go b/routers/routes/macaron.go index 170bc7d493dff..c2b877c8ac4a3 100644 --- a/routers/routes/macaron.go +++ b/routers/routes/macaron.go @@ -529,6 +529,7 @@ func RegisterMacaronRoutes(m *macaron.Macaron) { m.Get("/:username/:reponame/releases/download/:vTag/:fileName", ignSignIn, context.RepoAssignment(), repo.MustBeNotEmpty, repo.RedirectDownload) m.Group("/:username/:reponame", func() { + m.Post("/migrate/cancel", repo.CancelMigration) m.Group("/settings", func() { m.Combo("").Get(repo.Settings). Post(bindIgnErr(auth.RepoSettingForm{}), repo.SettingsPost) diff --git a/templates/repo/migrate/migrating.tmpl b/templates/repo/migrate/migrating.tmpl index a4ec15fa9a660..638e6d4343498 100644 --- a/templates/repo/migrate/migrating.tmpl +++ b/templates/repo/migrate/migrating.tmpl @@ -29,6 +29,17 @@ + {{if .Permission.IsAdmin}} +
+
+
+ {{.CsrfTokenHtml}} + +
+
+ {{end}} From fe9557a815a0e5d8e30c8ba4487f2c74b3df946b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 28 Nov 2020 23:56:07 +0100 Subject: [PATCH 02/13] Add TODO, as it is not doable now ... since our queues do not allow such things at the moment :/ --- routers/repo/migrate.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 8d19a759482e5..2078e6faab666 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -204,6 +204,9 @@ func CancelMigration(ctx *context.Context) { return } + // TODO: get task from queues and kill it + // https://github.com/go-gitea/gitea/pull/12917/files#r492109921 + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { ctx.ServerError("DeleteRepository", err) return From 8da1497b96a7f1c95210e7606fde767916f9dbe2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 29 Nov 2020 21:17:48 +0100 Subject: [PATCH 03/13] own test for migration canceled (successfuly) --- options/locale/locale_en-US.ini | 1 + routers/repo/migrate.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 1500bf73e9084..26d04bf27146f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -759,6 +759,7 @@ migrate.migrate_items_options = Access Token is required to migrate additional i migrated_from = Migrated from %[2]s migrated_from_fake = Migrated From %[1]s migrate.migrate = Migrate From %s +migrate.cancelled = The migration has been cancelled. migrate.migrating = Migrating from %s ... migrate.migrating_failed = Migrating from %s failed. migrate.github.description = Migrating data from Github.com or Github Enterprise. diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 2078e6faab666..73e4b75b9dd79 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -213,6 +213,6 @@ func CancelMigration(ctx *context.Context) { } log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) + ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) ctx.Redirect(ctx.Repo.Owner.DashboardLink()) } From 714351aca2c2bf8220b9d85aaf7687864a0b013c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 1 Dec 2020 19:20:15 +0100 Subject: [PATCH 04/13] run migrations wich process Manager --- modules/task/migrate.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/task/migrate.go b/modules/task/migrate.go index 99f0435b280d0..8082056a44af1 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -5,6 +5,7 @@ package task import ( + "context" "errors" "fmt" "strings" @@ -15,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/migrations" migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -96,7 +98,13 @@ func runMigrateTask(t *models.Task) (err error) { opts.MigrateToRepoID = t.RepoID var repo *models.Repository - repo, err = migrations.MigrateRepository(graceful.GetManager().HammerContext(), t.Doer, t.Owner.Name, *opts) + + 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) + repo, err = migrations.MigrateRepository(ctx, t.Doer, t.Owner.Name, *opts) if err == nil { log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name) return From fd0971578358e852cff5c273dc04c5f8da1a48d5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 1 Dec 2020 21:21:04 +0100 Subject: [PATCH 05/13] Migration: use migration context for git clone commands --- integrations/git_helper_for_declarative_test.go | 2 +- modules/git/git.go | 1 + modules/git/repo.go | 12 +++++++++--- modules/migrations/gitea_uploader.go | 2 +- modules/repository/repo.go | 7 ++++--- services/mirror/mirror_test.go | 3 ++- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/integrations/git_helper_for_declarative_test.go b/integrations/git_helper_for_declarative_test.go index 04b4c990139d3..f681cd143a12e 100644 --- a/integrations/git_helper_for_declarative_test.go +++ b/integrations/git_helper_for_declarative_test.go @@ -111,7 +111,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) { return func(t *testing.T) { - assert.NoError(t, git.CloneWithArgs(u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{})) + assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{})) assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md"))) } } diff --git a/modules/git/git.go b/modules/git/git.go index cce5cc738da7f..6b15138a5c748 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -32,6 +32,7 @@ var ( GitExecutable = "git" // DefaultContext is the default context to run git commands in + // will be overwritten by Init with HammerContext DefaultContext = context.Background() gitVersion *version.Version diff --git a/modules/git/repo.go b/modules/git/repo.go index ae370d3da973a..d0bbf77f174ff 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -8,6 +8,7 @@ package git import ( "bytes" "container/list" + "context" "errors" "fmt" "os" @@ -166,19 +167,24 @@ type CloneRepoOptions struct { // Clone clones original repository to target path. func Clone(from, to string, opts CloneRepoOptions) (err error) { + return CloneWithContext(DefaultContext, from, to, opts) +} + +// CloneWithContext clones original repository to target path. +func CloneWithContext(ctx context.Context, from, to string, opts CloneRepoOptions) (err error) { cargs := make([]string, len(GlobalCommandArgs)) copy(cargs, GlobalCommandArgs) - return CloneWithArgs(from, to, cargs, opts) + return CloneWithArgs(ctx, from, to, cargs, opts) } // CloneWithArgs original repository to target path. -func CloneWithArgs(from, to string, args []string, opts CloneRepoOptions) (err error) { +func CloneWithArgs(ctx context.Context, from, to string, args []string, opts CloneRepoOptions) (err error) { toDir := path.Dir(to) if err = os.MkdirAll(toDir, os.ModePerm); err != nil { return err } - cmd := NewCommandNoGlobals(args...).AddArguments("clone") + cmd := NewCommandNoGlobals(args...).AddArguments("clone").SetParentContext(ctx) if opts.Mirror { cmd.AddArguments("--mirror") } diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go index 466c832754c30..2cb19685fcb9e 100644 --- a/modules/migrations/gitea_uploader.go +++ b/modules/migrations/gitea_uploader.go @@ -125,7 +125,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate } r.DefaultBranch = repo.DefaultBranch - r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, base.MigrateOptions{ + r, err = repository.MigrateRepositoryGitData(g.ctx, owner, r, base.MigrateOptions{ RepoName: g.repoName, Description: repo.Description, OriginalURL: repo.OriginalURL, diff --git a/modules/repository/repo.go b/modules/repository/repo.go index b18dfddd2e5ab..8ecb43ede6e11 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -5,6 +5,7 @@ package repository import ( + "context" "fmt" "path" "strings" @@ -41,7 +42,7 @@ func WikiRemoteURL(remote string) string { } // MigrateRepositoryGitData starts migrating git related data after created migrating repository -func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) { +func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.Repository, opts migration.MigrateOptions) (*models.Repository, error) { repoPath := models.RepoPath(u.Name, opts.RepoName) if u.IsOrganization() { @@ -61,7 +62,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err) } - if err = git.Clone(opts.CloneAddr, repoPath, git.CloneRepoOptions{ + if err = git.CloneWithContext(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{ Mirror: true, Quiet: true, Timeout: migrateTimeout, @@ -77,7 +78,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err) } - if err = git.Clone(wikiRemotePath, wikiPath, git.CloneRepoOptions{ + if err = git.CloneWithContext(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{ Mirror: true, Quiet: true, Timeout: migrateTimeout, diff --git a/services/mirror/mirror_test.go b/services/mirror/mirror_test.go index 79e18885b3383..ddfb6c676b6a7 100644 --- a/services/mirror/mirror_test.go +++ b/services/mirror/mirror_test.go @@ -5,6 +5,7 @@ package mirror import ( + "context" "path/filepath" "testing" @@ -47,7 +48,7 @@ func TestRelease_MirrorDelete(t *testing.T) { }) assert.NoError(t, err) - mirror, err := repository.MigrateRepositoryGitData(user, user, mirrorRepo, opts) + mirror, err := repository.MigrateRepositoryGitData(context.Background(), user, mirrorRepo, opts) assert.NoError(t, err) gitRepo, err := git.OpenRepository(repoPath) From e174d8c8013e1e3a25eb0389c8f8ef823ef7e35a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 1 Dec 2020 22:04:47 +0100 Subject: [PATCH 06/13] get task from process manager and kill it --- options/locale/locale_en-US.ini | 1 + routers/repo/migrate.go | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 26d04bf27146f..698ae3ce8503b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -760,6 +760,7 @@ migrated_from = Migrated from %[2]s migrated_from_fake = Migrated From %[1]s migrate.migrate = Migrate From %s migrate.cancelled = The migration has been cancelled. +migrate.task_not_found = The migration task for %s not found. migrate.migrating = Migrating from %s ... migrate.migrating_failed = Migrating from %s failed. migrate.github.description = Migrating data from Github.com or Github Enterprise. diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 73e4b75b9dd79..2569f46a5a0ff 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -6,6 +6,7 @@ package repo import ( + "fmt" "net/http" "strings" @@ -15,11 +16,11 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migrations" + "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/task" "code.gitea.io/gitea/modules/util" - repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -204,15 +205,16 @@ func CancelMigration(ctx *context.Context) { return } - // TODO: get task from queues and kill it - // https://github.com/go-gitea/gitea/pull/12917/files#r492109921 - - if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { - ctx.ServerError("DeleteRepository", err) - return + for _, ps := range process.GetManager().Processes() { + if ps.Description == fmt.Sprintf("MigrateTask: %s", ctx.Repo.Repository.FullName()) { + log.Trace("Migration Canceled: %s", ctx.Repo.Repository.FullName()) + process.GetManager().Cancel(ps.PID) + ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) + ctx.Redirect(ctx.Repo.Owner.DashboardLink()) + return + } } - log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) + ctx.Flash.Error(ctx.Tr("repo.migrate.migrate.task_not_found", ctx.Repo.Repository.FullName())) ctx.Redirect(ctx.Repo.Owner.DashboardLink()) } From ab941d76bdce1c0b564a3f29933cb6659828e65b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 1 Dec 2020 22:12:55 +0100 Subject: [PATCH 07/13] locale fix --- routers/repo/migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 2569f46a5a0ff..214ff86abb9ce 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -215,6 +215,6 @@ func CancelMigration(ctx *context.Context) { } } - ctx.Flash.Error(ctx.Tr("repo.migrate.migrate.task_not_found", ctx.Repo.Repository.FullName())) + ctx.Flash.Error(ctx.Tr("repo.migrate.task_not_found", ctx.Repo.Repository.FullName())) ctx.Redirect(ctx.Repo.Owner.DashboardLink()) } From 6d91ba85c79ca1f9d75f242dec01839e9999e190 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 1 Dec 2020 22:14:56 +0100 Subject: [PATCH 08/13] Update modules/git/repo.go Co-authored-by: zeripath --- modules/git/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/repo.go b/modules/git/repo.go index d0bbf77f174ff..9b1da87a32e8e 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -184,7 +184,7 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo return err } - cmd := NewCommandNoGlobals(args...).AddArguments("clone").SetParentContext(ctx) + cmd := NewCommandContextNoGlobals(ctx, args...).AddArguments("clone") if opts.Mirror { cmd.AddArguments("--mirror") } From cab5d46322ef005b670b1077d883af615f7e2414 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 2 Dec 2020 00:01:22 +0100 Subject: [PATCH 09/13] Use GUID and PID to get a process --- models/task.go | 12 ++++++++++++ modules/process/manager.go | 11 +++++++++++ modules/task/migrate.go | 13 ++++++++----- routers/repo/migrate.go | 22 +++++++++++++--------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/models/task.go b/models/task.go index b86314b44938a..2af55d96654be 100644 --- a/models/task.go +++ b/models/task.go @@ -31,6 +31,7 @@ type Task struct { PayloadContent string `xorm:"TEXT"` Errors string `xorm:"TEXT"` // if task failed, saved the error reason Created timeutil.TimeStamp `xorm:"created"` + Process string // process GUID and PID } // LoadRepo loads repository of the task @@ -114,6 +115,17 @@ func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) { return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name()) } +// GUIDandPID return GUID and PID of a running task +func (task *Task) GUIDandPID() (guid int64, pid int64) { + if task.Status != structs.TaskStatusRunning { + return 0, 0 + } + if _, err := fmt.Sscanf(task.Process, "%d/%d", &guid, &pid); err != nil { + return 0, 0 + } + return +} + // ErrTaskDoesNotExist represents a "TaskDoesNotExist" kind of error. type ErrTaskDoesNotExist struct { ID int64 diff --git a/modules/process/manager.go b/modules/process/manager.go index 27ed1d4964d15..cf108e2ce913b 100644 --- a/modules/process/manager.go +++ b/modules/process/manager.go @@ -15,6 +15,8 @@ import ( "sort" "sync" "time" + + gouuid "github.com/google/uuid" ) // TODO: This packages still uses a singleton for the Manager. @@ -44,6 +46,9 @@ type Manager struct { counter int64 processes map[int64]*Process + + // guid is a unique id for a gitea instance + guid int64 } // GetManager returns a Manager and initializes one as singleton if there's none yet @@ -51,11 +56,17 @@ func GetManager() *Manager { if manager == nil { manager = &Manager{ processes: make(map[int64]*Process), + guid: int64(gouuid.New().ID()), } } return manager } +// GUID return a unique id of a gitea instance +func (pm *Manager) GUID() int64 { + return pm.guid +} + // Add a process to the ProcessManager and returns its PID. func (pm *Manager) Add(description string, cancel context.CancelFunc) int64 { pm.mutex.Lock() diff --git a/modules/task/migrate.go b/modules/task/migrate.go index 8082056a44af1..85b621016a720 100644 --- a/modules/task/migrate.go +++ b/modules/task/migrate.go @@ -84,11 +84,6 @@ func runMigrateTask(t *models.Task) (err error) { if err = t.LoadOwner(); err != nil { return } - t.StartTime = timeutil.TimeStampNow() - t.Status = structs.TaskStatusRunning - if err = t.UpdateCols("start_time", "status"); err != nil { - return - } var opts *migration.MigrateOptions opts, err = t.MigrateConfig() @@ -104,6 +99,14 @@ func runMigrateTask(t *models.Task) (err error) { 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 + t.Process = fmt.Sprintf("%d/%d", process.GetManager().GUID(), pid) + if err = t.UpdateCols("start_time", "status", "process"); err != nil { + return + } + repo, err = migrations.MigrateRepository(ctx, t.Doer, t.Owner.Name, *opts) if err == nil { log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 214ff86abb9ce..2240be9f014b0 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -6,7 +6,6 @@ package repo import ( - "fmt" "net/http" "strings" @@ -205,14 +204,19 @@ func CancelMigration(ctx *context.Context) { return } - for _, ps := range process.GetManager().Processes() { - if ps.Description == fmt.Sprintf("MigrateTask: %s", ctx.Repo.Repository.FullName()) { - log.Trace("Migration Canceled: %s", ctx.Repo.Repository.FullName()) - process.GetManager().Cancel(ps.PID) - ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) - ctx.Redirect(ctx.Repo.Owner.DashboardLink()) - return - } + guid := process.GetManager().GUID() + task, err := models.GetMigratingTask(ctx.Repo.Repository.ID) + if err != nil { + ctx.InternalServerError(err) + return + } + tGUID, tPID := task.GUIDandPID() + if guid == tGUID { + log.Trace("Migration [%s] cancel PID [%d] ", ctx.Repo.Repository.FullName(), tPID) + process.GetManager().Cancel(tPID) + ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) + ctx.Redirect(ctx.Repo.Owner.DashboardLink()) + return } ctx.Flash.Error(ctx.Tr("repo.migrate.task_not_found", ctx.Repo.Repository.FullName())) From a00915094aa38c792a5a147f1e150828654a1964 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 2 Dec 2020 00:24:59 +0100 Subject: [PATCH 10/13] Add DB-Migration & fix some code comments --- models/migrations/migrations.go | 27 +++++++++++++++++++-------- models/migrations/v161.go | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 models/migrations/v161.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 6b13719b449ab..75730a58eec95 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -57,7 +57,7 @@ type Version struct { // update minDBVersion accordingly var migrations = []Migration{ - // Gitea 1.5.3 ends at v70 + // Gitea 1.5.0 ends at v69 // v70 -> v71 NewMigration("add issue_dependencies", addIssueDependencies), @@ -66,7 +66,7 @@ var migrations = []Migration{ // v72 -> v73 NewMigration("add review", addReview), - // Gitea 1.6.4 ends at v73 + // Gitea 1.6.0 ends at v73 // v73 -> v74 NewMigration("add must_change_password column for users table", addMustChangePassword), @@ -75,7 +75,7 @@ var migrations = []Migration{ // v75 -> v76 NewMigration("clear nonused data which not deleted when user was deleted", clearNonusedData), - // Gitea 1.7.6 ends at v76 + // Gitea 1.7.0 ends at v76 // v76 -> v77 NewMigration("add pull request rebase with merge commit", addPullRequestRebaseWithMerge), @@ -90,7 +90,7 @@ var migrations = []Migration{ // v81 -> v82 NewMigration("update U2F counter type", changeU2FCounterType), - // Gitea 1.8.3 ends at v82 + // Gitea 1.8.0 ends at v82 // v82 -> v83 NewMigration("hot fix for wrong release sha1 on release table", fixReleaseSha1OnReleaseTable), @@ -105,7 +105,7 @@ var migrations = []Migration{ // v87 -> v88 NewMigration("add avatar field to repository", addAvatarFieldToRepository), - // Gitea 1.9.6 ends at v88 + // Gitea 1.9.0 ends at v88 // v88 -> v89 NewMigration("add commit status context field to commit_status", addCommitStatusContext), @@ -129,14 +129,14 @@ var migrations = []Migration{ NewMigration("add repo_admin_change_team_access to user", addRepoAdminChangeTeamAccessColumnForUser), // v98 -> v99 NewMigration("add original author name and id on migrated release", addOriginalAuthorOnMigratedReleases), - - // Gitea 1.10.3 ends at v99 - // v99 -> v100 NewMigration("add task table and status column for repository table", addTaskTable), // v100 -> v101 NewMigration("update migration repositories' service type", updateMigrationServiceTypes), // v101 -> v102 + + // Gitea 1.10.0 ends at v102 + NewMigration("change length of some external login users columns", changeSomeColumnsLengthOfExternalLoginUser), // v102 -> v103 NewMigration("update migration repositories' service type", dropColumnHeadUserNameOnPullRequest), @@ -167,6 +167,9 @@ var migrations = []Migration{ // v115 -> v116 NewMigration("add user_id prefix to existing user avatar name", renameExistingUserAvatarName), // v116 -> v117 + + // Gitea 1.11.0 ends at v117 + NewMigration("Extend TrackedTimes", extendTrackedTimes), // v117 -> v118 NewMigration("Add block on rejected reviews branch protection", addBlockOnRejectedReviews), @@ -213,6 +216,9 @@ var migrations = []Migration{ // v138 -> v139 NewMigration("Add ResolveDoerID to Comment table", addResolveDoerIDCommentColumn), // v139 -> v140 + + // Gitea 1.12.0 ends at v140 + NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs), // v140 -> v141 NewMigration("Save detected language file size to database instead of percent", fixLanguageStatsToSaveSize), @@ -243,6 +249,9 @@ var migrations = []Migration{ // v153 > v154 NewMigration("add Team review request support", addTeamReviewRequestSupport), // v154 > v155 + + // Gitea 1.13.0 ends at v155 + NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps), // v155 -> v156 NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn), @@ -256,6 +265,8 @@ var migrations = []Migration{ NewMigration("update reactions constraint", updateReactionConstraint), // v160 -> v161 NewMigration("Add block on official review requests branch protection", addBlockOnOfficialReviewRequests), + // v161 -> v162 + NewMigration("Add process field to task table", addProcessToTask), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v161.go b/models/migrations/v161.go new file mode 100644 index 0000000000000..5eddbb331b3df --- /dev/null +++ b/models/migrations/v161.go @@ -0,0 +1,17 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "xorm.io/xorm" +) + +func addProcessToTask(x *xorm.Engine) error { + type Task struct { + Process string // process GUID and PID + } + + return x.Sync2(new(Task)) +} From 1d206d84e301bae6102c2da4b82346ae6da264cc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 2 Dec 2020 00:48:27 +0100 Subject: [PATCH 11/13] Cancle not running tasks too --- routers/repo/migrate.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 2240be9f014b0..00f1bd3c55629 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -19,7 +19,9 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/task" + "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" + repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -210,6 +212,25 @@ func CancelMigration(ctx *context.Context) { ctx.InternalServerError(err) return } + if task.Status != structs.TaskStatusRunning { + task.Status = structs.TaskStatusStopped + task.EndTime = timeutil.TimeStampNow() + task.RepoID = 0 + + if err = task.UpdateCols("status", "repo_id", "end_time"); err != nil { + log.Error("Task UpdateCols failed: %v", err) + } + + if err := repo_service.DeleteRepository(ctx.User, ctx.Repo.Repository); err != nil { + ctx.ServerError("DeleteRepository", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.migrate.cancelled")) + ctx.Redirect(ctx.Repo.Owner.DashboardLink()) + return + } + tGUID, tPID := task.GUIDandPID() if guid == tGUID { log.Trace("Migration [%s] cancel PID [%d] ", ctx.Repo.Repository.FullName(), tPID) From 934ed0186d3285d531621e776266bf71e10c58d0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 2 Dec 2020 16:22:11 +0100 Subject: [PATCH 12/13] rm unrelated --- models/migrations/migrations.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 75730a58eec95..7f391593d19d0 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -57,7 +57,7 @@ type Version struct { // update minDBVersion accordingly var migrations = []Migration{ - // Gitea 1.5.0 ends at v69 + // Gitea 1.5.3 ends at v70 // v70 -> v71 NewMigration("add issue_dependencies", addIssueDependencies), @@ -66,7 +66,7 @@ var migrations = []Migration{ // v72 -> v73 NewMigration("add review", addReview), - // Gitea 1.6.0 ends at v73 + // Gitea 1.6.4 ends at v73 // v73 -> v74 NewMigration("add must_change_password column for users table", addMustChangePassword), @@ -75,7 +75,7 @@ var migrations = []Migration{ // v75 -> v76 NewMigration("clear nonused data which not deleted when user was deleted", clearNonusedData), - // Gitea 1.7.0 ends at v76 + // Gitea 1.7.6 ends at v76 // v76 -> v77 NewMigration("add pull request rebase with merge commit", addPullRequestRebaseWithMerge), @@ -90,7 +90,7 @@ var migrations = []Migration{ // v81 -> v82 NewMigration("update U2F counter type", changeU2FCounterType), - // Gitea 1.8.0 ends at v82 + // Gitea 1.8.3 ends at v82 // v82 -> v83 NewMigration("hot fix for wrong release sha1 on release table", fixReleaseSha1OnReleaseTable), @@ -105,7 +105,7 @@ var migrations = []Migration{ // v87 -> v88 NewMigration("add avatar field to repository", addAvatarFieldToRepository), - // Gitea 1.9.0 ends at v88 + // Gitea 1.9.6 ends at v88 // v88 -> v89 NewMigration("add commit status context field to commit_status", addCommitStatusContext), @@ -129,14 +129,14 @@ var migrations = []Migration{ NewMigration("add repo_admin_change_team_access to user", addRepoAdminChangeTeamAccessColumnForUser), // v98 -> v99 NewMigration("add original author name and id on migrated release", addOriginalAuthorOnMigratedReleases), + + // Gitea 1.10.3 ends at v99 + // v99 -> v100 NewMigration("add task table and status column for repository table", addTaskTable), // v100 -> v101 NewMigration("update migration repositories' service type", updateMigrationServiceTypes), // v101 -> v102 - - // Gitea 1.10.0 ends at v102 - NewMigration("change length of some external login users columns", changeSomeColumnsLengthOfExternalLoginUser), // v102 -> v103 NewMigration("update migration repositories' service type", dropColumnHeadUserNameOnPullRequest), @@ -167,9 +167,6 @@ var migrations = []Migration{ // v115 -> v116 NewMigration("add user_id prefix to existing user avatar name", renameExistingUserAvatarName), // v116 -> v117 - - // Gitea 1.11.0 ends at v117 - NewMigration("Extend TrackedTimes", extendTrackedTimes), // v117 -> v118 NewMigration("Add block on rejected reviews branch protection", addBlockOnRejectedReviews), @@ -216,9 +213,6 @@ var migrations = []Migration{ // v138 -> v139 NewMigration("Add ResolveDoerID to Comment table", addResolveDoerIDCommentColumn), // v139 -> v140 - - // Gitea 1.12.0 ends at v140 - NewMigration("prepend refs/heads/ to issue refs", prependRefsHeadsToIssueRefs), // v140 -> v141 NewMigration("Save detected language file size to database instead of percent", fixLanguageStatsToSaveSize), @@ -249,9 +243,6 @@ var migrations = []Migration{ // v153 > v154 NewMigration("add Team review request support", addTeamReviewRequestSupport), // v154 > v155 - - // Gitea 1.13.0 ends at v155 - NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps), // v155 -> v156 NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn), From b5c24617a601c65c0abd796cd08b74acdece065c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 4 Jan 2022 05:58:13 +0100 Subject: [PATCH 13/13] Update routers/repo/migrate.go Co-authored-by: delvh --- routers/repo/migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/migrate.go b/routers/repo/migrate.go index 00f1bd3c55629..b616f24e1e488 100644 --- a/routers/repo/migrate.go +++ b/routers/repo/migrate.go @@ -196,7 +196,7 @@ func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) { // CancelMigration cancels a running migration func CancelMigration(ctx *context.Context) { - if !ctx.Repo.IsOwner() { + if !ctx.Repo.IsAdmin() { ctx.Error(http.StatusNotFound) return }