0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-04-06 18:13:27 +02:00

feat: add endpoint to retrieve root-level groups in org

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2025-12-20 15:23:24 -05:00
parent b0257d1468
commit 8d47d6cdb9
No known key found for this signature in database
GPG Key ID: 924A5F6AF051E87C
2 changed files with 79 additions and 0 deletions

View File

@ -1734,6 +1734,7 @@ func Routes() *web.Router {
})
}, reqToken(), reqOrgOwnership())
m.Group("/groups", func() {
m.Get("", org.GetOrgGroups)
m.Post("/new", reqToken(), reqGroupMembership(perm.AccessModeWrite, true), group.NewGroup)
})
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), orgAssignment(true), checkTokenPublicOnly())

View File

@ -0,0 +1,78 @@
package org
import (
"net/http"
group_model "code.gitea.io/gitea/models/group"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/models/unit"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
)
func GetOrgGroups(ctx *context.APIContext) {
// swagger:operation GET /orgs/{org}/groups organization orgListGroups
// ---
// summary: List an organization's root-level groups
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization
// type: string
// required: true
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/GroupList"
// "404":
// "$ref": "#/responses/notFound"
var doerID int64
if ctx.Doer != nil {
doerID = ctx.Doer.ID
}
org, err := organization.GetOrgByName(ctx, ctx.PathParam("org"))
if err != nil {
if organization.IsErrOrgNotExist(err) {
ctx.APIError(http.StatusUnprocessableEntity, err)
} else {
ctx.APIErrorInternal(err)
}
return
}
if !organization.HasOrgOrUserVisible(ctx, org.AsUser(), ctx.Doer) {
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil)
return
}
groups, err := group_model.FindGroupsByCond(ctx, &group_model.FindGroupsOptions{
ParentGroupID: 0,
ActorID: doerID,
OwnerID: org.ID,
}, group_model.
AccessibleGroupCondition(ctx.Doer, unit.TypeInvalid, perm.AccessModeRead))
if err != nil {
ctx.APIErrorInternal(err)
return
}
apiGroups := make([]*api.Group, len(groups))
for i, group := range groups {
apiGroups[i], err = convert.ToAPIGroup(ctx, group, ctx.Doer)
if err != nil {
ctx.APIErrorInternal(err)
return
}
}
ctx.SetTotalCountHeader(int64(len(groups)))
ctx.JSON(http.StatusOK, apiGroups)
}