]> source.dussan.org Git - gitea.git/commitdiff
Fix javascript error when an anonymous user visiting migration page (#32144) (#32179)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 4 Oct 2024 17:58:04 +0000 (01:58 +0800)
committerGitHub <noreply@github.com>
Fri, 4 Oct 2024 17:58:04 +0000 (17:58 +0000)
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 <wxiaoguang@gmail.com>
models/admin/task.go
routers/web/repo/migrate.go
routers/web/user/task.go [deleted file]
routers/web/web.go
services/context/repo.go
templates/repo/migrate/migrating.tmpl
web_src/js/features/repo-migrate.js

index c8bc95f98142520d7d8ae0816a3ab91df154597e..10f8e6d57046d8448a3c882cc9bc7b74409f4d92 100644 (file)
@@ -179,27 +179,6 @@ func GetMigratingTask(ctx context.Context, repoID int64) (*Task, error) {
        return &task, nil
 }
 
-// GetMigratingTaskByID returns the migrating task by repo's id
-func GetMigratingTaskByID(ctx context.Context, id, doerID int64) (*Task, *migration.MigrateOptions, error) {
-       task := Task{
-               ID:     id,
-               DoerID: doerID,
-               Type:   structs.TaskTypeMigrateRepo,
-       }
-       has, err := db.GetEngine(ctx).Get(&task)
-       if err != nil {
-               return nil, nil, err
-       } else if !has {
-               return nil, nil, ErrTaskDoesNotExist{id, 0, task.Type}
-       }
-
-       var opts migration.MigrateOptions
-       if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil {
-               return nil, nil, err
-       }
-       return &task, &opts, nil
-}
-
 // CreateTask creates a task on database
 func CreateTask(ctx context.Context, task *Task) error {
        return db.Insert(ctx, task)
index 97b0c425ea3befe6c8a25c439c8d572f344af57a..97a5da15eed448b2ba953b844ed4807c17efbc3a 100644 (file)
@@ -15,6 +15,7 @@ import (
        repo_model "code.gitea.io/gitea/models/repo"
        user_model "code.gitea.io/gitea/models/user"
        "code.gitea.io/gitea/modules/base"
+       "code.gitea.io/gitea/modules/json"
        "code.gitea.io/gitea/modules/lfs"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
@@ -284,3 +285,40 @@ func MigrateCancelPost(ctx *context.Context) {
        }
        ctx.Redirect(ctx.Repo.Repository.Link())
 }
+
+// MigrateStatus returns migrate task's status
+func MigrateStatus(ctx *context.Context) {
+       task, err := admin_model.GetMigratingTask(ctx, ctx.Repo.Repository.ID)
+       if err != nil {
+               if admin_model.IsErrTaskDoesNotExist(err) {
+                       ctx.JSON(http.StatusNotFound, map[string]any{
+                               "err": "task does not exist or you do not have access to this task",
+                       })
+                       return
+               }
+               log.Error("GetMigratingTask: %v", err)
+               ctx.JSON(http.StatusInternalServerError, map[string]any{
+                       "err": http.StatusText(http.StatusInternalServerError),
+               })
+               return
+       }
+
+       message := task.Message
+
+       if task.Message != "" && task.Message[0] == '{' {
+               // assume message is actually a translatable string
+               var translatableMessage admin_model.TranslatableMessage
+               if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil {
+                       translatableMessage = admin_model.TranslatableMessage{
+                               Format: "migrate.migrating_failed.error",
+                               Args:   []any{task.Message},
+                       }
+               }
+               message = ctx.Locale.TrString(translatableMessage.Format, translatableMessage.Args...)
+       }
+
+       ctx.JSON(http.StatusOK, map[string]any{
+               "status":  task.Status,
+               "message": message,
+       })
+}
diff --git a/routers/web/user/task.go b/routers/web/user/task.go
deleted file mode 100644 (file)
index 8476767..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2020 The Gitea Authors. All rights reserved.
-// SPDX-License-Identifier: MIT
-
-package user
-
-import (
-       "net/http"
-       "strconv"
-
-       admin_model "code.gitea.io/gitea/models/admin"
-       "code.gitea.io/gitea/modules/json"
-       "code.gitea.io/gitea/services/context"
-)
-
-// TaskStatus returns task's status
-func TaskStatus(ctx *context.Context) {
-       task, opts, err := admin_model.GetMigratingTaskByID(ctx, ctx.ParamsInt64("task"), ctx.Doer.ID)
-       if err != nil {
-               if admin_model.IsErrTaskDoesNotExist(err) {
-                       ctx.JSON(http.StatusNotFound, map[string]any{
-                               "error": "task `" + strconv.FormatInt(ctx.ParamsInt64("task"), 10) + "` does not exist",
-                       })
-                       return
-               }
-               ctx.JSON(http.StatusInternalServerError, map[string]any{
-                       "err": err,
-               })
-               return
-       }
-
-       message := task.Message
-
-       if task.Message != "" && task.Message[0] == '{' {
-               // assume message is actually a translatable string
-               var translatableMessage admin_model.TranslatableMessage
-               if err := json.Unmarshal([]byte(message), &translatableMessage); err != nil {
-                       translatableMessage = admin_model.TranslatableMessage{
-                               Format: "migrate.migrating_failed.error",
-                               Args:   []any{task.Message},
-                       }
-               }
-               message = ctx.Locale.TrString(translatableMessage.Format, translatableMessage.Args...)
-       }
-
-       ctx.JSON(http.StatusOK, map[string]any{
-               "status":    task.Status,
-               "message":   message,
-               "repo-id":   task.RepoID,
-               "repo-name": opts.RepoName,
-               "start":     task.StartTime,
-               "end":       task.EndTime,
-       })
-}
index 328be992d41189d22d2b79bed5ee6037f7dbd05b..5e19d1fd131c8a5cc5d01eb106c2326009607f79 100644 (file)
@@ -667,7 +667,6 @@ func registerRoutes(m *web.Route) {
                m.Get("/forgot_password", auth.ForgotPasswd)
                m.Post("/forgot_password", auth.ForgotPasswdPost)
                m.Post("/logout", auth.SignOut)
-               m.Get("/task/{task}", reqSignIn, user.TaskStatus)
                m.Get("/stopwatches", reqSignIn, user.GetStopwatches)
                m.Get("/search", ignExploreSignIn, user.Search)
                m.Group("/oauth2", func() {
@@ -1036,6 +1035,13 @@ func registerRoutes(m *web.Route) {
        }, ignSignIn, context.UserAssignmentWeb(), context.OrgAssignment())
        // end "/{username}/-": packages, projects, code
 
+       m.Group("/{username}/{reponame}/-", func() {
+               m.Group("/migrate", func() {
+                       m.Get("/status", repo.MigrateStatus)
+               })
+       }, ignSignIn, context.RepoAssignment, reqRepoCodeReader)
+       // end "/{username}/{reponame}/-": migrate
+
        m.Group("/{username}/{reponame}/settings", func() {
                m.Group("", func() {
                        m.Combo("").Get(repo_setting.Settings).
index 4836c1456c8c500f5d7ceecf68a62499dcfd8480..c8005cfc72fe575444b53fbd6f1d24135c1d356b 100644 (file)
@@ -607,7 +607,10 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
                }
        }
 
-       isHomeOrSettings := ctx.Link == ctx.Repo.RepoLink || ctx.Link == ctx.Repo.RepoLink+"/settings" || strings.HasPrefix(ctx.Link, ctx.Repo.RepoLink+"/settings/")
+       isHomeOrSettings := ctx.Link == ctx.Repo.RepoLink ||
+               ctx.Link == ctx.Repo.RepoLink+"/settings" ||
+               strings.HasPrefix(ctx.Link, ctx.Repo.RepoLink+"/settings/") ||
+               ctx.Link == ctx.Repo.RepoLink+"/-/migrate/status"
 
        // Disable everything when the repo is being created
        if ctx.Repo.Repository.IsBeingCreated() || ctx.Repo.Repository.IsBroken() {
index ed03db2ebd7ab83a5122395a343d1880be9f1952..bc07b488f24272a2239f12dc10c0809f2b07a8ad 100644 (file)
@@ -7,7 +7,7 @@
                                {{template "base/alert" .}}
                                <div class="home">
                                        <div class="ui stackable middle very relaxed page grid">
-                                               <div id="repo_migrating" class="sixteen wide center aligned centered column" data-migrating-task-id="{{.MigrateTask.ID}}">
+                                               <div id="repo_migrating" class="sixteen wide center aligned centered column" data-migrating-repo-link="{{.Link}}">
                                                        <div>
                                                                <img src="{{AssetUrlPrefix}}/img/loading.png">
                                                        </div>
index 490e7df0e43adda17b851a817c0db09395faa8b6..d6a7eeb1253a401e40e7b4d242f79c804e425cee 100644 (file)
@@ -1,19 +1,17 @@
 import {hideElem, showElem} from '../utils/dom.js';
 import {GET, POST} from '../modules/fetch.js';
 
-const {appSubUrl} = window.config;
-
 export function initRepoMigrationStatusChecker() {
   const repoMigrating = document.getElementById('repo_migrating');
   if (!repoMigrating) return;
 
-  document.getElementById('repo_migrating_retry').addEventListener('click', doMigrationRetry);
+  document.getElementById('repo_migrating_retry')?.addEventListener('click', doMigrationRetry);
 
-  const task = repoMigrating.getAttribute('data-migrating-task-id');
+  const repoLink = repoMigrating.getAttribute('data-migrating-repo-link');
 
   // returns true if the refresh still needs to be called after a while
   const refresh = async () => {
-    const res = await GET(`${appSubUrl}/user/task/${task}`);
+    const res = await GET(`${repoLink}/-/migrate/status`);
     if (res.status !== 200) return true; // continue to refresh if network error occurs
 
     const data = await res.json();