mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add paginated API endpoint for groups (#27603)
backend-only changes from #27271; see that PR for summary of changes + implementation details
This commit is contained in:
Generated
+113
@@ -5761,6 +5761,66 @@ const docTemplate = `{
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v2/organizations/{organization}/paginated-groups": {
|
||||
"get": {
|
||||
"description": "Unlike \"Get groups by organization\" (GET /organizations/{organization}/groups),\nwhich authorizes each group individually via its ACL, this endpoint requires\norganization-wide group read permission and does no per-group filtering. It is\ntherefore not a drop-in replacement: callers without org-wide group read receive\nan error rather than a filtered subset.\n\nThe ` + "`" + `q` + "`" + ` parameter uses the shared filter syntax. Bare terms (including multi-word)\nperform a free-text search over group name and display name. ` + "`" + `search:` + "`" + ` is the only\naccepted key and unknown keys return 400. Because group display names may contain\ncolons, a value with a colon must be quoted, e.g. ` + "`" + `search:\"team: frontend\"` + "`" + `; an\nunquoted colon fails with ` + "`" + `Query element \"team:\" cannot start or end with ':'` + "`" + `.\n\nThis endpoint returns group summaries without the member roster: each group\ncarries only ` + "`" + `total_member_count` + "`" + ` and no ` + "`" + `members` + "`" + ` field. Callers that need the\nroster use the group members endpoint (GET /groups/{group}/members).",
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Enterprise"
|
||||
],
|
||||
"summary": "Get groups by organization (paginated)",
|
||||
"operationId": "get-groups-by-organization-paginated",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Organization ID or name",
|
||||
"name": "organization",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Search query (see description for syntax and colon-quoting)",
|
||||
"name": "q",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "Page limit",
|
||||
"name": "limit",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "Page offset",
|
||||
"name": "offset",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"description": "After ID",
|
||||
"name": "after_id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/codersdk.PaginatedGroupsResponse"
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"CoderSessionToken": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v2/organizations/{organization}/paginated-members": {
|
||||
"get": {
|
||||
"produces": [
|
||||
@@ -22514,6 +22574,59 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedGroup": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"avatar_url": {
|
||||
"type": "string",
|
||||
"format": "uri"
|
||||
},
|
||||
"display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"organization_display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"organization_id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"organization_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"quota_allowance": {
|
||||
"type": "integer"
|
||||
},
|
||||
"source": {
|
||||
"$ref": "#/definitions/codersdk.GroupSource"
|
||||
},
|
||||
"total_member_count": {
|
||||
"description": "TotalMemberCount is the number of members in the group, shown even when\nthe caller cannot read individual members. The roster itself is not\nreturned by this endpoint.",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedGroupsResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"count": {
|
||||
"type": "integer"
|
||||
},
|
||||
"groups": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/codersdk.PaginatedGroup"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedMembersResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
Generated
+109
@@ -5102,6 +5102,62 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v2/organizations/{organization}/paginated-groups": {
|
||||
"get": {
|
||||
"description": "Unlike \"Get groups by organization\" (GET /organizations/{organization}/groups),\nwhich authorizes each group individually via its ACL, this endpoint requires\norganization-wide group read permission and does no per-group filtering. It is\ntherefore not a drop-in replacement: callers without org-wide group read receive\nan error rather than a filtered subset.\n\nThe `q` parameter uses the shared filter syntax. Bare terms (including multi-word)\nperform a free-text search over group name and display name. `search:` is the only\naccepted key and unknown keys return 400. Because group display names may contain\ncolons, a value with a colon must be quoted, e.g. `search:\"team: frontend\"`; an\nunquoted colon fails with `Query element \"team:\" cannot start or end with ':'`.\n\nThis endpoint returns group summaries without the member roster: each group\ncarries only `total_member_count` and no `members` field. Callers that need the\nroster use the group members endpoint (GET /groups/{group}/members).",
|
||||
"produces": ["application/json"],
|
||||
"tags": ["Enterprise"],
|
||||
"summary": "Get groups by organization (paginated)",
|
||||
"operationId": "get-groups-by-organization-paginated",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Organization ID or name",
|
||||
"name": "organization",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Search query (see description for syntax and colon-quoting)",
|
||||
"name": "q",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "Page limit",
|
||||
"name": "limit",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "Page offset",
|
||||
"name": "offset",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"description": "After ID",
|
||||
"name": "after_id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/codersdk.PaginatedGroupsResponse"
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"CoderSessionToken": []
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v2/organizations/{organization}/paginated-members": {
|
||||
"get": {
|
||||
"produces": ["application/json"],
|
||||
@@ -20567,6 +20623,59 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedGroup": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"avatar_url": {
|
||||
"type": "string",
|
||||
"format": "uri"
|
||||
},
|
||||
"display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"organization_display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"organization_id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"organization_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"quota_allowance": {
|
||||
"type": "integer"
|
||||
},
|
||||
"source": {
|
||||
"$ref": "#/definitions/codersdk.GroupSource"
|
||||
},
|
||||
"total_member_count": {
|
||||
"description": "TotalMemberCount is the number of members in the group, shown even when\nthe caller cannot read individual members. The roster itself is not\nreturned by this endpoint.",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedGroupsResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"count": {
|
||||
"type": "integer"
|
||||
},
|
||||
"groups": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/codersdk.PaginatedGroup"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"codersdk.PaginatedMembersResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -377,6 +377,24 @@ func Group(row database.GetGroupsRow, members []database.GroupMember, totalMembe
|
||||
}
|
||||
}
|
||||
|
||||
// PaginatedGroup converts a group row into the slim summary returned by the
|
||||
// paginated groups endpoint, which omits the member roster and carries only
|
||||
// the total member count.
|
||||
func PaginatedGroup(row database.GetGroupsRow, totalMemberCount int) codersdk.PaginatedGroup {
|
||||
return codersdk.PaginatedGroup{
|
||||
ID: row.Group.ID,
|
||||
Name: row.Group.Name,
|
||||
DisplayName: row.Group.DisplayName,
|
||||
OrganizationID: row.Group.OrganizationID,
|
||||
AvatarURL: row.Group.AvatarURL,
|
||||
TotalMemberCount: totalMemberCount,
|
||||
QuotaAllowance: int(row.Group.QuotaAllowance),
|
||||
Source: codersdk.GroupSource(row.Group.Source),
|
||||
OrganizationName: row.OrganizationName,
|
||||
OrganizationDisplayName: row.OrganizationDisplayName,
|
||||
}
|
||||
}
|
||||
|
||||
func TemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) {
|
||||
// Use a stable sort, similarly to how we would sort in the query, note that
|
||||
// we don't sort in the query because order varies depending on the table
|
||||
|
||||
@@ -3901,6 +3901,17 @@ func (q *querier) GetGroups(ctx context.Context, arg database.GetGroupsParams) (
|
||||
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetGroups)(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
// Required to have permission to read all groups in the organization. This
|
||||
// mirrors PaginatedOrganizationMembers: a single org-wide read check with no
|
||||
// per-row post-filter, so that SQL LIMIT/OFFSET and COUNT(*) OVER() stay
|
||||
// consistent across pages.
|
||||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceGroup.InOrg(arg.OrganizationID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return q.db.GetGroupsByOrganizationIDPaginated(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
// No authz checks
|
||||
return q.db.GetHealthSettings(ctx)
|
||||
|
||||
@@ -2353,6 +2353,19 @@ func (s *MethodTestSuite) TestOrganization() {
|
||||
|
||||
check.Args(arg).Asserts(mem, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetGroupsByOrganizationIDPaginated", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
o := testutil.Fake(s.T(), faker, database.Organization{})
|
||||
g := testutil.Fake(s.T(), faker, database.Group{OrganizationID: o.ID})
|
||||
arg := database.GetGroupsByOrganizationIDPaginatedParams{OrganizationID: o.ID, LimitOpt: 0}
|
||||
rows := []database.GetGroupsByOrganizationIDPaginatedRow{{
|
||||
Group: g,
|
||||
OrganizationName: o.Name,
|
||||
OrganizationDisplayName: o.DisplayName,
|
||||
Count: 1,
|
||||
}}
|
||||
dbm.EXPECT().GetGroupsByOrganizationIDPaginated(gomock.Any(), arg).Return(rows, nil).AnyTimes()
|
||||
check.Args(arg).Asserts(rbac.ResourceGroup.InOrg(o.ID), policy.ActionRead).Returns(rows)
|
||||
}))
|
||||
s.Run("PaginatedOrganizationMembers", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
|
||||
o := testutil.Fake(s.T(), faker, database.Organization{})
|
||||
u := testutil.Fake(s.T(), faker, database.User{})
|
||||
|
||||
+8
@@ -2153,6 +2153,14 @@ func (m queryMetricsStore) GetGroups(ctx context.Context, arg database.GetGroups
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetGroupsByOrganizationIDPaginated(ctx, arg)
|
||||
m.queryLatencies.WithLabelValues("GetGroupsByOrganizationIDPaginated").Observe(time.Since(start).Seconds())
|
||||
m.queryCounts.WithLabelValues(httpmw.ExtractHTTPRoute(ctx), httpmw.ExtractHTTPMethod(ctx), "GetGroupsByOrganizationIDPaginated").Inc()
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetHealthSettings(ctx)
|
||||
|
||||
Generated
+15
@@ -3990,6 +3990,21 @@ func (mr *MockStoreMockRecorder) GetGroups(ctx, arg any) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroups", reflect.TypeOf((*MockStore)(nil).GetGroups), ctx, arg)
|
||||
}
|
||||
|
||||
// GetGroupsByOrganizationIDPaginated mocks base method.
|
||||
func (m *MockStore) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg database.GetGroupsByOrganizationIDPaginatedParams) ([]database.GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetGroupsByOrganizationIDPaginated", ctx, arg)
|
||||
ret0, _ := ret[0].([]database.GetGroupsByOrganizationIDPaginatedRow)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetGroupsByOrganizationIDPaginated indicates an expected call of GetGroupsByOrganizationIDPaginated.
|
||||
func (mr *MockStoreMockRecorder) GetGroupsByOrganizationIDPaginated(ctx, arg any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroupsByOrganizationIDPaginated", reflect.TypeOf((*MockStore)(nil).GetGroupsByOrganizationIDPaginated), ctx, arg)
|
||||
}
|
||||
|
||||
// GetHealthSettings mocks base method.
|
||||
func (m *MockStore) GetHealthSettings(ctx context.Context) (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
||||
Generated
+1
@@ -624,6 +624,7 @@ type sqlcQuerier interface {
|
||||
GetGroupMembersCountByGroupIDs(ctx context.Context, arg GetGroupMembersCountByGroupIDsParams) ([]GetGroupMembersCountByGroupIDsRow, error)
|
||||
// A limit of 0 means "no limit".
|
||||
GetGroups(ctx context.Context, arg GetGroupsParams) ([]GetGroupsRow, error)
|
||||
GetGroupsByOrganizationIDPaginated(ctx context.Context, arg GetGroupsByOrganizationIDPaginatedParams) ([]GetGroupsByOrganizationIDPaginatedRow, error)
|
||||
GetHealthSettings(ctx context.Context) (string, error)
|
||||
// Returns the highest group AI budget across the groups the user belongs to,
|
||||
// breaking ties by the earliest organization membership. Implements the
|
||||
|
||||
Generated
+95
@@ -15125,6 +15125,101 @@ func (q *sqlQuerier) GetGroups(ctx context.Context, arg GetGroupsParams) ([]GetG
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getGroupsByOrganizationIDPaginated = `-- name: GetGroupsByOrganizationIDPaginated :many
|
||||
SELECT
|
||||
groups.id, groups.name, groups.organization_id, groups.avatar_url, groups.quota_allowance, groups.display_name, groups.source, groups.chat_spend_limit_micros,
|
||||
organizations.name AS organization_name,
|
||||
organizations.display_name AS organization_display_name,
|
||||
COUNT(*) OVER() AS count
|
||||
FROM
|
||||
groups
|
||||
INNER JOIN
|
||||
organizations ON groups.organization_id = organizations.id
|
||||
WHERE
|
||||
true
|
||||
AND groups.organization_id = $1
|
||||
-- Keyset pagination cursor. When @after_id is set, return only groups
|
||||
-- ordered after it, matching the ORDER BY (LOWER(name), id) below. This
|
||||
-- lets callers page without duplicated or skipped rows even if groups are
|
||||
-- inserted or deleted between page requests.
|
||||
AND CASE
|
||||
WHEN $2 :: uuid != '00000000-0000-0000-0000-000000000000' :: uuid THEN
|
||||
(LOWER(groups.name), groups.id) > (
|
||||
SELECT LOWER(name), id FROM groups WHERE id = $2
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Filter by group name or display name (substring, case-insensitive).
|
||||
AND CASE WHEN $3 :: text != '' THEN (
|
||||
groups.name ILIKE concat('%', $3, '%')
|
||||
OR groups.display_name ILIKE concat('%', $3, '%')
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
ORDER BY
|
||||
-- Deterministic and consistent ordering of all groups. This is to ensure consistent pagination.
|
||||
LOWER(groups.name) ASC, groups.id ASC OFFSET $4
|
||||
LIMIT
|
||||
-- A null limit means "no limit", so 0 means return all
|
||||
NULLIF($5 :: int, 0)
|
||||
`
|
||||
|
||||
type GetGroupsByOrganizationIDPaginatedParams struct {
|
||||
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
|
||||
AfterID uuid.UUID `db:"after_id" json:"after_id"`
|
||||
Search string `db:"search" json:"search"`
|
||||
OffsetOpt int32 `db:"offset_opt" json:"offset_opt"`
|
||||
LimitOpt int32 `db:"limit_opt" json:"limit_opt"`
|
||||
}
|
||||
|
||||
type GetGroupsByOrganizationIDPaginatedRow struct {
|
||||
Group Group `db:"group" json:"group"`
|
||||
OrganizationName string `db:"organization_name" json:"organization_name"`
|
||||
OrganizationDisplayName string `db:"organization_display_name" json:"organization_display_name"`
|
||||
Count int64 `db:"count" json:"count"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetGroupsByOrganizationIDPaginated(ctx context.Context, arg GetGroupsByOrganizationIDPaginatedParams) ([]GetGroupsByOrganizationIDPaginatedRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getGroupsByOrganizationIDPaginated,
|
||||
arg.OrganizationID,
|
||||
arg.AfterID,
|
||||
arg.Search,
|
||||
arg.OffsetOpt,
|
||||
arg.LimitOpt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetGroupsByOrganizationIDPaginatedRow
|
||||
for rows.Next() {
|
||||
var i GetGroupsByOrganizationIDPaginatedRow
|
||||
if err := rows.Scan(
|
||||
&i.Group.ID,
|
||||
&i.Group.Name,
|
||||
&i.Group.OrganizationID,
|
||||
&i.Group.AvatarURL,
|
||||
&i.Group.QuotaAllowance,
|
||||
&i.Group.DisplayName,
|
||||
&i.Group.Source,
|
||||
&i.Group.ChatSpendLimitMicros,
|
||||
&i.OrganizationName,
|
||||
&i.OrganizationDisplayName,
|
||||
&i.Count,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertAllUsersGroup = `-- name: InsertAllUsersGroup :one
|
||||
INSERT INTO groups (
|
||||
id,
|
||||
|
||||
@@ -91,6 +91,44 @@ WHERE
|
||||
LIMIT NULLIF(@limit_opt :: int, 0)
|
||||
;
|
||||
|
||||
-- name: GetGroupsByOrganizationIDPaginated :many
|
||||
SELECT
|
||||
sqlc.embed(groups),
|
||||
organizations.name AS organization_name,
|
||||
organizations.display_name AS organization_display_name,
|
||||
COUNT(*) OVER() AS count
|
||||
FROM
|
||||
groups
|
||||
INNER JOIN
|
||||
organizations ON groups.organization_id = organizations.id
|
||||
WHERE
|
||||
true
|
||||
AND groups.organization_id = @organization_id
|
||||
-- Keyset pagination cursor. When @after_id is set, return only groups
|
||||
-- ordered after it, matching the ORDER BY (LOWER(name), id) below. This
|
||||
-- lets callers page without duplicated or skipped rows even if groups are
|
||||
-- inserted or deleted between page requests.
|
||||
AND CASE
|
||||
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000' :: uuid THEN
|
||||
(LOWER(groups.name), groups.id) > (
|
||||
SELECT LOWER(name), id FROM groups WHERE id = @after_id
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
-- Filter by group name or display name (substring, case-insensitive).
|
||||
AND CASE WHEN @search :: text != '' THEN (
|
||||
groups.name ILIKE concat('%', @search, '%')
|
||||
OR groups.display_name ILIKE concat('%', @search, '%')
|
||||
)
|
||||
ELSE true
|
||||
END
|
||||
ORDER BY
|
||||
-- Deterministic and consistent ordering of all groups. This is to ensure consistent pagination.
|
||||
LOWER(groups.name) ASC, groups.id ASC OFFSET @offset_opt
|
||||
LIMIT
|
||||
-- A null limit means "no limit", so 0 means return all
|
||||
NULLIF(@limit_opt :: int, 0);
|
||||
|
||||
-- name: InsertGroup :one
|
||||
INSERT INTO groups (
|
||||
id,
|
||||
|
||||
@@ -174,6 +174,30 @@ func Users(query string) (database.GetUsersParams, []codersdk.ValidationError) {
|
||||
return filter, parser.Errors
|
||||
}
|
||||
|
||||
// Groups parses a group search query using the standard filter syntax shared
|
||||
// with the rest of the dashboard. Bare terms (including multi-word terms)
|
||||
// become a free-text search over group name and display name. A value that
|
||||
// contains a colon must be quoted or supplied via the explicit search key,
|
||||
// e.g. search:"team: frontend", because an unquoted colon is otherwise treated
|
||||
// as a key:value delimiter. Unknown keys are rejected, which keeps room for
|
||||
// real key:value filters in the future.
|
||||
func Groups(query string) (string, []codersdk.ValidationError) {
|
||||
// Always lowercase for all searches.
|
||||
query = strings.ToLower(query)
|
||||
values, errors := searchTerms(query, func(term string, values url.Values) error {
|
||||
values.Add("search", term)
|
||||
return nil
|
||||
})
|
||||
if len(errors) > 0 {
|
||||
return "", errors
|
||||
}
|
||||
|
||||
parser := httpapi.NewQueryParamParser()
|
||||
search := parser.String(values, "", "search")
|
||||
parser.ErrorExcessParams(values)
|
||||
return search, parser.Errors
|
||||
}
|
||||
|
||||
func Members(query string, organizationID uuid.UUID) (database.OrganizationMembersParams, []codersdk.ValidationError) {
|
||||
query = strings.TrimSpace(query)
|
||||
if query == "" {
|
||||
|
||||
@@ -1753,3 +1753,94 @@ func TestSearchChats(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchGroups(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []struct {
|
||||
Name string
|
||||
Query string
|
||||
Expected string
|
||||
ExpectedErrorContains string
|
||||
}{
|
||||
{
|
||||
Name: "Empty",
|
||||
Query: "",
|
||||
Expected: "",
|
||||
},
|
||||
{
|
||||
Name: "SingleWord",
|
||||
Query: "alpha",
|
||||
Expected: "alpha",
|
||||
},
|
||||
{
|
||||
// Groups support free-text search, so an unquoted multi-word query
|
||||
// is joined into a single search value instead of being rejected as
|
||||
// a duplicate param.
|
||||
Name: "MultiWord",
|
||||
Query: "front end",
|
||||
Expected: "front end",
|
||||
},
|
||||
{
|
||||
Name: "CaseInsensitive",
|
||||
Query: "AlPhA",
|
||||
Expected: "alpha",
|
||||
},
|
||||
{
|
||||
Name: "MultiWordCaseInsensitive",
|
||||
Query: "Front End",
|
||||
Expected: "front end",
|
||||
},
|
||||
{
|
||||
Name: "TrimsSurroundingSpaces",
|
||||
Query: " alpha ",
|
||||
Expected: "alpha",
|
||||
},
|
||||
{
|
||||
// Structured key:value queries are not supported for groups; the
|
||||
// unrecognized key surfaces as an invalid query param. Rejecting
|
||||
// unknown keys leaves room for real key:value filters later.
|
||||
Name: "StructuredKeyValueRejected",
|
||||
Query: "name:alpha",
|
||||
ExpectedErrorContains: "is not a valid query param",
|
||||
},
|
||||
{
|
||||
// The explicit search key is supported.
|
||||
Name: "SearchKey",
|
||||
Query: "search:alpha",
|
||||
Expected: "alpha",
|
||||
},
|
||||
{
|
||||
// A colon-containing name is searchable when quoted via the search
|
||||
// key, since group display names may legally contain colons.
|
||||
Name: "QuotedColonValue",
|
||||
Query: `search:"team: frontend"`,
|
||||
Expected: "team: frontend",
|
||||
},
|
||||
{
|
||||
// An unquoted colon is treated as a key:value delimiter, so a bare
|
||||
// colon term is rejected. Users must quote it (see QuotedColonValue).
|
||||
Name: "BareColonRejected",
|
||||
Query: "team: frontend",
|
||||
ExpectedErrorContains: "cannot start or end with ':'",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range testCases {
|
||||
t.Run(c.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
search, errs := searchquery.Groups(c.Query)
|
||||
if c.ExpectedErrorContains != "" {
|
||||
require.True(t, len(errs) > 0, "expect some errors")
|
||||
var s strings.Builder
|
||||
for _, err := range errs {
|
||||
_, _ = s.WriteString(fmt.Sprintf("%s: %s\n", err.Field, err.Detail))
|
||||
}
|
||||
require.Contains(t, s.String(), c.ExpectedErrorContains)
|
||||
} else {
|
||||
require.Len(t, errs, 0, "expected no error")
|
||||
require.Equal(t, c.Expected, search, "expected search value")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user