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
This commit is contained in:
wxiaoguang
2023-07-26 14:04:01 +08:00
committed by GitHub
parent 338d03ce2f
commit dcd3a63128
36 changed files with 75 additions and 196 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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/")
}