Skip to content

Commit 10055bd

Browse files
6543lunny
authored andcommitted
[API] add GET /orgs endpoint (#9560)
* introduce `GET /orgs` * add TEST * show also other VisibleType's * update description * refactor a lot * SearchUserOptions by default return only public
1 parent 497e15f commit 10055bd

File tree

8 files changed

+108
-9
lines changed

8 files changed

+108
-9
lines changed

integrations/api_org_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@ func TestAPIOrgDeny(t *testing.T) {
136136
MakeRequest(t, req, http.StatusNotFound)
137137
})
138138
}
139+
140+
func TestAPIGetAll(t *testing.T) {
141+
defer prepareTestEnv(t)()
142+
143+
req := NewRequestf(t, "GET", "/api/v1/orgs")
144+
resp := MakeRequest(t, req, http.StatusOK)
145+
146+
var apiOrgList []*api.Organization
147+
DecodeJSON(t, resp, &apiOrgList)
148+
149+
assert.Len(t, apiOrgList, 7)
150+
assert.Equal(t, "org25", apiOrgList[0].FullName)
151+
assert.Equal(t, "public", apiOrgList[0].Visibility)
152+
}

models/user.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ type SearchUserOptions struct {
14691469
UID int64
14701470
OrderBy SearchOrderBy
14711471
Page int
1472-
Private bool // Include private orgs in search
1472+
Visible []structs.VisibleType
14731473
OwnerID int64 // id of user for visibility calculation
14741474
PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
14751475
IsActive util.OptionalBool
@@ -1492,8 +1492,9 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
14921492
cond = cond.And(keywordCond)
14931493
}
14941494

1495-
if !opts.Private {
1496-
// user not logged in and so they won't be allowed to see non-public orgs
1495+
if len(opts.Visible) > 0 {
1496+
cond = cond.And(builder.In("visibility", opts.Visible))
1497+
} else {
14971498
cond = cond.And(builder.In("visibility", structs.VisibleTypePublic))
14981499
}
14991500

routers/admin/orgs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"code.gitea.io/gitea/modules/base"
1010
"code.gitea.io/gitea/modules/context"
1111
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/structs"
1213
"code.gitea.io/gitea/routers"
1314
)
1415

@@ -25,6 +26,6 @@ func Organizations(ctx *context.Context) {
2526
routers.RenderUserSearch(ctx, &models.SearchUserOptions{
2627
Type: models.UserTypeOrganization,
2728
PageSize: setting.UI.Admin.OrgPagingNum,
28-
Private: true,
29+
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
2930
}, tplOrgs)
3031
}

routers/api/v1/admin/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func GetAllOrgs(ctx *context.APIContext) {
104104
OrderBy: models.SearchOrderByAlphabetically,
105105
Page: ctx.QueryInt("page"),
106106
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
107-
Private: true,
107+
Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate},
108108
})
109109
if err != nil {
110110
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ func RegisterRoutes(m *macaron.Macaron) {
821821
m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
822822
m.Get("/users/:username/orgs", org.ListUserOrgs)
823823
m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create)
824+
m.Get("/orgs", org.GetAll)
824825
m.Group("/orgs/:orgname", func() {
825826
m.Combo("").Get(org.Get).
826827
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).

routers/api/v1/org/org.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,53 @@ func ListUserOrgs(ctx *context.APIContext) {
6666
listUserOrgs(ctx, u, ctx.User.IsAdmin)
6767
}
6868

69+
// GetAll return list of all public organizations
70+
func GetAll(ctx *context.APIContext) {
71+
// swagger:operation Get /orgs organization orgGetAll
72+
// ---
73+
// summary: Get list of organizations
74+
// produces:
75+
// - application/json
76+
// parameters:
77+
// - name: page
78+
// in: query
79+
// description: page number of results to return (1-based)
80+
// type: integer
81+
// - name: limit
82+
// in: query
83+
// description: page size of results, maximum page size is 50
84+
// type: integer
85+
// responses:
86+
// "200":
87+
// "$ref": "#/responses/OrganizationList"
88+
89+
vMode := []api.VisibleType{api.VisibleTypePublic}
90+
if ctx.IsSigned {
91+
vMode = append(vMode, api.VisibleTypeLimited)
92+
if ctx.User.IsAdmin {
93+
vMode = append(vMode, api.VisibleTypePrivate)
94+
}
95+
}
96+
97+
publicOrgs, _, err := models.SearchUsers(&models.SearchUserOptions{
98+
Type: models.UserTypeOrganization,
99+
OrderBy: models.SearchOrderByAlphabetically,
100+
Page: ctx.QueryInt("page"),
101+
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
102+
Visible: vMode,
103+
})
104+
if err != nil {
105+
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
106+
return
107+
}
108+
orgs := make([]*api.Organization, len(publicOrgs))
109+
for i := range publicOrgs {
110+
orgs[i] = convert.ToOrganization(publicOrgs[i])
111+
}
112+
113+
ctx.JSON(http.StatusOK, &orgs)
114+
}
115+
69116
// Create api for create organization
70117
func Create(ctx *context.APIContext, form api.CreateOrgOption) {
71118
// swagger:operation POST /orgs organization orgCreate

routers/home.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
code_indexer "code.gitea.io/gitea/modules/indexer/code"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/structs"
1819
"code.gitea.io/gitea/modules/util"
1920
"code.gitea.io/gitea/routers/user"
2021
)
@@ -249,7 +250,7 @@ func ExploreUsers(ctx *context.Context) {
249250
Type: models.UserTypeIndividual,
250251
PageSize: setting.UI.ExplorePagingNum,
251252
IsActive: util.OptionalBoolTrue,
252-
Private: true,
253+
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
253254
}, tplExploreUsers)
254255
}
255256

@@ -265,12 +266,17 @@ func ExploreOrganizations(ctx *context.Context) {
265266
ownerID = ctx.User.ID
266267
}
267268

268-
RenderUserSearch(ctx, &models.SearchUserOptions{
269+
opts := models.SearchUserOptions{
269270
Type: models.UserTypeOrganization,
270271
PageSize: setting.UI.ExplorePagingNum,
271-
Private: ctx.User != nil,
272272
OwnerID: ownerID,
273-
}, tplExploreOrganizations)
273+
}
274+
if ctx.User != nil {
275+
opts.Visible = []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}
276+
} else {
277+
opts.Visible = []structs.VisibleType{structs.VisibleTypePublic}
278+
}
279+
RenderUserSearch(ctx, &opts, tplExploreOrganizations)
274280
}
275281

276282
// ExploreCode render explore code page

templates/swagger/v1_json.tmpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,35 @@
606606
}
607607
},
608608
"/orgs": {
609+
"get": {
610+
"produces": [
611+
"application/json"
612+
],
613+
"tags": [
614+
"organization"
615+
],
616+
"summary": "Get list of organizations",
617+
"operationId": "orgGetAll",
618+
"parameters": [
619+
{
620+
"type": "integer",
621+
"description": "page number of results to return (1-based)",
622+
"name": "page",
623+
"in": "query"
624+
},
625+
{
626+
"type": "integer",
627+
"description": "page size of results, maximum page size is 50",
628+
"name": "limit",
629+
"in": "query"
630+
}
631+
],
632+
"responses": {
633+
"200": {
634+
"$ref": "#/responses/OrganizationList"
635+
}
636+
}
637+
},
609638
"post": {
610639
"consumes": [
611640
"application/json"

0 commit comments

Comments
 (0)