Skip to content

Commit dcd3a63

Browse files
authored
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
1 parent 338d03c commit dcd3a63

36 files changed

+75
-196
lines changed

modules/context/base.go

-12
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,6 @@ func (b *Base) JSON(status int, content any) {
136136
}
137137
}
138138

139-
func (b *Base) JSONRedirect(redirect string) {
140-
b.JSON(http.StatusOK, map[string]any{"redirect": redirect})
141-
}
142-
143-
func (b *Base) JSONOK() {
144-
b.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it
145-
}
146-
147-
func (b *Base) JSONError(msg string) {
148-
b.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg})
149-
}
150-
151139
// RemoteAddr returns the client machine ip address
152140
func (b *Base) RemoteAddr() string {
153141
return b.Req.RemoteAddr

modules/context/context.go

+12
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,15 @@ func (ctx *Context) GetErrMsg() string {
226226
}
227227
return msg
228228
}
229+
230+
func (ctx *Context) JSONRedirect(redirect string) {
231+
ctx.JSON(http.StatusOK, map[string]any{"redirect": redirect})
232+
}
233+
234+
func (ctx *Context) JSONOK() {
235+
ctx.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it
236+
}
237+
238+
func (ctx *Context) JSONError(msg string) {
239+
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg})
240+
}

routers/web/admin/auths.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,11 @@ func DeleteAuthSource(ctx *context.Context) {
454454
} else {
455455
ctx.Flash.Error(fmt.Sprintf("auth_service.DeleteSource: %v", err))
456456
}
457-
ctx.JSON(http.StatusOK, map[string]any{
458-
"redirect": setting.AppSubURL + "/admin/auths/" + url.PathEscape(ctx.Params(":authid")),
459-
})
457+
ctx.JSONRedirect(setting.AppSubURL + "/admin/auths/" + url.PathEscape(ctx.Params(":authid")))
460458
return
461459
}
462460
log.Trace("Authentication deleted by admin(%s): %d", ctx.Doer.Name, source.ID)
463461

464462
ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
465-
ctx.JSON(http.StatusOK, map[string]any{
466-
"redirect": setting.AppSubURL + "/admin/auths",
467-
})
463+
ctx.JSONRedirect(setting.AppSubURL + "/admin/auths")
468464
}

routers/web/admin/config.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ func Config(ctx *context.Context) {
179179
func ChangeConfig(ctx *context.Context) {
180180
key := strings.TrimSpace(ctx.FormString("key"))
181181
if key == "" {
182-
ctx.JSON(http.StatusOK, map[string]string{
183-
"redirect": ctx.Req.URL.String(),
184-
})
182+
ctx.JSONRedirect(ctx.Req.URL.String())
185183
return
186184
}
187185
value := ctx.FormString("value")

routers/web/admin/hooks.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,5 @@ func DeleteDefaultOrSystemWebhook(ctx *context.Context) {
6767
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
6868
}
6969

70-
ctx.JSON(http.StatusOK, map[string]any{
71-
"redirect": setting.AppSubURL + "/admin/hooks",
72-
})
70+
ctx.JSONRedirect(setting.AppSubURL + "/admin/hooks")
7371
}

routers/web/admin/packages.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,5 @@ func DeletePackageVersion(ctx *context.Context) {
9797
}
9898

9999
ctx.Flash.Success(ctx.Tr("packages.settings.delete.success"))
100-
ctx.JSON(http.StatusOK, map[string]any{
101-
"redirect": setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type")),
102-
})
100+
ctx.JSONRedirect(setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type")))
103101
}

routers/web/admin/repos.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ func DeleteRepo(ctx *context.Context) {
5858
log.Trace("Repository deleted: %s", repo.FullName())
5959

6060
ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
61-
ctx.JSON(http.StatusOK, map[string]any{
62-
"redirect": setting.AppSubURL + "/admin/repos?page=" + url.QueryEscape(ctx.FormString("page")) + "&sort=" + url.QueryEscape(ctx.FormString("sort")),
63-
})
61+
ctx.JSONRedirect(setting.AppSubURL + "/admin/repos?page=" + url.QueryEscape(ctx.FormString("page")) + "&sort=" + url.QueryEscape(ctx.FormString("sort")))
6462
}
6563

6664
// UnadoptedRepos lists the unadopted repositories

routers/web/admin/stacktrace.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ func Stacktrace(ctx *context.Context) {
4242
func StacktraceCancel(ctx *context.Context) {
4343
pid := ctx.Params("pid")
4444
process.GetManager().Cancel(process.IDType(pid))
45-
ctx.JSON(http.StatusOK, map[string]any{
46-
"redirect": setting.AppSubURL + "/admin/monitor/stacktrace",
47-
})
45+
ctx.JSONRedirect(setting.AppSubURL + "/admin/monitor/stacktrace")
4846
}

routers/web/auth/webauthn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,5 @@ func WebAuthnLoginAssertionPost(ctx *context.Context) {
154154
}
155155
_ = ctx.Session.Delete("twofaUid")
156156

157-
ctx.JSON(http.StatusOK, map[string]string{"redirect": redirect})
157+
ctx.JSONRedirect(redirect)
158158
}

routers/web/org/members.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ func MembersAction(ctx *context.Context) {
101101
err = models.RemoveOrgUser(org.ID, uid)
102102
if organization.IsErrLastOrgOwner(err) {
103103
ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
104-
ctx.JSON(http.StatusOK, map[string]any{
105-
"redirect": ctx.Org.OrgLink + "/members",
106-
})
104+
ctx.JSONRedirect(ctx.Org.OrgLink + "/members")
107105
return
108106
}
109107
case "leave":
@@ -115,9 +113,7 @@ func MembersAction(ctx *context.Context) {
115113
})
116114
} else if organization.IsErrLastOrgOwner(err) {
117115
ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
118-
ctx.JSON(http.StatusOK, map[string]any{
119-
"redirect": ctx.Org.OrgLink + "/members",
120-
})
116+
ctx.JSONRedirect(ctx.Org.OrgLink + "/members")
121117
} else {
122118
log.Error("RemoveOrgUser(%d,%d): %v", org.ID, ctx.Doer.ID, err)
123119
}
@@ -138,7 +134,5 @@ func MembersAction(ctx *context.Context) {
138134
redirect = setting.AppSubURL + "/"
139135
}
140136

141-
ctx.JSON(http.StatusOK, map[string]any{
142-
"redirect": redirect,
143-
})
137+
ctx.JSONRedirect(redirect)
144138
}

routers/web/org/org_labels.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ func DeleteLabel(ctx *context.Context) {
9090
ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
9191
}
9292

93-
ctx.JSON(http.StatusOK, map[string]any{
94-
"redirect": ctx.Org.OrgLink + "/settings/labels",
95-
})
93+
ctx.JSONRedirect(ctx.Org.OrgLink + "/settings/labels")
9694
}
9795

9896
// InitializeLabels init labels for an organization

routers/web/org/projects.go

+8-24
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ func DeleteProject(ctx *context.Context) {
219219
ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success"))
220220
}
221221

222-
ctx.JSON(http.StatusOK, map[string]any{
223-
"redirect": ctx.ContextUser.HomeLink() + "/-/projects",
224-
})
222+
ctx.JSONRedirect(ctx.ContextUser.HomeLink() + "/-/projects")
225223
}
226224

227225
// RenderEditProject allows a project to be edited
@@ -449,9 +447,7 @@ func UpdateIssueProject(ctx *context.Context) {
449447
}
450448
}
451449

452-
ctx.JSON(http.StatusOK, map[string]any{
453-
"ok": true,
454-
})
450+
ctx.JSONOK()
455451
}
456452

457453
// DeleteProjectBoard allows for the deletion of a project board
@@ -497,9 +493,7 @@ func DeleteProjectBoard(ctx *context.Context) {
497493
return
498494
}
499495

500-
ctx.JSON(http.StatusOK, map[string]any{
501-
"ok": true,
502-
})
496+
ctx.JSONOK()
503497
}
504498

505499
// AddBoardToProjectPost allows a new board to be added to a project.
@@ -526,9 +520,7 @@ func AddBoardToProjectPost(ctx *context.Context) {
526520
return
527521
}
528522

529-
ctx.JSON(http.StatusOK, map[string]any{
530-
"ok": true,
531-
})
523+
ctx.JSONOK()
532524
}
533525

534526
// CheckProjectBoardChangePermissions check permission
@@ -594,9 +586,7 @@ func EditProjectBoard(ctx *context.Context) {
594586
return
595587
}
596588

597-
ctx.JSON(http.StatusOK, map[string]any{
598-
"ok": true,
599-
})
589+
ctx.JSONOK()
600590
}
601591

602592
// SetDefaultProjectBoard set default board for uncategorized issues/pulls
@@ -611,9 +601,7 @@ func SetDefaultProjectBoard(ctx *context.Context) {
611601
return
612602
}
613603

614-
ctx.JSON(http.StatusOK, map[string]any{
615-
"ok": true,
616-
})
604+
ctx.JSONOK()
617605
}
618606

619607
// UnsetDefaultProjectBoard unset default board for uncategorized issues/pulls
@@ -628,9 +616,7 @@ func UnsetDefaultProjectBoard(ctx *context.Context) {
628616
return
629617
}
630618

631-
ctx.JSON(http.StatusOK, map[string]any{
632-
"ok": true,
633-
})
619+
ctx.JSONOK()
634620
}
635621

636622
// MoveIssues moves or keeps issues in a column and sorts them inside that column
@@ -730,7 +716,5 @@ func MoveIssues(ctx *context.Context) {
730716
return
731717
}
732718

733-
ctx.JSON(http.StatusOK, map[string]any{
734-
"ok": true,
735-
})
719+
ctx.JSONOK()
736720
}

routers/web/org/setting.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,7 @@ func DeleteWebhook(ctx *context.Context) {
219219
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
220220
}
221221

222-
ctx.JSON(http.StatusOK, map[string]any{
223-
"redirect": ctx.Org.OrgLink + "/settings/hooks",
224-
})
222+
ctx.JSONRedirect(ctx.Org.OrgLink + "/settings/hooks")
225223
}
226224

227225
// Labels render organization labels page

routers/web/org/teams.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ func TeamsRepoAction(ctx *context.Context) {
256256
}
257257

258258
if action == "addall" || action == "removeall" {
259-
ctx.JSON(http.StatusOK, map[string]any{
260-
"redirect": ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories",
261-
})
259+
ctx.JSONRedirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories")
262260
return
263261
}
264262
ctx.Redirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName) + "/repositories")
@@ -530,9 +528,7 @@ func DeleteTeam(ctx *context.Context) {
530528
ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
531529
}
532530

533-
ctx.JSON(http.StatusOK, map[string]any{
534-
"redirect": ctx.Org.OrgLink + "/teams",
535-
})
531+
ctx.JSONRedirect(ctx.Org.OrgLink + "/teams")
536532
}
537533

538534
// TeamInvite renders the team invite page

routers/web/repo/branch.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ func RestoreBranchPost(ctx *context.Context) {
162162
}
163163

164164
func redirect(ctx *context.Context) {
165-
ctx.JSON(http.StatusOK, map[string]any{
166-
"redirect": ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")),
167-
})
165+
ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")))
168166
}
169167

170168
// CreateBranch creates new branch in repository

routers/web/repo/issue.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -2221,9 +2221,7 @@ func UpdateIssueMilestone(ctx *context.Context) {
22212221
}
22222222
}
22232223

2224-
ctx.JSON(http.StatusOK, map[string]any{
2225-
"ok": true,
2226-
})
2224+
ctx.JSONOK()
22272225
}
22282226

22292227
// UpdateIssueAssignee change issue's or pull's assignee
@@ -2267,9 +2265,7 @@ func UpdateIssueAssignee(ctx *context.Context) {
22672265
}
22682266
}
22692267
}
2270-
ctx.JSON(http.StatusOK, map[string]any{
2271-
"ok": true,
2272-
})
2268+
ctx.JSONOK()
22732269
}
22742270

22752271
// UpdatePullReviewRequest add or remove review request
@@ -2392,9 +2388,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
23922388
}
23932389
}
23942390

2395-
ctx.JSON(http.StatusOK, map[string]any{
2396-
"ok": true,
2397-
})
2391+
ctx.JSONOK()
23982392
}
23992393

24002394
// SearchIssues searches for issues across the repositories that the user has access to

routers/web/repo/issue_label.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ func DeleteLabel(ctx *context.Context) {
157157
ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
158158
}
159159

160-
ctx.JSON(http.StatusOK, map[string]any{
161-
"redirect": ctx.Repo.RepoLink + "/labels",
162-
})
160+
ctx.JSONRedirect(ctx.Repo.RepoLink + "/labels")
163161
}
164162

165163
// UpdateIssueLabel change issue's labels
@@ -226,7 +224,5 @@ func UpdateIssueLabel(ctx *context.Context) {
226224
return
227225
}
228226

229-
ctx.JSON(http.StatusOK, map[string]any{
230-
"ok": true,
231-
})
227+
ctx.JSONOK()
232228
}

routers/web/repo/milestone.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ func DeleteMilestone(ctx *context.Context) {
255255
ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success"))
256256
}
257257

258-
ctx.JSON(http.StatusOK, map[string]any{
259-
"redirect": ctx.Repo.RepoLink + "/milestones",
260-
})
258+
ctx.JSONRedirect(ctx.Repo.RepoLink + "/milestones")
261259
}
262260

263261
// MilestoneIssuesAndPulls lists all the issues and pull requests of the milestone

0 commit comments

Comments
 (0)