Skip to content

Make Migrations Cancelable #12917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package git
import (
"bytes"
"container/list"
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -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 := NewCommandContextNoGlobals(ctx, args...).AddArguments("clone")
if opts.Mirror {
cmd.AddArguments("--mirror")
}
Expand Down
2 changes: 1 addition & 1 deletion modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package repository

import (
"context"
"fmt"
"path"
"strings"
Expand Down Expand Up @@ -41,7 +42,7 @@ func UncycloRemoteURL(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() {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion modules/task/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package task

import (
"context"
"errors"
"fmt"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ migrate.migrate_items_options = Access Token is required to migrate additional i
migrated_from = Migrated from <a href="%[1]s">%[2]s</a>
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 <b>%s</b> ...
migrate.migrating_failed = Migrating from <b>%s</b> failed.
migrate.github.description = Migrating data from Github.com or Github Enterprise.
Expand Down
30 changes: 30 additions & 0 deletions routers/repo/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
package repo

import (
"fmt"
"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/process"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/task"
Expand Down Expand Up @@ -188,3 +192,29 @@ 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
}

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
}
}

ctx.Flash.Error(ctx.Tr("repo.migrate.task_not_found", ctx.Repo.Repository.FullName()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user will see a migrating task, but when click cancel, gitea will say it's not exist. This is still a problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even sensible to flash a message if the next action is to redirect back to the homepage?

ctx.Redirect(ctx.Repo.Owner.DashboardLink())
}
1 change: 1 addition & 0 deletions routers/routes/macaron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion services/mirror/mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mirror

import (
"context"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions templates/repo/migrate/migrating.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
</div>
</div>
</div>
{{if .Permission.IsAdmin}}
<div class="ui divider"></div>
<div class="item center">
<form action="{{.Link}}/migrate/cancel" method="post">
{{.CsrfTokenHtml}}
<button class="ui button" data-do="delete">
<span class="item text">{{.i18n.Tr "cancel"}}</span>
</button>
</form>
</div>
{{end}}
</div>
</div>
</div>
Expand Down