Skip to content

Commit 012e45a

Browse files
zeripathwxiaoguang
andauthored
Correctly handle failed migrations (#17575) (#18099)
* Correctly handle failed migrations There is a bug in handling failed migrations whereby the migration task gets decoupled from the migration repository. This leads to a failure of the task to get deleted with the repository and also leads to the migration failed page resulting in a ISE. This PR removes the zeroing out of the task id from the migration but also makes the migration handler tolerate missing tasks much nicer. Fix #17571 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent d25ff0d commit 012e45a

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

modules/task/migrate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func runMigrateTask(t *models.Task) (err error) {
5858
t.EndTime = timeutil.TimeStampNow()
5959
t.Status = structs.TaskStatusFailed
6060
t.Message = err.Error()
61+
// Ensure that the repo loaded before we zero out the repo ID from the task - thus ensuring that we can delete it
62+
_ = t.LoadRepo()
63+
6164
t.RepoID = 0
6265
if err := t.UpdateCols("status", "errors", "repo_id", "end_time"); err != nil {
6366
log.Error("Task UpdateCols failed: %v", err)

modules/task/task.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.
9292
return nil, err
9393
}
9494

95-
var task = models.Task{
95+
var task = &models.Task{
9696
DoerID: doer.ID,
9797
OwnerID: u.ID,
9898
Type: structs.TaskTypeMigrateRepo,
9999
Status: structs.TaskStatusQueue,
100100
PayloadContent: string(bs),
101101
}
102102

103-
if err := models.CreateTask(&task); err != nil {
103+
if err := models.CreateTask(task); err != nil {
104104
return nil, err
105105
}
106106

@@ -128,5 +128,5 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.
128128
return nil, err
129129
}
130130

131-
return &task, nil
131+
return task, nil
132132
}

options/locale/locale_en-US.ini

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,12 @@ migrate.migrate = Migrate From %s
896896
migrate.migrating = Migrating from <b>%s</b> ...
897897
migrate.migrating_failed = Migrating from <b>%s</b> failed.
898898
migrate.migrating_failed.error = Error: %s
899-
migrate.github.description = Migrating data from Github.com or Github Enterprise.
900-
migrate.git.description = Migrating or Mirroring git data from Git services
901-
migrate.gitlab.description = Migrating data from GitLab.com or Self-Hosted gitlab server.
902-
migrate.gitea.description = Migrating data from Gitea.com or Self-Hosted Gitea server.
903-
migrate.gogs.description = Migrating data from notabug.org or other Self-Hosted Gogs server.
899+
migrate.github.description = Migrate data from github.com or other Github instances.
900+
migrate.git.description = Migrate a repository only from any Git service.
901+
migrate.gitlab.description = Migrate data from gitlab.com or other GitLab instances.
902+
migrate.gitea.description = Migrate data from gitea.com or other Gitea instances.
903+
migrate.gogs.description = Migrate data from notabug.org or other Gogs instances.
904+
migrate.onedev.description = Migrate data from code.onedev.io or other OneDev instances.
904905
migrate.migrating_git = Migrating Git Data
905906
migrate.migrating_topics = Migrating Topics
906907
migrate.migrating_milestones = Migrating Milestones

routers/web/repo/view.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,13 @@ func Home(ctx *context.Context) {
619619
if ctx.Repo.Repository.IsBeingCreated() {
620620
task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
621621
if err != nil {
622+
if models.IsErrTaskDoesNotExist(err) {
623+
ctx.Data["Repo"] = ctx.Repo
624+
ctx.Data["CloneAddr"] = ""
625+
ctx.Data["Failed"] = true
626+
ctx.HTML(http.StatusOK, tplMigrating)
627+
return
628+
}
622629
ctx.ServerError("models.GetMigratingTask", err)
623630
return
624631
}

routers/web/user/task.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66

77
import (
88
"net/http"
9+
"strconv"
910

1011
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/context"
@@ -16,6 +17,12 @@ import (
1617
func TaskStatus(ctx *context.Context) {
1718
task, opts, err := models.GetMigratingTaskByID(ctx.ParamsInt64("task"), ctx.User.ID)
1819
if err != nil {
20+
if models.IsErrTaskDoesNotExist(err) {
21+
ctx.JSON(http.StatusNotFound, map[string]interface{}{
22+
"error": "task `" + strconv.FormatInt(ctx.ParamsInt64("task"), 10) + "` does not exist",
23+
})
24+
return
25+
}
1926
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
2027
"err": err,
2128
})

templates/repo/migrate/migrating.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
<p id="repo_migrating_progress_message"></p>
2626
</div>
2727
<div id="repo_migrating_failed" hidden>
28-
<p>{{.i18n.Tr "repo.migrate.migrating_failed" .CloneAddr | Safe}}</p>
28+
{{if .CloneAddr}}
29+
<p>{{.i18n.Tr "repo.migrate.migrating_failed" .CloneAddr | Safe}}</p>
30+
{{else}}
31+
<p>{{.i18n.Tr "repo.migrate.migrating_failed" "<nil>" | Safe}}</p>
32+
{{end}}
2933
<p id="repo_migrating_failed_error"></p>
3034
</div>
3135
{{if and .Failed .Permission.IsAdmin}}

0 commit comments

Comments
 (0)