From 865d2221c0f4b2a8623ff9299930c9bab0da2c78 Mon Sep 17 00:00:00 2001
From: Kerwin Bryant <kerwin612@qq.com>
Date: Fri, 4 Aug 2023 10:21:32 +0800
Subject: [PATCH] Add `Retry` button when creating a mirror-repo fails (#26228)

fixed #26156
* Added a retry button in the frontend (only displayed when the status
is abnormal)
* After clicking Retry, the backend adds the task back to the task queue


![7UJDNM671RI})EA8~~XPL39](https://github.com/go-gitea/gitea/assets/3371163/e088fd63-5dcc-4bc6-8849-7db3086511b7)

![T83F1WL9)VGHR@MB956$VT9](https://github.com/go-gitea/gitea/assets/3371163/744425bb-dde1-4315-be2e-5c99ac3a44d4)

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
---
 options/locale/locale_en-US.ini       |  1 +
 routers/web/repo/migrate.go           |  9 +++++++++
 routers/web/web.go                    |  6 +++++-
 services/task/task.go                 | 24 ++++++++++++++++++++++++
 templates/repo/migrate/migrating.tmpl |  5 +++--
 web_src/js/features/repo-migrate.js   | 16 +++++++++++++++-
 6 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini
index 24087cdd1b..b2eeab617e 100644
--- a/options/locale/locale_en-US.ini
+++ b/options/locale/locale_en-US.ini
@@ -80,6 +80,7 @@ milestones = Milestones
 
 ok = OK
 cancel = Cancel
+retry = Retry
 rerun = Re-run
 rerun_all = Re-run all jobs
 save = Save
diff --git a/routers/web/repo/migrate.go b/routers/web/repo/migrate.go
index b918650d1d..a6125a1a58 100644
--- a/routers/web/repo/migrate.go
+++ b/routers/web/repo/migrate.go
@@ -259,6 +259,15 @@ func setMigrationContextData(ctx *context.Context, serviceType structs.GitServic
 	ctx.Data["service"] = serviceType
 }
 
+func MigrateRetryPost(ctx *context.Context) {
+	if err := task.RetryMigrateTask(ctx.Repo.Repository.ID); err != nil {
+		log.Error("Retry task failed: %v", err)
+		ctx.ServerError("task.RetryMigrateTask", err)
+		return
+	}
+	ctx.JSONOK()
+}
+
 func MigrateCancelPost(ctx *context.Context) {
 	migratingTask, err := admin_model.GetMigratingTask(ctx.Repo.Repository.ID)
 	if err != nil {
diff --git a/routers/web/web.go b/routers/web/web.go
index ca75bd5967..aa3d830f94 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -953,7 +953,11 @@ func registerRoutes(m *web.Route) {
 				addSettingsSecretsRoutes()
 				addSettingVariablesRoutes()
 			}, actions.MustEnableActions)
-			m.Post("/migrate/cancel", repo.MigrateCancelPost) // this handler must be under "settings", otherwise this incomplete repo can't be accessed
+			// the follow handler must be under "settings", otherwise this incomplete repo can't be accessed
+			m.Group("/migrate", func() {
+				m.Post("/retry", repo.MigrateRetryPost)
+				m.Post("/cancel", repo.MigrateCancelPost)
+			})
 		}, ctxDataSet("PageIsRepoSettings", true, "LFSStartServer", setting.LFS.StartServer))
 	}, reqSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoAdmin, context.RepoRef())
 
diff --git a/services/task/task.go b/services/task/task.go
index 11a47a68bb..db5c1dd3f8 100644
--- a/services/task/task.go
+++ b/services/task/task.go
@@ -126,3 +126,27 @@ func CreateMigrateTask(doer, u *user_model.User, opts base.MigrateOptions) (*adm
 
 	return task, nil
 }
+
+// RetryMigrateTask retry a migrate task
+func RetryMigrateTask(repoID int64) error {
+	migratingTask, err := admin_model.GetMigratingTask(repoID)
+	if err != nil {
+		log.Error("GetMigratingTask: %v", err)
+		return err
+	}
+	if migratingTask.Status == structs.TaskStatusQueued || migratingTask.Status == structs.TaskStatusRunning {
+		return nil
+	}
+
+	// TODO Need to removing the storage/database garbage brought by the failed task
+
+	// Reset task status and messages
+	migratingTask.Status = structs.TaskStatusQueued
+	migratingTask.Message = ""
+	if err = migratingTask.UpdateCols("status", "message"); err != nil {
+		log.Error("task.UpdateCols failed: %v", err)
+		return err
+	}
+
+	return taskQueue.Push(migratingTask)
+}
diff --git a/templates/repo/migrate/migrating.tmpl b/templates/repo/migrate/migrating.tmpl
index 50bff0a937..d1bdc5a90f 100644
--- a/templates/repo/migrate/migrating.tmpl
+++ b/templates/repo/migrate/migrating.tmpl
@@ -36,10 +36,11 @@
 								<div class="divider"></div>
 								<div class="item">
 									{{if .Failed}}
-									<button class="ui basic red show-modal button" data-modal="#delete-repo-modal">{{.locale.Tr "repo.settings.delete"}}</button>
+										<button class="ui basic red show-modal button" data-modal="#delete-repo-modal">{{.locale.Tr "repo.settings.delete"}}</button>
 									{{else}}
-									<button class="ui basic red show-modal button" data-modal="#cancel-repo-modal">{{.locale.Tr "cancel"}}</button>
+										<button class="ui basic red show-modal button" data-modal="#cancel-repo-modal">{{.locale.Tr "cancel"}}</button>
 									{{end}}
+									<button id="repo_migrating_retry" data-migrating-task-retry-url="{{.Link}}/settings/migrate/retry" class="ui basic button gt-hidden">{{.locale.Tr "retry"}}</button>
 								</div>
 							{{end}}
 						</div>
diff --git a/web_src/js/features/repo-migrate.js b/web_src/js/features/repo-migrate.js
index e57348d31b..de9f7b023c 100644
--- a/web_src/js/features/repo-migrate.js
+++ b/web_src/js/features/repo-migrate.js
@@ -1,12 +1,14 @@
 import $ from 'jquery';
 import {hideElem, showElem} from '../utils/dom.js';
 
-const {appSubUrl} = window.config;
+const {appSubUrl, csrfToken} = window.config;
 
 export function initRepoMigrationStatusChecker() {
   const $repoMigrating = $('#repo_migrating');
   if (!$repoMigrating.length) return;
 
+  $('#repo_migrating_retry').on('click', doMigrationRetry);
+
   const task = $repoMigrating.attr('data-migrating-task-id');
 
   // returns true if the refresh still need to be called after a while
@@ -31,6 +33,7 @@ export function initRepoMigrationStatusChecker() {
     if (data.status === 3) {
       hideElem('#repo_migrating_progress');
       hideElem('#repo_migrating');
+      showElem('#repo_migrating_retry');
       showElem('#repo_migrating_failed');
       showElem('#repo_migrating_failed_image');
       $('#repo_migrating_failed_error').text(data.message);
@@ -53,3 +56,14 @@ export function initRepoMigrationStatusChecker() {
 
   syncTaskStatus(); // no await
 }
+
+async function doMigrationRetry(e) {
+  await fetch($(e.target).attr('data-migrating-task-retry-url'), {
+    method: 'post',
+    headers: {
+      'X-Csrf-Token': csrfToken,
+      'Content-Type': 'application/json',
+    },
+  });
+  window.location.reload();
+}