Skip to content
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
20 changes: 19 additions & 1 deletion packages/cli/src/commands/workspaces/list/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { boolean, CLIError, string, table } from "@superset/cli-framework";
import { command } from "../../../lib/command";
import { resolveHostFilter } from "../../../lib/host-target";

const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

export default command({
description: "List workspaces accessible to you in the active organization",
options: {
host: string().desc("Filter to a specific host (machineId)"),
local: boolean().desc("Filter to this machine"),
project: string().desc("Filter by project name (case-insensitive) or id"),
search: string()
.alias("s")
.desc("Search by workspace name or branch substring"),
},
display: (data) =>
table(
Expand All @@ -26,8 +33,19 @@ export default command({
local: options.local ?? undefined,
});

const projectInput = options.project ?? undefined;
const projectId =
projectInput && UUID_RE.test(projectInput) ? projectInput : undefined;
const projectName = projectInput && !projectId ? projectInput : undefined;

const [workspaces, hosts] = await Promise.all([
ctx.api.v2Workspace.list.query({ organizationId, hostId }),
ctx.api.v2Workspace.list.query({
organizationId,
hostId,
projectId,
projectName,
search: options.search ?? undefined,
}),
ctx.api.host.list.query({ organizationId }),
]);
const hostNameById = new Map(hosts.map((host) => [host.id, host.name]));
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/resources/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export type WorkspaceListResponse = Array<Workspace>;
export interface WorkspaceListParams {
/** Restrict the listing to workspaces on a single host machineId. */
hostId?: string;
/** Restrict the listing to a single project by UUID. */
projectId?: string;
/** Restrict the listing by project name (case-insensitive exact match). */
projectName?: string;
/** Substring match against workspace name or branch. */
search?: string;
}

export interface WorkspaceCreateParams {
Expand Down
24 changes: 23 additions & 1 deletion packages/trpc/src/router/v2-workspace/v2-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { getCurrentTxid } from "@superset/db/utils";
import type { TRPCRouterRecord } from "@trpc/server";
import { TRPCError } from "@trpc/server";
import { and, eq, inArray } from "drizzle-orm";
import { and, eq, ilike, inArray, or, sql } from "drizzle-orm";
import { z } from "zod";
import { posthog } from "../../lib/analytics";
import { jwtProcedure, protectedProcedure } from "../../trpc";
Expand Down Expand Up @@ -114,6 +114,9 @@ export const v2WorkspaceRouter = {
z.object({
organizationId: z.string().uuid(),
hostId: z.string().min(1).optional(),
projectId: z.string().uuid().optional(),
projectName: z.string().min(1).optional(),
search: z.string().min(1).optional(),
}),
)
.query(async ({ ctx, input }) => {
Expand All @@ -124,6 +127,18 @@ export const v2WorkspaceRouter = {
});
}

const escapeLike = (value: string) =>
value.replace(/[\\%_]/g, (char) => `\\${char}`);
const searchPattern = input.search
? `%${escapeLike(input.search)}%`
: null;
const searchMatch = searchPattern
? or(
ilike(v2Workspaces.name, searchPattern),
ilike(v2Workspaces.branch, searchPattern),
)
: undefined;

const rows = await db
.select({
id: v2Workspaces.id,
Expand All @@ -147,6 +162,13 @@ export const v2WorkspaceRouter = {
eq(v2Workspaces.organizationId, input.organizationId),
eq(v2UsersHosts.userId, ctx.userId),
input.hostId ? eq(v2Workspaces.hostId, input.hostId) : undefined,
input.projectId
? eq(v2Workspaces.projectId, input.projectId)
: undefined,
input.projectName
? sql`lower(${v2Projects.name}) = lower(${input.projectName})`
: undefined,
searchMatch,
),
);

Expand Down
Loading