mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-19 11:31:05 +00:00
Adds `sort` and `order` query parameters to all action job list API
endpoints (`/admin/actions/jobs`, `/repos/{owner}/{repo}/actions/jobs`,
`/repos/{owner}/{repo}/actions/runs/{run}/jobs`, `/user/actions/jobs`),
following the existing `OrderByMap` pattern used by repo/user search
endpoints.
- Default is `id` / `asc` (backwards compatible — matches previous DB
natural order)
- Only `id` sort field for now; the map is extensible for future fields
- Returns 422 for invalid sort/order values
- `ToOrders()` returns empty string when `OrderBy` is unset, so internal
callers (webhook dispatch, concurrency checks) are unaffected
Closes: #37666
Supersedes: #37667
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: silverwind <me@silverwind.io>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
// ResolveSortOrder reads "sort" and "order" query params and returns the matching
|
|
// SearchOrderBy from orderByMap. When "sort" is absent, returns defaultOrder.
|
|
// On invalid input it writes a 422 response and returns ok=false; callers should
|
|
// then return immediately.
|
|
func ResolveSortOrder(ctx *context.APIContext, orderByMap map[string]map[string]db.SearchOrderBy, defaultOrder db.SearchOrderBy) (db.SearchOrderBy, bool) {
|
|
sortMode := ctx.FormString("sort")
|
|
if sortMode == "" {
|
|
return defaultOrder, true
|
|
}
|
|
sortOrder := ctx.FormString("order")
|
|
if sortOrder == "" {
|
|
sortOrder = "asc"
|
|
}
|
|
orderMap, ok := orderByMap[sortOrder]
|
|
if !ok {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("Invalid sort order: %q", sortOrder))
|
|
return "", false
|
|
}
|
|
orderBy, ok := orderMap[sortMode]
|
|
if !ok {
|
|
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("Invalid sort mode: %q", sortMode))
|
|
return "", false
|
|
}
|
|
return orderBy, true
|
|
}
|