Skip to content

feat: getWorkspaces filter site api #1564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (api *api) workspaces(rw http.ResponseWriter, r *http.Request) {

// Empty strings mean no filter
orgFilter := r.URL.Query().Get("organization_id")
ownerFilter := r.URL.Query().Get("owner_id")
ownerFilter := r.URL.Query().Get("owner")

filter := database.GetWorkspacesWithFilterParams{Deleted: false}
if orgFilter != "" {
Expand Down
2 changes: 1 addition & 1 deletion codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (f WorkspaceFilter) asRequestOption() requestOption {
q.Set("organization_id", f.OrganizationID.String())
}
if f.Owner != "" {
q.Set("owner_id", f.Owner)
q.Set("owner", f.Owner)
}
r.URL.RawQuery = q.Encode()
}
Expand Down
15 changes: 14 additions & 1 deletion site/src/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios"
import { getApiKey, login, logout } from "./api"
import { getApiKey, getWorkspacesURL, login, logout } from "./api"
import * as TypesGen from "./typesGenerated"

describe("api.ts", () => {
Expand Down Expand Up @@ -113,4 +113,17 @@ describe("api.ts", () => {
}
})
})

describe("getWorkspacesURL", () => {
it.each<[TypesGen.WorkspaceFilter | undefined, string]>([
[undefined, "/api/v2/workspaces"],

[{ OrganizationID: "1", Owner: "" }, "/api/v2/workspaces?organization_id=1"],
[{ OrganizationID: "", Owner: "1" }, "/api/v2/workspaces?owner=1"],

[{ OrganizationID: "1", Owner: "me" }, "/api/v2/workspaces?organization_id=1&owner=me"],
])(`getWorkspacesURL(%p) returns %p`, (filter, expected) => {
expect(getWorkspacesURL(filter)).toBe(expected)
})
})
})
23 changes: 19 additions & 4 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,25 @@ export const getWorkspace = async (workspaceId: string): Promise<TypesGen.Worksp
return response.data
}

// TODO: @emyrk add query params as arguments. Supports 'organization_id' and 'owner'
// 'owner' can be a username, user_id, or 'me'
export const getWorkspaces = async (): Promise<TypesGen.Workspace[]> => {
const response = await axios.get<TypesGen.Workspace[]>(`/api/v2/workspaces`)
export const getWorkspacesURL = (filter?: TypesGen.WorkspaceFilter): string => {
const basePath = "/api/v2/workspaces"
const searchParams = new URLSearchParams()

if (filter?.OrganizationID) {
searchParams.append("organization_id", filter.OrganizationID)
}
if (filter?.Owner) {
searchParams.append("owner", filter.Owner)
}

const searchString = searchParams.toString()

return searchString ? `${basePath}?${searchString}` : basePath
}

export const getWorkspaces = async (filter?: TypesGen.WorkspaceFilter): Promise<TypesGen.Workspace[]> => {
const url = getWorkspacesURL(filter)
const response = await axios.get<TypesGen.Workspace[]>(url)
return response.data
}

Expand Down
9 changes: 6 additions & 3 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ export const handlers = [
rest.post("/api/v2/users/me/workspaces", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockWorkspace))
}),
rest.get("/api/v2/workspaces", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockWorkspace]))
}),
rest.get("/api/v2/users/me/organizations", (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockOrganization]))
}),
Expand Down Expand Up @@ -79,6 +76,12 @@ export const handlers = [
}),

// workspaces

// REMARK: This endpoint works with query parameters, but they won't be
// reflected in the return.
rest.get("/api/v2/workspaces", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json([M.MockWorkspace]))
}),
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {
if (req.params.workspaceName !== M.MockWorkspace.name) {
return res(
Expand Down