From dcd3a631288686a95cedbd4aa9cce245e896825d Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 26 Jul 2023 14:04:01 +0800 Subject: [PATCH] Move web JSON functions to web context and simplify code (#26132) The JSONRedirect/JSONOK/JSONError functions were put into "Base" context incorrectly, it would cause abuse. Actually, they are for "web context" only, so, move them to the correct place. And by the way, use them to simplify old code: +75 -196 --- modules/context/base.go | 12 ------- modules/context/context.go | 12 +++++++ routers/web/admin/auths.go | 8 ++--- routers/web/admin/config.go | 4 +-- routers/web/admin/hooks.go | 4 +-- routers/web/admin/packages.go | 4 +-- routers/web/admin/repos.go | 4 +-- routers/web/admin/stacktrace.go | 4 +-- routers/web/auth/webauthn.go | 2 +- routers/web/org/members.go | 12 ++----- routers/web/org/org_labels.go | 4 +-- routers/web/org/projects.go | 32 +++++-------------- routers/web/org/setting.go | 4 +-- routers/web/org/teams.go | 8 ++--- routers/web/repo/branch.go | 4 +-- routers/web/repo/issue.go | 12 ++----- routers/web/repo/issue_label.go | 8 ++--- routers/web/repo/milestone.go | 4 +-- routers/web/repo/projects.go | 32 +++++-------------- routers/web/repo/pull.go | 4 +-- routers/web/repo/pull_review.go | 4 +-- routers/web/repo/release.go | 8 ++--- routers/web/repo/setting/collaboration.go | 8 ++--- routers/web/repo/setting/deploy_key.go | 4 +-- routers/web/repo/setting/protected_branch.go | 20 +++--------- routers/web/repo/setting/webhook.go | 4 +-- routers/web/repo/wiki.go | 4 +-- routers/web/shared/actions/runners.go | 9 ++---- routers/web/user/setting/account.go | 4 +-- routers/web/user/setting/applications.go | 4 +-- routers/web/user/setting/keys.go | 4 +-- routers/web/user/setting/oauth2_common.go | 4 +-- routers/web/user/setting/security/openid.go | 4 +-- routers/web/user/setting/security/security.go | 4 +-- routers/web/user/setting/security/webauthn.go | 4 +-- routers/web/user/setting/webhooks.go | 4 +-- 36 files changed, 75 insertions(+), 196 deletions(-) diff --git a/modules/context/base.go b/modules/context/base.go index 8566ef7861..83e5a214f8 100644 --- a/modules/context/base.go +++ b/modules/context/base.go @@ -136,18 +136,6 @@ func (b *Base) JSON(status int, content any) { } } -func (b *Base) JSONRedirect(redirect string) { - b.JSON(http.StatusOK, map[string]any{"redirect": redirect}) -} - -func (b *Base) JSONOK() { - b.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it -} - -func (b *Base) JSONError(msg string) { - b.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg}) -} - // RemoteAddr returns the client machine ip address func (b *Base) RemoteAddr() string { return b.Req.RemoteAddr diff --git a/modules/context/context.go b/modules/context/context.go index 47a04c989c..de0518a1d2 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -226,3 +226,15 @@ func (ctx *Context) GetErrMsg() string { } return msg } + +func (ctx *Context) JSONRedirect(redirect string) { + ctx.JSON(http.StatusOK, map[string]any{"redirect": redirect}) +} + +func (ctx *Context) JSONOK() { + ctx.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it +} + +func (ctx *Context) JSONError(msg string) { + ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg}) +} diff --git a/routers/web/admin/auths.go b/routers/web/admin/auths.go index adde26f0b5..b743d1b0a5 100644 --- a/routers/web/admin/auths.go +++ b/routers/web/admin/auths.go @@ -454,15 +454,11 @@ func DeleteAuthSource(ctx *context.Context) { } else { ctx.Flash.Error(fmt.Sprintf("auth_service.DeleteSource: %v", err)) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/auths/" + url.PathEscape(ctx.Params(":authid")), - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/auths/" + url.PathEscape(ctx.Params(":authid"))) return } log.Trace("Authentication deleted by admin(%s): %d", ctx.Doer.Name, source.ID) ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/auths", - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/auths") } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index ba0d862645..c70a2d1c95 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -179,9 +179,7 @@ func Config(ctx *context.Context) { func ChangeConfig(ctx *context.Context) { key := strings.TrimSpace(ctx.FormString("key")) if key == "" { - ctx.JSON(http.StatusOK, map[string]string{ - "redirect": ctx.Req.URL.String(), - }) + ctx.JSONRedirect(ctx.Req.URL.String()) return } value := ctx.FormString("value") diff --git a/routers/web/admin/hooks.go b/routers/web/admin/hooks.go index 2e4122c904..cd8cc29cdf 100644 --- a/routers/web/admin/hooks.go +++ b/routers/web/admin/hooks.go @@ -67,7 +67,5 @@ func DeleteDefaultOrSystemWebhook(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/hooks", - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/hooks") } diff --git a/routers/web/admin/packages.go b/routers/web/admin/packages.go index 731a1d6ac2..8e4b8a373e 100644 --- a/routers/web/admin/packages.go +++ b/routers/web/admin/packages.go @@ -97,7 +97,5 @@ func DeletePackageVersion(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("packages.settings.delete.success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type")), - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type"))) } diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go index 2ea8a2ad35..d1d0abca02 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -58,9 +58,7 @@ func DeleteRepo(ctx *context.Context) { log.Trace("Repository deleted: %s", repo.FullName()) ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/repos?page=" + url.QueryEscape(ctx.FormString("page")) + "&sort=" + url.QueryEscape(ctx.FormString("sort")), - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/repos?page=" + url.QueryEscape(ctx.FormString("page")) + "&sort=" + url.QueryEscape(ctx.FormString("sort"))) } // UnadoptedRepos lists the unadopted repositories diff --git a/routers/web/admin/stacktrace.go b/routers/web/admin/stacktrace.go index f2d2be481a..b603fb59a2 100644 --- a/routers/web/admin/stacktrace.go +++ b/routers/web/admin/stacktrace.go @@ -42,7 +42,5 @@ func Stacktrace(ctx *context.Context) { func StacktraceCancel(ctx *context.Context) { pid := ctx.Params("pid") process.GetManager().Cancel(process.IDType(pid)) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/admin/monitor/stacktrace", - }) + ctx.JSONRedirect(setting.AppSubURL + "/admin/monitor/stacktrace") } diff --git a/routers/web/auth/webauthn.go b/routers/web/auth/webauthn.go index e369f86081..013e11eacc 100644 --- a/routers/web/auth/webauthn.go +++ b/routers/web/auth/webauthn.go @@ -154,5 +154,5 @@ func WebAuthnLoginAssertionPost(ctx *context.Context) { } _ = ctx.Session.Delete("twofaUid") - ctx.JSON(http.StatusOK, map[string]string{"redirect": redirect}) + ctx.JSONRedirect(redirect) } diff --git a/routers/web/org/members.go b/routers/web/org/members.go index 8da0f0b9fd..fae8b48128 100644 --- a/routers/web/org/members.go +++ b/routers/web/org/members.go @@ -101,9 +101,7 @@ func MembersAction(ctx *context.Context) { err = models.RemoveOrgUser(org.ID, uid) if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/members", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/members") return } case "leave": @@ -115,9 +113,7 @@ func MembersAction(ctx *context.Context) { }) } else if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/members", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/members") } else { log.Error("RemoveOrgUser(%d,%d): %v", org.ID, ctx.Doer.ID, err) } @@ -138,7 +134,5 @@ func MembersAction(ctx *context.Context) { redirect = setting.AppSubURL + "/" } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": redirect, - }) + ctx.JSONRedirect(redirect) } diff --git a/routers/web/org/org_labels.go b/routers/web/org/org_labels.go index 08566637a8..a9f9e963d4 100644 --- a/routers/web/org/org_labels.go +++ b/routers/web/org/org_labels.go @@ -90,9 +90,7 @@ func DeleteLabel(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/settings/labels", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/settings/labels") } // InitializeLabels init labels for an organization diff --git a/routers/web/org/projects.go b/routers/web/org/projects.go index 50bb5591e5..ea6e7dff48 100644 --- a/routers/web/org/projects.go +++ b/routers/web/org/projects.go @@ -219,9 +219,7 @@ func DeleteProject(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.ContextUser.HomeLink() + "/-/projects", - }) + ctx.JSONRedirect(ctx.ContextUser.HomeLink() + "/-/projects") } // RenderEditProject allows a project to be edited @@ -449,9 +447,7 @@ func UpdateIssueProject(ctx *context.Context) { } } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // DeleteProjectBoard allows for the deletion of a project board @@ -497,9 +493,7 @@ func DeleteProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // AddBoardToProjectPost allows a new board to be added to a project. @@ -526,9 +520,7 @@ func AddBoardToProjectPost(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // CheckProjectBoardChangePermissions check permission @@ -594,9 +586,7 @@ func EditProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // SetDefaultProjectBoard set default board for uncategorized issues/pulls @@ -611,9 +601,7 @@ func SetDefaultProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // UnsetDefaultProjectBoard unset default board for uncategorized issues/pulls @@ -628,9 +616,7 @@ func UnsetDefaultProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // MoveIssues moves or keeps issues in a column and sorts them inside that column @@ -730,7 +716,5 @@ func MoveIssues(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go index f63b2e9f06..5ae61c79be 100644 --- a/routers/web/org/setting.go +++ b/routers/web/org/setting.go @@ -219,9 +219,7 @@ func DeleteWebhook(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/settings/hooks", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/settings/hooks") } // Labels render organization labels page diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index aefadaf809..196d3e9bf0 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -256,9 +256,7 @@ func TeamsRepoAction(ctx *context.Context) { } if action == "addall" || action == "removeall" { - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories") return } ctx.Redirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories") @@ -530,9 +528,7 @@ func DeleteTeam(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Org.OrgLink + "/teams", - }) + ctx.JSONRedirect(ctx.Org.OrgLink + "/teams") } // TeamInvite renders the team invite page diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 999104d787..d71d555bc2 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -162,9 +162,7 @@ func RestoreBranchPost(ctx *context.Context) { } func redirect(ctx *context.Context) { - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")), - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page"))) } // CreateBranch creates new branch in repository diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index b7d159d158..f5cec5a323 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2221,9 +2221,7 @@ func UpdateIssueMilestone(ctx *context.Context) { } } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // UpdateIssueAssignee change issue's or pull's assignee @@ -2267,9 +2265,7 @@ func UpdateIssueAssignee(ctx *context.Context) { } } } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // UpdatePullReviewRequest add or remove review request @@ -2392,9 +2388,7 @@ func UpdatePullReviewRequest(ctx *context.Context) { } } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // SearchIssues searches for issues across the repositories that the user has access to diff --git a/routers/web/repo/issue_label.go b/routers/web/repo/issue_label.go index af5db83bd5..5d326bab58 100644 --- a/routers/web/repo/issue_label.go +++ b/routers/web/repo/issue_label.go @@ -157,9 +157,7 @@ func DeleteLabel(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/labels", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/labels") } // UpdateIssueLabel change issue's labels @@ -226,7 +224,5 @@ func UpdateIssueLabel(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index 7b20cd9848..ad355ce5d7 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -255,9 +255,7 @@ func DeleteMilestone(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/milestones", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/milestones") } // MilestoneIssuesAndPulls lists all the issues and pull requests of the milestone diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index 1574c90c02..b8662e0605 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -203,9 +203,7 @@ func DeleteProject(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/projects", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/projects") } // RenderEditProject allows a project to be edited @@ -397,9 +395,7 @@ func UpdateIssueProject(ctx *context.Context) { } } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // DeleteProjectBoard allows for the deletion of a project board @@ -452,9 +448,7 @@ func DeleteProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // AddBoardToProjectPost allows a new board to be added to a project. @@ -487,9 +481,7 @@ func AddBoardToProjectPost(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } func checkProjectBoardChangePermissions(ctx *context.Context) (*project_model.Project, *project_model.Board) { @@ -561,9 +553,7 @@ func EditProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // SetDefaultProjectBoard set default board for uncategorized issues/pulls @@ -578,9 +568,7 @@ func SetDefaultProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // UnSetDefaultProjectBoard unset default board for uncategorized issues/pulls @@ -595,9 +583,7 @@ func UnSetDefaultProjectBoard(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } // MoveIssues moves or keeps issues in a column and sorts them inside that column @@ -699,7 +685,5 @@ func MoveIssues(ctx *context.Context) { return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 5290e25d46..237e53413f 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1423,9 +1423,7 @@ func CleanUpPullRequest(ctx *context.Context) { } defer func() { - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": issue.Link(), - }) + ctx.JSONRedirect(issue.Link()) }() // Check if branch has no new commits diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index f7c962d1ae..c2271750c4 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -156,9 +156,7 @@ func UpdateResolveConversation(ctx *context.Context) { renderConversation(ctx, comment) return } - ctx.JSON(http.StatusOK, map[string]any{ - "ok": true, - }) + ctx.JSONOK() } func renderConversation(ctx *context.Context, comment *issues_model.Comment) { diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 3d991384e5..957cf56972 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -628,13 +628,9 @@ func deleteReleaseOrTag(ctx *context.Context, isDelTag bool) { } if isDelTag { - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/tags", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/tags") return } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/releases", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/releases") } diff --git a/routers/web/repo/setting/collaboration.go b/routers/web/repo/setting/collaboration.go index 8f2d306862..b708422cbd 100644 --- a/routers/web/repo/setting/collaboration.go +++ b/routers/web/repo/setting/collaboration.go @@ -133,9 +133,7 @@ func DeleteCollaboration(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/settings/collaboration", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/collaboration") } // AddTeamPost response for adding a team to a repository @@ -204,7 +202,5 @@ func DeleteTeam(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("repo.settings.remove_team_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/settings/collaboration", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/collaboration") } diff --git a/routers/web/repo/setting/deploy_key.go b/routers/web/repo/setting/deploy_key.go index d08c51f5e5..577706d454 100644 --- a/routers/web/repo/setting/deploy_key.go +++ b/routers/web/repo/setting/deploy_key.go @@ -105,7 +105,5 @@ func DeleteDeployKey(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/settings/keys", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/keys") } diff --git a/routers/web/repo/setting/protected_branch.go b/routers/web/repo/setting/protected_branch.go index cf59e747d8..5bfdb8f515 100644 --- a/routers/web/repo/setting/protected_branch.go +++ b/routers/web/repo/setting/protected_branch.go @@ -318,41 +318,31 @@ func DeleteProtectedBranchRulePost(ctx *context.Context) { ruleID := ctx.ParamsInt64("id") if ruleID <= 0 { ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID))) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink), - }) + ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink)) return } rule, err := git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, ruleID) if err != nil { ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID))) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink), - }) + ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink)) return } if rule == nil { ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", fmt.Sprintf("%d", ruleID))) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink), - }) + ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink)) return } if err := git_model.DeleteProtectedBranch(ctx, ctx.Repo.Repository.ID, ruleID); err != nil { ctx.Flash.Error(ctx.Tr("repo.settings.remove_protected_branch_failed", rule.RuleName)) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink), - }) + ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink)) return } ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", rule.RuleName)) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink), - }) + ctx.JSONRedirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink)) } // RenameBranchPost responses for rename a branch diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index d85d5c8b07..5c4e1d47d0 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -729,7 +729,5 @@ func DeleteWebhook(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/settings/hooks", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/hooks") } diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 4773e25c70..e3c187c33b 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -790,7 +790,5 @@ func DeleteWikiPagePost(ctx *context.Context) { notification.NotifyDeleteWikiPage(ctx, ctx.Doer, ctx.Repo.Repository, string(wikiName)) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": ctx.Repo.RepoLink + "/wiki/", - }) + ctx.JSONRedirect(ctx.Repo.RepoLink + "/wiki/") } diff --git a/routers/web/shared/actions/runners.go b/routers/web/shared/actions/runners.go index 21e5a90d8f..eb84cc4a2e 100644 --- a/routers/web/shared/actions/runners.go +++ b/routers/web/shared/actions/runners.go @@ -5,7 +5,6 @@ package actions import ( "errors" - "net/http" actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" @@ -160,9 +159,7 @@ func RunnerDeletePost(ctx *context.Context, runnerID int64, log.Warn("DeleteRunnerPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL) ctx.Flash.Warning(ctx.Tr("actions.runners.delete_runner_failed")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": failedRedirectTo, - }) + ctx.JSONRedirect(failedRedirectTo) return } @@ -170,7 +167,5 @@ func RunnerDeletePost(ctx *context.Context, runnerID int64, ctx.Flash.Success(ctx.Tr("actions.runners.delete_runner_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": successRedirectTo, - }) + ctx.JSONRedirect(successRedirectTo) } diff --git a/routers/web/user/setting/account.go b/routers/web/user/setting/account.go index 532f0d8e39..ecb846e91b 100644 --- a/routers/web/user/setting/account.go +++ b/routers/web/user/setting/account.go @@ -227,9 +227,7 @@ func DeleteEmail(ctx *context.Context) { log.Trace("Email address deleted: %s", ctx.Doer.Name) ctx.Flash.Success(ctx.Tr("settings.email_deletion_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/account", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/account") } // DeleteAccount render user suicide page and response for delete user himself diff --git a/routers/web/user/setting/applications.go b/routers/web/user/setting/applications.go index 8120937696..088aba38b6 100644 --- a/routers/web/user/setting/applications.go +++ b/routers/web/user/setting/applications.go @@ -83,9 +83,7 @@ func DeleteApplication(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.delete_token_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/applications", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/applications") } func loadApplicationsData(ctx *context.Context) { diff --git a/routers/web/user/setting/keys.go b/routers/web/user/setting/keys.go index d9412cae7c..2336c04bbe 100644 --- a/routers/web/user/setting/keys.go +++ b/routers/web/user/setting/keys.go @@ -256,9 +256,7 @@ func DeleteKey(ctx *context.Context) { ctx.Flash.Warning("Function not implemented") ctx.Redirect(setting.AppSubURL + "/user/settings/keys") } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/keys", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/keys") } func loadKeysData(ctx *context.Context) { diff --git a/routers/web/user/setting/oauth2_common.go b/routers/web/user/setting/oauth2_common.go index 923ce4b436..641cc1fd9f 100644 --- a/routers/web/user/setting/oauth2_common.go +++ b/routers/web/user/setting/oauth2_common.go @@ -138,7 +138,7 @@ func (oa *OAuth2CommonHandlers) DeleteApp(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success")) - ctx.JSON(http.StatusOK, map[string]any{"redirect": oa.BasePathList}) + ctx.JSONRedirect(oa.BasePathList) } // RevokeGrant revokes the grant @@ -149,5 +149,5 @@ func (oa *OAuth2CommonHandlers) RevokeGrant(ctx *context.Context) { } ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success")) - ctx.JSON(http.StatusOK, map[string]any{"redirect": oa.BasePathList}) + ctx.JSONRedirect(oa.BasePathList) } diff --git a/routers/web/user/setting/security/openid.go b/routers/web/user/setting/security/openid.go index f4133f3916..b5509f570f 100644 --- a/routers/web/user/setting/security/openid.go +++ b/routers/web/user/setting/security/openid.go @@ -112,9 +112,7 @@ func DeleteOpenID(ctx *context.Context) { log.Trace("OpenID address deleted: %s", ctx.Doer.Name) ctx.Flash.Success(ctx.Tr("settings.openid_deletion_success")) - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/security", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/security") } // ToggleOpenIDVisibility response for toggle visibility of user's openid diff --git a/routers/web/user/setting/security/security.go b/routers/web/user/setting/security/security.go index cc5f817a9d..dae9bf950d 100644 --- a/routers/web/user/setting/security/security.go +++ b/routers/web/user/setting/security/security.go @@ -48,9 +48,7 @@ func DeleteAccountLink(ctx *context.Context) { } } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/security", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/security") } func loadSecurityData(ctx *context.Context) { diff --git a/routers/web/user/setting/security/webauthn.go b/routers/web/user/setting/security/webauthn.go index 89ac184a47..990e506d6f 100644 --- a/routers/web/user/setting/security/webauthn.go +++ b/routers/web/user/setting/security/webauthn.go @@ -116,7 +116,5 @@ func WebauthnDelete(ctx *context.Context) { ctx.ServerError("GetWebAuthnCredentialByID", err) return } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/security", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/security") } diff --git a/routers/web/user/setting/webhooks.go b/routers/web/user/setting/webhooks.go index db03d7b1ed..04092461fd 100644 --- a/routers/web/user/setting/webhooks.go +++ b/routers/web/user/setting/webhooks.go @@ -42,7 +42,5 @@ func DeleteWebhook(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) } - ctx.JSON(http.StatusOK, map[string]any{ - "redirect": setting.AppSubURL + "/user/settings/hooks", - }) + ctx.JSONRedirect(setting.AppSubURL + "/user/settings/hooks") }