MCPcopy Index your code
hub / github.com/coder/coder / workspaces

Method workspaces

coderd/workspaces.go:151–262  ·  view source on GitHub ↗

workspaces returns all workspaces a user can read. Optional filters with query params @Summary List workspaces @ID list-workspaces @Security CoderSessionToken @Produce json @Tags Workspaces @Param q query string false "Search query in the format `key:value`. Available keys are: owner, template, nam

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

149// @Success 200 {object} codersdk.WorkspacesResponse
150// @Router /api/v2/workspaces [get]
151func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
152 ctx := r.Context()
153 apiKey := httpmw.APIKey(r)
154
155 page, ok := ParsePagination(rw, r)
156 if !ok {
157 return
158 }
159
160 queryStr := r.URL.Query().Get("q")
161 filter, errs := searchquery.Workspaces(ctx, api.Database, queryStr, page, api.AgentInactiveDisconnectTimeout)
162 if len(errs) > 0 {
163 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
164 Message: "Invalid workspace search query.",
165 Validations: errs,
166 })
167 return
168 }
169
170 if filter.OwnerUsername == "me" {
171 filter.OwnerID = apiKey.UserID
172 filter.OwnerUsername = ""
173 }
174
175 prepared, err := api.HTTPAuth.AuthorizeSQLFilter(r, policy.ActionRead, rbac.ResourceWorkspace.Type)
176 if err != nil {
177 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
178 Message: "Internal error preparing sql filter.",
179 Detail: err.Error(),
180 })
181 return
182 }
183
184 // To show the requester's favorite workspaces first, we pass their userID and compare it to
185 // the workspace owner_id when ordering the rows.
186 filter.RequesterID = apiKey.UserID
187
188 // We need the technical row to present the correct count on every page.
189 filter.WithSummary = true
190
191 workspaceRows, err := api.Database.GetAuthorizedWorkspaces(ctx, filter, prepared)
192 if err != nil {
193 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
194 Message: "Internal error fetching workspaces.",
195 Detail: err.Error(),
196 })
197 return
198 }
199
200 if len(workspaceRows) == 0 {
201 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
202 Message: "Internal error fetching workspaces.",
203 Detail: "Workspace summary row is missing.",
204 })
205 return
206 }
207 if len(workspaceRows) == 1 {
208 httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspacesResponse{

Callers

nothing calls this directly

Calls 12

workspaceDataMethod · 0.95
APIKeyFunction · 0.92
WorkspacesFunction · 0.92
WriteFunction · 0.92
ConvertWorkspaceRowsFunction · 0.92
ParsePaginationFunction · 0.85
convertWorkspacesFunction · 0.85
AuthorizeSQLFilterMethod · 0.80
ContextMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected