MCPcopy Create free account
hub / github.com/bytebase/bytebase / ListGroups

Method ListGroups

backend/store/group.go:99–197  ·  view source on GitHub ↗

ListGroups list all groups.

(ctx context.Context, find *FindGroupMessage)

Source from the content-addressed store, hash-verified

97
98// ListGroups list all groups.
99func (s *Store) ListGroups(ctx context.Context, find *FindGroupMessage) ([]*GroupMessage, error) {
100 with := qb.Q()
101 from := qb.Q().Space("user_group")
102 where := qb.Q().Space("user_group.workspace = ?", find.Workspace)
103
104 // Build CTE for project filtering if needed
105 if v := find.ProjectID; v != nil {
106 with.Space(`WITH all_members AS (
107 SELECT
108 jsonb_array_elements_text(jsonb_array_elements(policy.payload->'bindings')->'members') AS member,
109 jsonb_array_elements(policy.payload->'bindings')->>'role' AS role
110 FROM policy
111 WHERE ((resource_type = ? AND resource = ?) OR resource_type = ?) AND type = ? AND policy.workspace = ?
112 ),
113 project_members AS (
114 SELECT ARRAY_AGG(member) AS members FROM all_members WHERE role NOT LIKE 'roles/workspace%'
115 )`, storepb.Policy_PROJECT.String(), "projects/"+*v, storepb.Policy_WORKSPACE.String(), storepb.Policy_IAM.String(), find.Workspace)
116 from.Space("INNER JOIN project_members ON ?", projectMembersGroupJoinCondition())
117 }
118
119 if filterQ := find.FilterQ; filterQ != nil {
120 where.And("?", filterQ)
121 }
122 if v := find.ID; v != nil {
123 where.And("id = ?", *v)
124 }
125 if v := find.Email; v != nil {
126 where.And("email = ?", *v)
127 }
128
129 q := qb.Q()
130 if with.Len() > 0 {
131 q.Space("?", with)
132 }
133 q.Space(`
134 SELECT
135 user_group.id,
136 user_group.workspace,
137 user_group.email,
138 user_group.name,
139 user_group.description,
140 user_group.payload
141 FROM ?
142 WHERE ?
143 ORDER BY email
144 `, from, where)
145
146 if v := find.Limit; v != nil {
147 q.Space("LIMIT ?", *v)
148 }
149 if v := find.Offset; v != nil {
150 q.Space("OFFSET ?", *v)
151 }
152
153 query, args, err := q.ToSQL()
154 if err != nil {
155 return nil, errors.Wrapf(err, "failed to build sql")
156 }

Callers 2

GetGroupMethod · 0.95
GetUserGroupsSnapshotMethod · 0.95

Calls 14

GetDBMethod · 0.95
QFunction · 0.92
getGroupCacheKeyFunction · 0.85
SpaceMethod · 0.80
AndMethod · 0.80
LenMethod · 0.80
ToSQLMethod · 0.80
QueryContextMethod · 0.80
ScanMethod · 0.80
UnmarshalMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected