feat(api): add sort parameter to list issues API (#7211)

- Add the `sort` parameter to the `/api/v1/{repo}/{owner}/issues` API endpoint. Default behavior is preserved.
- Resolves forgejo/forgejo#4173
- Add (non-exhaustive) integration testing.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7211
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-03-17 09:02:57 +00:00 committed by Earl Warren
parent a624b6a8f4
commit 7d6d4f94ee
4 changed files with 97 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt"
"os"
"runtime/pprof"
"strings"
"sync/atomic"
"time"
@ -280,6 +281,33 @@ const (
SortByDeadlineAsc = internal.SortByDeadlineAsc
)
// ParseSortBy parses the `sortBy` string and returns the associated `SortBy`
// value, if one exists. Otherwise return `defaultSortBy`.
func ParseSortBy(sortBy string, defaultSortBy internal.SortBy) internal.SortBy {
switch strings.ToLower(sortBy) {
case "relevance":
return SortByScore
case "latest":
return SortByCreatedDesc
case "oldest":
return SortByCreatedAsc
case "recentupdate":
return SortByUpdatedDesc
case "leastupdate":
return SortByUpdatedAsc
case "mostcomment":
return SortByCommentsDesc
case "leastcomment":
return SortByCommentsAsc
case "nearduedate":
return SortByDeadlineAsc
case "farduedate":
return SortByDeadlineDesc
default:
return defaultSortBy
}
}
// SearchIssues search issues by options.
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
indexer := *globalIndexer.Load()