Skip to content

Commit 46beb7f

Browse files
GiteaBotearl-warrenlunny
authored
enable system users search via the API (#28013) (#28018)
Backport #28013 by @earl-warren Refs: https://codeberg.org/forgejo/forgejo/issues/1403 (cherry picked from commit dd4d17c159eaf8b642aa9e6105b0532e25972bb7) --------- Co-authored-by: Earl Warren <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 3107093 commit 46beb7f

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

models/user/user_system.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func NewReplaceUser(name string) *User {
3636
}
3737

3838
const (
39+
GhostUserID = -1
3940
ActionsUserID = -2
4041
ActionsUserName = "gitea-actions"
4142
ActionsFullName = "Gitea Actions"

routers/api/v1/user/user.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,33 @@ func Search(ctx *context.APIContext) {
5454

5555
listOptions := utils.GetListOptions(ctx)
5656

57-
users, maxResults, err := user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
58-
Actor: ctx.Doer,
59-
Keyword: ctx.FormTrim("q"),
60-
UID: ctx.FormInt64("uid"),
61-
Type: user_model.UserTypeIndividual,
62-
ListOptions: listOptions,
63-
})
64-
if err != nil {
65-
ctx.JSON(http.StatusInternalServerError, map[string]any{
66-
"ok": false,
67-
"error": err.Error(),
57+
uid := ctx.FormInt64("uid")
58+
var users []*user_model.User
59+
var maxResults int64
60+
var err error
61+
62+
switch uid {
63+
case user_model.GhostUserID:
64+
maxResults = 1
65+
users = []*user_model.User{user_model.NewGhostUser()}
66+
case user_model.ActionsUserID:
67+
maxResults = 1
68+
users = []*user_model.User{user_model.NewActionsUser()}
69+
default:
70+
users, maxResults, err = user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
71+
Actor: ctx.Doer,
72+
Keyword: ctx.FormTrim("q"),
73+
UID: uid,
74+
Type: user_model.UserTypeIndividual,
75+
ListOptions: listOptions,
6876
})
69-
return
77+
if err != nil {
78+
ctx.JSON(http.StatusInternalServerError, map[string]any{
79+
"ok": false,
80+
"error": err.Error(),
81+
})
82+
return
83+
}
7084
}
7185

7286
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)

tests/integration/api_user_search_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) {
5656
}
5757
}
5858

59+
func TestAPIUserSearchSystemUsers(t *testing.T) {
60+
defer tests.PrepareTestEnv(t)()
61+
for _, systemUser := range []*user_model.User{
62+
user_model.NewGhostUser(),
63+
user_model.NewActionsUser(),
64+
} {
65+
t.Run(systemUser.Name, func(t *testing.T) {
66+
req := NewRequestf(t, "GET", "/api/v1/users/search?uid=%d", systemUser.ID)
67+
resp := MakeRequest(t, req, http.StatusOK)
68+
69+
var results SearchResults
70+
DecodeJSON(t, resp, &results)
71+
assert.NotEmpty(t, results.Data)
72+
if assert.EqualValues(t, 1, len(results.Data)) {
73+
user := results.Data[0]
74+
assert.EqualValues(t, user.UserName, systemUser.Name)
75+
assert.EqualValues(t, user.ID, systemUser.ID)
76+
}
77+
})
78+
}
79+
}
80+
5981
func TestAPIUserSearchAdminLoggedInUserHidden(t *testing.T) {
6082
defer tests.PrepareTestEnv(t)()
6183
adminUsername := "user1"

0 commit comments

Comments
 (0)