This commit is contained in:
@@ -6,12 +6,12 @@ package utils
|
||||
import (
|
||||
"errors"
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
"code.gitea.io/gitea/modules/reqctx"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
git_model "gitea.dev/models/git"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/gitrepo"
|
||||
"gitea.dev/modules/reqctx"
|
||||
"gitea.dev/services/context"
|
||||
)
|
||||
|
||||
type RefCommit struct {
|
||||
|
||||
@@ -8,17 +8,17 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
webhook_service "code.gitea.io/gitea/services/webhook"
|
||||
"gitea.dev/models/db"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/models/webhook"
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/validation"
|
||||
webhook_module "gitea.dev/modules/webhook"
|
||||
"gitea.dev/services/context"
|
||||
webhook_service "gitea.dev/services/webhook"
|
||||
)
|
||||
|
||||
// ListOwnerHooks lists the webhooks of the provided owner
|
||||
@@ -48,7 +48,7 @@ func ListOwnerHooks(ctx *context.APIContext, owner *user_model.User) {
|
||||
ctx.JSON(http.StatusOK, apiHooks)
|
||||
}
|
||||
|
||||
// GetOwnerHook gets an user or organization webhook. Errors are written to ctx.
|
||||
// GetOwnerHook gets a user or organization webhook. Errors are written to ctx.
|
||||
func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webhook, error) {
|
||||
w, err := webhook.GetWebhookByOwnerID(ctx, ownerID, hookID)
|
||||
if err != nil {
|
||||
@@ -114,7 +114,7 @@ func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
}
|
||||
}
|
||||
|
||||
// AddOwnerHook adds a hook to an user or organization
|
||||
// AddOwnerHook adds a hook to a user or organization
|
||||
func AddOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.CreateHookOption) {
|
||||
hook, ok := addHook(ctx, form, owner.ID, 0)
|
||||
if !ok {
|
||||
@@ -294,7 +294,7 @@ func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID in
|
||||
ctx.JSON(http.StatusOK, h)
|
||||
}
|
||||
|
||||
// EditOwnerHook updates a webhook of an user or organization
|
||||
// EditOwnerHook updates a webhook of a user or organization
|
||||
func EditOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.EditHookOption, hookID int64) {
|
||||
hook, err := GetOwnerHook(ctx, owner.ID, hookID)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/services/contexttest"
|
||||
"gitea.dev/models/unittest"
|
||||
"gitea.dev/modules/structs"
|
||||
"gitea.dev/services/contexttest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -6,9 +6,9 @@ package utils
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
webhook_service "code.gitea.io/gitea/services/webhook"
|
||||
"gitea.dev/models/unittest"
|
||||
"gitea.dev/modules/setting"
|
||||
webhook_service "gitea.dev/services/webhook"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
"gitea.dev/models/db"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
)
|
||||
|
||||
// GetListOptions returns list options using the page and limit parameters
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
"gitea.dev/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.Sprintf("Invalid sort order: %q", sortOrder))
|
||||
return "", false
|
||||
}
|
||||
orderBy, ok := orderMap[sortMode]
|
||||
if !ok {
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid sort mode: %q", sortMode))
|
||||
return "", false
|
||||
}
|
||||
return orderBy, true
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
"gitea.dev/services/contexttest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestResolveSortOrder(t *testing.T) {
|
||||
m := map[string]map[string]db.SearchOrderBy{
|
||||
"asc": {"id": "id ASC"},
|
||||
"desc": {"id": "id DESC"},
|
||||
}
|
||||
defaultOrder := db.SearchOrderBy("default")
|
||||
|
||||
cases := []struct {
|
||||
path string
|
||||
wantOK bool
|
||||
wantOrder db.SearchOrderBy
|
||||
wantStatus int
|
||||
}{
|
||||
{"GET /", true, defaultOrder, 0},
|
||||
{"GET /?sort=id", true, "id ASC", 0},
|
||||
{"GET /?sort=id&order=desc", true, "id DESC", 0},
|
||||
{"GET /?sort=bogus", false, "", http.StatusUnprocessableEntity},
|
||||
{"GET /?sort=id&order=bogus", false, "", http.StatusUnprocessableEntity},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.path, func(t *testing.T) {
|
||||
ctx, _ := contexttest.MockAPIContext(t, tc.path)
|
||||
got, ok := ResolveSortOrder(ctx, m, defaultOrder)
|
||||
assert.Equal(t, tc.wantOK, ok)
|
||||
assert.Equal(t, tc.wantOrder, got)
|
||||
assert.Equal(t, tc.wantStatus, ctx.Resp.WrittenStatus())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user