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

rename context.Query to context.Form #16562

Merged
merged 3 commits into from
Jul 29, 2021
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
2 changes: 1 addition & 1 deletion modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func genAPILinks(curURL *url.URL, total, pageSize, curPage int) []string {

// SetLinkHeader sets pagination link header by given total number and page size.
func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
links := genAPILinks(ctx.Req.URL, total, pageSize, ctx.QueryInt("page"))
links := genAPILinks(ctx.Req.URL, total, pageSize, ctx.FormInt("page"))

if len(links) > 0 {
ctx.Header().Set("Link", strings.Join(links, ","))
Expand Down
31 changes: 14 additions & 17 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,41 +287,38 @@ func (ctx *Context) Header() http.Header {
return ctx.Resp.Header()
}

// FIXME: We should differ Query and Form, currently we just use form as query
// Currently to be compatible with macaron, we keep it.

// Query returns request form as string with default
func (ctx *Context) Query(key string, defaults ...string) string {
// Form returns request form as string with default
func (ctx *Context) Form(key string, defaults ...string) string {
return (*Forms)(ctx.Req).MustString(key, defaults...)
}

// QueryTrim returns request form as string with default and trimmed spaces
func (ctx *Context) QueryTrim(key string, defaults ...string) string {
// FormTrim returns request form as string with default and trimmed spaces
func (ctx *Context) FormTrim(key string, defaults ...string) string {
return (*Forms)(ctx.Req).MustTrimmed(key, defaults...)
}

// QueryStrings returns request form as strings with default
func (ctx *Context) QueryStrings(key string, defaults ...[]string) []string {
// FormStrings returns request form as strings with default
func (ctx *Context) FormStrings(key string, defaults ...[]string) []string {
return (*Forms)(ctx.Req).MustStrings(key, defaults...)
}

// QueryInt returns request form as int with default
func (ctx *Context) QueryInt(key string, defaults ...int) int {
// FormInt returns request form as int with default
func (ctx *Context) FormInt(key string, defaults ...int) int {
return (*Forms)(ctx.Req).MustInt(key, defaults...)
}

// QueryInt64 returns request form as int64 with default
func (ctx *Context) QueryInt64(key string, defaults ...int64) int64 {
// FormInt64 returns request form as int64 with default
func (ctx *Context) FormInt64(key string, defaults ...int64) int64 {
return (*Forms)(ctx.Req).MustInt64(key, defaults...)
}

// QueryBool returns request form as bool with default
func (ctx *Context) QueryBool(key string, defaults ...bool) bool {
// FormBool returns request form as bool with default
func (ctx *Context) FormBool(key string, defaults ...bool) bool {
return (*Forms)(ctx.Req).MustBool(key, defaults...)
}

// QueryOptionalBool returns request form as OptionalBool with default
func (ctx *Context) QueryOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
// FormOptionalBool returns request form as OptionalBool with default
func (ctx *Context) FormOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
return (*Forms)(ctx.Req).MustOptionalBool(key, defaults...)
}

Expand Down
8 changes: 4 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {

// Check access.
if ctx.Repo.Permission.AccessMode == models.AccessModeNone {
if ctx.Query("go-get") == "1" {
if ctx.Form("go-get") == "1" {
EarlyResponseForGoGetMeta(ctx)
return
}
Expand Down Expand Up @@ -415,7 +415,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
owner, err = models.GetUserByName(userName)
if err != nil {
if models.IsErrUserNotExist(err) {
if ctx.Query("go-get") == "1" {
if ctx.Form("go-get") == "1" {
EarlyResponseForGoGetMeta(ctx)
return
}
Expand All @@ -437,7 +437,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
if err == nil {
RedirectToRepo(ctx, redirectRepoID)
} else if models.IsErrRepoRedirectNotExist(err) {
if ctx.Query("go-get") == "1" {
if ctx.Form("go-get") == "1" {
EarlyResponseForGoGetMeta(ctx)
return
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
}
}

if ctx.Query("go-get") == "1" {
if ctx.Form("go-get") == "1" {
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", "branch", ctx.Repo.BranchName)
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/admin/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ListUnadoptedRepositories(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"

listOptions := utils.GetListOptions(ctx)
repoNames, count, err := repository.ListUnadoptedRepositories(ctx.Query("query"), &listOptions)
repoNames, count, err := repository.ListUnadoptedRepositories(ctx.Form("query"), &listOptions)
if err != nil {
ctx.InternalServerError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import (

func sudo() func(ctx *context.APIContext) {
return func(ctx *context.APIContext) {
sudo := ctx.Query("sudo")
sudo := ctx.Form("sudo")
if len(sudo) == 0 {
sudo = ctx.Req.Header.Get("Sudo")
}
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/notify/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func getFindNotificationOptions(ctx *context.APIContext) *models.FindNotificatio
UpdatedBeforeUnix: before,
UpdatedAfterUnix: since,
}
if !ctx.QueryBool("all") {
statuses := ctx.QueryStrings("status-types")
if !ctx.FormBool("all") {
statuses := ctx.FormStrings("status-types")
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
}

subjectTypes := ctx.QueryStrings("subject-type")
subjectTypes := ctx.FormStrings("subject-type")
if len(subjectTypes) != 0 {
opts.Source = subjectToSource(subjectTypes)
}
Expand Down
8 changes: 4 additions & 4 deletions routers/api/v1/notify/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
// "$ref": "#/responses/empty"

lastRead := int64(0)
qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
qLastRead := strings.Trim(ctx.Form("last_read_at"), " ")
if len(qLastRead) > 0 {
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
if err != nil {
Expand All @@ -189,8 +189,8 @@ func ReadRepoNotifications(ctx *context.APIContext) {
UpdatedBeforeUnix: lastRead,
}

if !ctx.QueryBool("all") {
statuses := ctx.QueryStrings("status-types")
if !ctx.FormBool("all") {
statuses := ctx.FormStrings("status-types")
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
log.Error("%v", opts.Status)
}
Expand All @@ -200,7 +200,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
return
}

targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
targetStatus := statusStringToNotificationStatus(ctx.Form("to-status"))
if targetStatus == 0 {
targetStatus = models.NotificationStatusRead
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/notify/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func ReadThread(ctx *context.APIContext) {
return
}

targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
targetStatus := statusStringToNotificationStatus(ctx.Form("to-status"))
if targetStatus == 0 {
targetStatus = models.NotificationStatusRead
}
Expand Down
8 changes: 4 additions & 4 deletions routers/api/v1/notify/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func ReadNotifications(ctx *context.APIContext) {
// "$ref": "#/responses/empty"

lastRead := int64(0)
qLastRead := strings.Trim(ctx.Query("last_read_at"), " ")
qLastRead := strings.Trim(ctx.Form("last_read_at"), " ")
if len(qLastRead) > 0 {
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
if err != nil {
Expand All @@ -137,8 +137,8 @@ func ReadNotifications(ctx *context.APIContext) {
UserID: ctx.User.ID,
UpdatedBeforeUnix: lastRead,
}
if !ctx.QueryBool("all") {
statuses := ctx.QueryStrings("status-types")
if !ctx.FormBool("all") {
statuses := ctx.FormStrings("status-types")
opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
}
nl, err := models.GetNotifications(opts)
Expand All @@ -147,7 +147,7 @@ func ReadNotifications(ctx *context.APIContext) {
return
}

targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
targetStatus := statusStringToNotificationStatus(ctx.Form("to-status"))
if targetStatus == 0 {
targetStatus = models.NotificationStatusRead
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/org/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ListLabels(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/LabelList"

labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.Query("sort"), utils.GetListOptions(ctx))
labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.Form("sort"), utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsByOrgID", err)
return
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/org/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ func SearchTeam(ctx *context.APIContext) {

opts := &models.SearchTeamOptions{
UserID: ctx.User.ID,
Keyword: strings.TrimSpace(ctx.Query("q")),
Keyword: strings.TrimSpace(ctx.Form("q")),
OrgID: ctx.Org.Organization.ID,
IncludeDesc: ctx.Query("include_desc") == "" || ctx.QueryBool("include_desc"),
IncludeDesc: ctx.Form("include_desc") == "" || ctx.FormBool("include_desc"),
ListOptions: listOptions,
}

Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func GetAllCommits(ctx *context.APIContext) {
listOptions.PageSize = setting.Git.CommitsRangeSize
}

sha := ctx.Query("sha")
sha := ctx.Form("sha")

var baseCommit *git.Commit
if len(sha) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetRawFile(ctx *context.APIContext) {

commit := ctx.Repo.Commit

if ref := ctx.QueryTrim("ref"); len(ref) > 0 {
if ref := ctx.FormTrim("ref"); len(ref) > 0 {
var err error
commit, err = ctx.Repo.GitRepo.GetCommit(ref)
if err != nil {
Expand Down Expand Up @@ -549,7 +549,7 @@ func GetContents(ctx *context.APIContext) {
}

treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref")
ref := ctx.FormTrim("ref")

if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
Expand Down
36 changes: 18 additions & 18 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func SearchIssues(ctx *context.APIContext) {
}

var isClosed util.OptionalBool
switch ctx.Query("state") {
switch ctx.Form("state") {
case "closed":
isClosed = util.OptionalBoolTrue
case "all":
Expand Down Expand Up @@ -140,7 +140,7 @@ func SearchIssues(ctx *context.APIContext) {
var issues []*models.Issue
var filteredCount int64

keyword := strings.Trim(ctx.Query("q"), " ")
keyword := strings.Trim(ctx.Form("q"), " ")
if strings.IndexByte(keyword, 0) >= 0 {
keyword = ""
}
Expand All @@ -153,7 +153,7 @@ func SearchIssues(ctx *context.APIContext) {
}

var isPull util.OptionalBool
switch ctx.Query("type") {
switch ctx.Form("type") {
case "pulls":
isPull = util.OptionalBoolTrue
case "issues":
Expand All @@ -162,21 +162,21 @@ func SearchIssues(ctx *context.APIContext) {
isPull = util.OptionalBoolNone
}

labels := strings.TrimSpace(ctx.Query("labels"))
labels := strings.TrimSpace(ctx.Form("labels"))
var includedLabelNames []string
if len(labels) > 0 {
includedLabelNames = strings.Split(labels, ",")
}

milestones := strings.TrimSpace(ctx.Query("milestones"))
milestones := strings.TrimSpace(ctx.Form("milestones"))
var includedMilestones []string
if len(milestones) > 0 {
includedMilestones = strings.Split(milestones, ",")
}

// this api is also used in UI,
// so the default limit is set to fit UI needs
limit := ctx.QueryInt("limit")
limit := ctx.FormInt("limit")
if limit == 0 {
limit = setting.UI.IssuePagingNum
} else if limit > setting.API.MaxResponseItems {
Expand All @@ -188,7 +188,7 @@ func SearchIssues(ctx *context.APIContext) {
if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 {
issuesOpt := &models.IssuesOptions{
ListOptions: models.ListOptions{
Page: ctx.QueryInt("page"),
Page: ctx.FormInt("page"),
PageSize: limit,
},
RepoIDs: repoIDs,
Expand All @@ -197,23 +197,23 @@ func SearchIssues(ctx *context.APIContext) {
IncludedLabelNames: includedLabelNames,
IncludeMilestones: includedMilestones,
SortType: "priorityrepo",
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
PriorityRepoID: ctx.FormInt64("priority_repo_id"),
IsPull: isPull,
UpdatedBeforeUnix: before,
UpdatedAfterUnix: since,
}

// Filter for: Created by User, Assigned to User, Mentioning User, Review of User Requested
if ctx.QueryBool("created") {
if ctx.FormBool("created") {
issuesOpt.PosterID = ctx.User.ID
}
if ctx.QueryBool("assigned") {
if ctx.FormBool("assigned") {
issuesOpt.AssigneeID = ctx.User.ID
}
if ctx.QueryBool("mentioned") {
if ctx.FormBool("mentioned") {
issuesOpt.MentionedID = ctx.User.ID
}
if ctx.QueryBool("review_requested") {
if ctx.FormBool("review_requested") {
issuesOpt.ReviewRequestedID = ctx.User.ID
}

Expand Down Expand Up @@ -319,7 +319,7 @@ func ListIssues(ctx *context.APIContext) {
}

var isClosed util.OptionalBool
switch ctx.Query("state") {
switch ctx.Form("state") {
case "closed":
isClosed = util.OptionalBoolTrue
case "all":
Expand All @@ -331,7 +331,7 @@ func ListIssues(ctx *context.APIContext) {
var issues []*models.Issue
var filteredCount int64

keyword := strings.Trim(ctx.Query("q"), " ")
keyword := strings.Trim(ctx.Form("q"), " ")
if strings.IndexByte(keyword, 0) >= 0 {
keyword = ""
}
Expand All @@ -345,7 +345,7 @@ func ListIssues(ctx *context.APIContext) {
}
}

if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
if splitted := strings.Split(ctx.Form("labels"), ","); len(splitted) > 0 {
labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
Expand All @@ -354,7 +354,7 @@ func ListIssues(ctx *context.APIContext) {
}

var mileIDs []int64
if part := strings.Split(ctx.Query("milestones"), ","); len(part) > 0 {
if part := strings.Split(ctx.Form("milestones"), ","); len(part) > 0 {
for i := range part {
// uses names and fall back to ids
// non existent milestones are discarded
Expand Down Expand Up @@ -386,7 +386,7 @@ func ListIssues(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)

var isPull util.OptionalBool
switch ctx.Query("type") {
switch ctx.Form("type") {
case "pulls":
isPull = util.OptionalBoolTrue
case "issues":
Expand Down Expand Up @@ -448,7 +448,7 @@ func ListIssues(ctx *context.APIContext) {
}

func getUserIDForFilter(ctx *context.APIContext, queryName string) int64 {
userName := ctx.Query(queryName)
userName := ctx.Form(queryName)
if len(userName) == 0 {
return 0
}
Expand Down
Loading