Skip to content

Commit 2e3a191

Browse files
lunnywxiaoguang
andauthored
Fix javascript error when an anonymous user visiting migration page (#32144) (#32179)
backport #32144 This PR fixes javascript errors when an anonymous user visits the migration page. It also makes task view checking more restrictive. The router moved from `/user/task/{id}/status` to `/username/reponame/-/migrate/status` because it's a migrate status. Co-authored-by: wxiaoguang <[email protected]>
1 parent 361221c commit 2e3a191

File tree

7 files changed

+53
-82
lines changed

7 files changed

+53
-82
lines changed

models/admin/task.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,6 @@ func GetMigratingTask(ctx context.Context, repoID int64) (*Task, error) {
179179
return &task, nil
180180
}
181181

182-
// GetMigratingTaskByID returns the migrating task by repo's id
183-
func GetMigratingTaskByID(ctx context.Context, id, doerID int64) (*Task, *migration.MigrateOptions, error) {
184-
task := Task{
185-
ID: id,
186-
DoerID: doerID,
187-
Type: structs.TaskTypeMigrateRepo,
188-
}
189-
has, err := db.GetEngine(ctx).Get(&task)
190-
if err != nil {
191-
return nil, nil, err
192-
} else if !has {
193-
return nil, nil, ErrTaskDoesNotExist{id, 0, task.Type}
194-
}
195-
196-
var opts migration.MigrateOptions
197-
if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil {
198-
return nil, nil, err
199-
}
200-
return &task, &opts, nil
201-
}
202-
203182
// CreateTask creates a task on database
204183
func CreateTask(ctx context.Context, task *Task) error {
205184
return db.Insert(ctx, task)

routers/web/repo/migrate.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
repo_model "code.gitea.io/gitea/models/repo"
1616
user_model "code.gitea.io/gitea/models/user"
1717
"code.gitea.io/gitea/modules/base"
18+
"code.gitea.io/gitea/modules/json"
1819
"code.gitea.io/gitea/modules/lfs"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/setting"
@@ -284,3 +285,40 @@ func MigrateCancelPost(ctx *context.Context) {
284285
}
285286
ctx.Redirect(ctx.Repo.Repository.Link())
286287
}
288+
289+
// MigrateStatus returns migrate task's status
290+
func MigrateStatus(ctx *context.Context) {
291+
task, err := admin_model.GetMigratingTask(ctx, ctx.Repo.Repository.ID)
292+
if err != nil {
293+
if admin_model.IsErrTaskDoesNotExist(err) {
294+
ctx.JSON(http.StatusNotFound, map[string]any{
295+
"err": "task does not exist or you do not have access to this task",
296+
})
297+
return
298+
}
299+
log.Error("GetMigratingTask: %v", err)
300+
ctx.JSON(http.StatusInternalServerError, map[string]any{
301+
"err": http.StatusText(http.StatusInternalServerError),
302+
})
303+
return
304+
}
305+
306+
message := task.Message
307+
308+
if task.Message != "" && task.Message[0] == '{' {
309+
// assume message is actually a translatable string
310+
var translatableMessage admin_model.TranslatableMessage
311+
if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil {
312+
translatableMessage = admin_model.TranslatableMessage{
313+
Format: "migrate.migrating_failed.error",
314+
Args: []any{task.Message},
315+
}
316+
}
317+
message = ctx.Locale.TrString(translatableMessage.Format, translatableMessage.Args...)
318+
}
319+
320+
ctx.JSON(http.StatusOK, map[string]any{
321+
"status": task.Status,
322+
"message": message,
323+
})
324+
}

routers/web/user/task.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

routers/web/web.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,6 @@ func registerRoutes(m *web.Route) {
667667
m.Get("/forgot_password", auth.ForgotPasswd)
668668
m.Post("/forgot_password", auth.ForgotPasswdPost)
669669
m.Post("/logout", auth.SignOut)
670-
m.Get("/task/{task}", reqSignIn, user.TaskStatus)
671670
m.Get("/stopwatches", reqSignIn, user.GetStopwatches)
672671
m.Get("/search", ignExploreSignIn, user.Search)
673672
m.Group("/oauth2", func() {
@@ -1036,6 +1035,13 @@ func registerRoutes(m *web.Route) {
10361035
}, ignSignIn, context.UserAssignmentWeb(), context.OrgAssignment())
10371036
// end "/{username}/-": packages, projects, code
10381037

1038+
m.Group("/{username}/{reponame}/-", func() {
1039+
m.Group("/migrate", func() {
1040+
m.Get("/status", repo.MigrateStatus)
1041+
})
1042+
}, ignSignIn, context.RepoAssignment, reqRepoCodeReader)
1043+
// end "/{username}/{reponame}/-": migrate
1044+
10391045
m.Group("/{username}/{reponame}/settings", func() {
10401046
m.Group("", func() {
10411047
m.Combo("").Get(repo_setting.Settings).

services/context/repo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,10 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
607607
}
608608
}
609609

610-
isHomeOrSettings := ctx.Link == ctx.Repo.RepoLink || ctx.Link == ctx.Repo.RepoLink+"/settings" || strings.HasPrefix(ctx.Link, ctx.Repo.RepoLink+"/settings/")
610+
isHomeOrSettings := ctx.Link == ctx.Repo.RepoLink ||
611+
ctx.Link == ctx.Repo.RepoLink+"/settings" ||
612+
strings.HasPrefix(ctx.Link, ctx.Repo.RepoLink+"/settings/") ||
613+
ctx.Link == ctx.Repo.RepoLink+"/-/migrate/status"
611614

612615
// Disable everything when the repo is being created
613616
if ctx.Repo.Repository.IsBeingCreated() || ctx.Repo.Repository.IsBroken() {

templates/repo/migrate/migrating.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{template "base/alert" .}}
88
<div class="home">
99
<div class="ui stackable middle very relaxed page grid">
10-
<div id="repo_migrating" class="sixteen wide center aligned centered column" data-migrating-task-id="{{.MigrateTask.ID}}">
10+
<div id="repo_migrating" class="sixteen wide center aligned centered column" data-migrating-repo-link="{{.Link}}">
1111
<div>
1212
<img src="{{AssetUrlPrefix}}/img/loading.png">
1313
</div>

web_src/js/features/repo-migrate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import {hideElem, showElem} from '../utils/dom.js';
22
import {GET, POST} from '../modules/fetch.js';
33

4-
const {appSubUrl} = window.config;
5-
64
export function initRepoMigrationStatusChecker() {
75
const repoMigrating = document.getElementById('repo_migrating');
86
if (!repoMigrating) return;
97

10-
document.getElementById('repo_migrating_retry').addEventListener('click', doMigrationRetry);
8+
document.getElementById('repo_migrating_retry')?.addEventListener('click', doMigrationRetry);
119

12-
const task = repoMigrating.getAttribute('data-migrating-task-id');
10+
const repoLink = repoMigrating.getAttribute('data-migrating-repo-link');
1311

1412
// returns true if the refresh still needs to be called after a while
1513
const refresh = async () => {
16-
const res = await GET(`${appSubUrl}/user/task/${task}`);
14+
const res = await GET(`${repoLink}/-/migrate/status`);
1715
if (res.status !== 200) return true; // continue to refresh if network error occurs
1816

1917
const data = await res.json();

0 commit comments

Comments
 (0)