Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move web JSON functions to web context and simplify code #26132

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions modules/context/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
8 changes: 2 additions & 6 deletions routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions routers/web/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions routers/web/admin/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions routers/web/admin/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
4 changes: 1 addition & 3 deletions routers/web/admin/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions routers/web/admin/stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion routers/web/auth/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 3 additions & 9 deletions routers/web/org/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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)
}
Expand All @@ -138,7 +134,5 @@ func MembersAction(ctx *context.Context) {
redirect = setting.AppSubURL + "/"
}

ctx.JSON(http.StatusOK, map[string]any{
"redirect": redirect,
})
ctx.JSONRedirect(redirect)
}
4 changes: 1 addition & 3 deletions routers/web/org/org_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 8 additions & 24 deletions routers/web/org/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -526,9 +520,7 @@ func AddBoardToProjectPost(ctx *context.Context) {
return
}

ctx.JSON(http.StatusOK, map[string]any{
"ok": true,
})
ctx.JSONOK()
}

// CheckProjectBoardChangePermissions check permission
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -730,7 +716,5 @@ func MoveIssues(ctx *context.Context) {
return
}

ctx.JSON(http.StatusOK, map[string]any{
"ok": true,
})
ctx.JSONOK()
}
4 changes: 1 addition & 3 deletions routers/web/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions routers/web/org/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions routers/web/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -226,7 +224,5 @@ func UpdateIssueLabel(ctx *context.Context) {
return
}

ctx.JSON(http.StatusOK, map[string]any{
"ok": true,
})
ctx.JSONOK()
}
4 changes: 1 addition & 3 deletions routers/web/repo/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading