Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: masking exemption ux and cel parser (#21609)
ecmadao751afb269e05
diff --git a/frontend/src/components/ExprEditor.test.tsx b/frontend/src/components/ExprEditor.test.tsxindex e171738af2..5e0faccecd 100644--- a/frontend/src/components/ExprEditor.test.tsx+++ b/frontend/src/components/ExprEditor.test.tsx@@ -788,4 +788,44 @@ describe("ExprEditor", () => {unmount();});++ test("shows the select placeholder for an empty static option value", async () => {+ const initialExpr: ConditionGroupExpr = {+ type: ExprType.ConditionGroup,+ operator: "_&&_",+ args: [+ {+ type: ExprType.Condition,+ operator: "_==_",+ args: [CEL_ATTRIBUTE_RESOURCE_ENVIRONMENT_ID, ""],+ },+ ],+ };+ const environmentOptionConfig = new Map<Factor, OptionConfig>([+ [+ CEL_ATTRIBUTE_RESOURCE_ENVIRONMENT_ID,+ { options: [{ value: "test", label: "Test" }] },+ ],+ ]);++ const { container, unmount } = renderIntoContainer(+ <ExprEditor+ expr={initialExpr}+ factorList={[CEL_ATTRIBUTE_RESOURCE_ENVIRONMENT_ID]}+ optionConfigMap={environmentOptionConfig}+ onUpdate={() => {}}+ />+ );+ await flushEffects();++ const placeholder = Array.from(container.querySelectorAll("span")).find(+ (span) =>+ span.textContent === "cel.condition.select-value" &&+ span.className.includes("text-control-placeholder")+ );++ expect(placeholder).toBeInstanceOf(HTMLSpanElement);++ unmount();+ });});diff --git a/frontend/src/components/ExprEditor.tsx b/frontend/src/components/ExprEditor.tsxindex f0b4603d76..45bc1d4fc2 100644--- a/frontend/src/components/ExprEditor.tsx+++ b/frontend/src/components/ExprEditor.tsx@@ -1172,7 +1172,13 @@ function ValueInput({<SelectTrigger className="w-full"><SelectValue placeholder={t("cel.condition.select-value")}>{(value: string | null) => {- if (!value) return null;+ if (!value) {+ return (+ <span className="text-control-placeholder">+ {t("cel.condition.select-value")}+ </span>+ );+ }const found = optionConfig.options.find((o) => o.value === value);return renderOptionValue(value, found?.label);}}diff --git a/frontend/src/lib/sensitive-data/exemptionDataUtils.test.ts b/frontend/src/lib/sensitive-data/exemptionDataUtils.test.tsindex e1a2494351..b0320c31b9 100644--- a/frontend/src/lib/sensitive-data/exemptionDataUtils.test.ts+++ b/frontend/src/lib/sensitive-data/exemptionDataUtils.test.ts@@ -7,12 +7,25 @@ vi.mock("@/lib/i18n", () => ({t: (key: string, params?: Record<string, unknown>) => {const map: Record<string, string> = {"database.all": "All databases",+ "database.columns": "Columns","common.schema": "Schema","common.table": "Table","project.masking-exemption.level": "level",};if (key === "common.n-more" && params?.n !== undefined)return `+${params.n} more`;+ if (+ key === "project.masking-exemption.n-exemptions" &&+ params?.n !== undefined+ ) {+ return `${params.n} exemption${params.n === 1 ? "" : "s"}`;+ }+ if (+ key === "project.masking-exemption.n-databases" &&+ params?.n !== undefined+ ) {+ return `${params.n} database${params.n === 1 ? "" : "s"}`;+ }return map[key] ?? key;},},@@ -20,10 +33,12 @@ vi.mock("@/lib/i18n", () => ({import {generateGrantTitle,+ generateMemberScopeSummary,getConditionExpression,groupByMember,parseClassificationLevel,parseExpirationTimestamp,+ resourceCoversDatabase,rewriteResourceDatabase,} from "./exemptionDataUtils";import type { AccessUser, ExemptionGrant } from "./types";@@ -165,6 +180,26 @@ describe("parseExpirationTimestamp", () => {});});+describe("generateMemberScopeSummary", () => {+ test("shows the database name only for a single database", () => {+ expect(generateMemberScopeSummary(3, ["bytebase_210"])).toBe(+ "3 exemptions · bytebase_210"+ );+ });++ test("shows a database count for multiple databases", () => {+ expect(+ generateMemberScopeSummary(3, ["bytebase_210", "bytebase_211"])+ ).toBe("3 exemptions · 2 databases");+ });++ test("uses all databases when any grant has global scope", () => {+ expect(generateMemberScopeSummary(3, ["", "bytebase_210"])).toBe(+ "3 exemptions · All databases"+ );+ });+});+describe("groupByMember", () => {const makeAccessUser = (overrides: Partial<AccessUser>): AccessUser => ({type: "user",@@ -589,6 +624,18 @@ describe("generateGrantTitle", () => {).toBe("All databases, level ≤ 3");});+ test("no database, with columns", () => {+ expect(+ generateGrantTitle(+ makeGrant({+ databaseResources: [+ { databaseFullName: "", columns: ["email", "phone"] },+ ],+ })+ )+ ).toBe("All databases (columns: email, phone)");+ });+// --- Multiple databases ---test("2 databases", () => {@@ -984,6 +1031,32 @@ describe("generateGrantTitle", () => {});});+describe("resourceCoversDatabase", () => {+ test("a column-only resource covers every database", () => {+ expect(+ resourceCoversDatabase(+ { databaseFullName: "", columns: ["email"] },+ "instances/prod/databases/hr"+ )+ ).toBe(true);+ });++ test("an instance-only resource covers only that instance", () => {+ expect(+ resourceCoversDatabase(+ { databaseFullName: "", instanceResourceId: "prod" },+ "instances/prod/databases/hr"+ )+ ).toBe(true);+ expect(+ resourceCoversDatabase(+ { databaseFullName: "", instanceResourceId: "prod" },+ "instances/dev/databases/hr"+ )+ ).toBe(false);+ });+});+describe("rewriteResourceDatabase", () => {test("rewrites single resource.database condition", () => {const input =diff --git a/frontend/src/lib/sensitive-data/exemptionDataUtils.ts b/frontend/src/lib/sensitive-data/exemptionDataUtils.tsindex 9b82a25c37..3fc57e5dc8 100644--- a/frontend/src/lib/sensitive-data/exemptionDataUtils.ts+++ b/frontend/src/lib/sensitive-data/exemptionDataUtils.ts@@ -1,4 +1,5 @@import i18n from "@/lib/i18n";+import type { DatabaseResource } from "@/types/member";import { celString } from "@/utils/v1/celLiteral";import type {AccessUser,@@ -81,9 +82,15 @@ export function generateGrantTitle(grant: ExemptionGrant): string {return dbName !== "";});- // No specific databasesif (realResources.length === 0) {- return `${i18n.t("database.all")}${levelSuffix}`;+ const columns = [+ ...new Set(resources.flatMap((resource) => resource.columns ?? [])),+ ];+ const columnSuffix =+ columns.length > 0+ ? ` (${i18n.t("database.columns").toLowerCase()}: ${listItems(columns)})`+ : "";+ return `${i18n.t("database.all")}${columnSuffix}${levelSuffix}`;}// Collect unique database names@@ -171,6 +178,22 @@ export function generateGrantTitle(grant: ExemptionGrant): string {return `${dbName} (${table}: ${listItems(columns)})${levelSuffix}`;}+export function resourceCoversDatabase(+ resource: DatabaseResource,+ databaseFullName: string+): boolean {+ if (resource.databaseFullName !== "") {+ return resource.databaseFullName === databaseFullName;+ }+ const target = databaseFullName.match(databaseResourcePattern)?.groups;+ return (+ (!resource.instanceResourceId ||+ resource.instanceResourceId === target?.instanceName) &&+ (!resource.databaseResourceId ||+ resource.databaseResourceId === target?.databaseName)+ );+}+// Rewrite `resource.database == "instances/X/databases/Y"` to// `resource.instance_id == "X" && resource.database_name == "Y"`.// The expression editor uses resource.database as a combined selector,@@ -219,6 +242,28 @@ export function buildMemberSummary(grants: ExemptionGrant[]): {};}+export function generateMemberScopeSummary(+ grantCount: number,+ databaseNames: string[]+): string {+ const exemptionCount = i18n.t("project.masking-exemption.n-exemptions", {+ count: grantCount,+ n: grantCount,+ });+ const uniqueDatabases = [...new Set(databaseNames)];+ if (uniqueDatabases.includes("")) {+ return `${exemptionCount} · ${i18n.t("database.all")}`;+ }+ if (uniqueDatabases.length === 1) {+ return `${exemptionCount} · ${uniqueDatabases[0]}`;+ }+ if (uniqueDatabases.length > 1) {+ const databaseCount = uniqueDatabases.length;+ return `${exemptionCount} · ${i18n.t("project.masking-exemption.n-databases", { count: databaseCount, n: databaseCount })}`;+ }+ return exemptionCount;+}+export function groupByMember(accessUsers: AccessUser[]): ExemptionMember[] {// Track each member's entries and the index of their last (most recent) entryconst memberMap = new Map<diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex f598134234..940140f0fe 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2064,13 +2064,17 @@"expires-in-days_other": "Expires in {{days}} days","expires-today": "Expires today","grant-exemption": "Grant Exemption",+ "hide-raw-cel-condition": "Hide raw CEL condition","level": "level",+ "n-databases_one": "{{n}} database",+ "n-databases_other": "{{n}} databases","n-exemptions_one": "{{n}} exemption","n-exemptions_other": "{{n}} exemptions","no-exemptions": "No masking exemptions granted yet","no-reason": "No reason provided","revoke-exemption-title": "Remove masking exemption for {{member}}?",- "self": "Masking Exemptions"+ "self": "Masking Exemptions",+ "show-raw-cel-condition": "Show raw CEL condition"},"members": {"description": "Project members determine the permission to operate database and issues inside the project. Project Owner, Workspace Admin and Workspace DBA all have full project permissions.",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 153dc87fed..79fb05df44 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2064,13 +2064,17 @@"expires-in-days_other": "Expira en {{days}} días","expires-today": "Expira hoy","grant-exemption": "Conceder Exención",+ "hide-raw-cel-condition": "Ocultar condición CEL sin procesar","level": "nivel",+ "n-databases_one": "{{n}} base de datos",+ "n-databases_other": "{{n}} bases de datos","n-exemptions_one": "{{n}} exención","n-exemptions_other": "{{n}} exenciones","no-exemptions": "Aún no se han concedido exenciones de enmascaramiento","no-reason": "No se proporcionó motivo","revoke-exemption-title": "¿Eliminar la exención de enmascaramiento para {{member}}?",- "self": "Exenciones de Enmascaramiento"+ "self": "Exenciones de Enmascaramiento",+ "show-raw-cel-condition": "Mostrar condición CEL sin procesar"},"members": {"description": "Los miembros del proyecto determinan el permiso para operar la base de datos y los problemas dentro del proyecto. El propietario del proyecto, el administrador del espacio de trabajo y el DBA del espacio de trabajo tienen permisos completos para el proyecto.",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 2ea965967e..54b83939b4 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2064,13 +2064,17 @@"expires-in-days_other": "{{days}} 日後に期限切れ","expires-today": "本日期限切れ","grant-exemption": "免除を付与",+ "hide-raw-cel-condition": "生の CEL 条件を非表示","level": "レベル",+ "n-databases_one": "{{n}} 個のデータベース",+ "n-databases_other": "{{n}} 個のデータベース","n-exemptions_one": "{{n}} 件の免除","n-exemptions_other": "{{n}} 件の免除","no-exemptions": "マスキング免除はまだ付与されていません","no-reason": "理由が提供されていません","revoke-exemption-title": "{{member}} のマスキング免除を削除しますか?",- "self": "マスキング免除"+ "self": "マスキング免除",+ "show-raw-cel-condition": "生の CEL 条件を表示"},"members": {"description": "プロジェクトメンバーのロールにより、プロジェクト内のデータベースとイシューを操作するための権限が決まります。プロジェクト所有者、ワークスペース管理者、および DBA はすべて、プロジェクトに対する完全な権限を持っています。",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex e62df251a3..dde3e71812 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -2064,13 +2064,17 @@"expires-in-days_other": "Hết hạn sau {{days}} ngày","expires-today": "Hết hạn hôm nay","grant-exemption": "Cấp Miễn trừ",+ "hide-raw-cel-condition": "Ẩn điều kiện CEL thô","level": "cấp",+ "n-databases_one": "{{n}} cơ sở dữ liệu",+ "n-databases_other": "{{n}} cơ sở dữ liệu","n-exemptions_one": "{{n}} miễn trừ","n-exemptions_other": "{{n}} miễn trừ","no-exemptions": "Chưa có miễn trừ che giấu nào được cấp","no-reason": "Không có lý do được cung cấp","revoke-exemption-title": "Xóa miễn trừ che giấu cho {{member}}?",- "self": "Miễn trừ Che giấu"+ "self": "Miễn trừ Che giấu",+ "show-raw-cel-condition": "Hiện điều kiện CEL thô"},"members": {"description": "Thành viên dự án xác định quyền để vận hành cơ sở dữ liệu và các vấn đề bên trong dự án. Chủ sở hữu dự án, Quản trị viên không gian làm việc và DBA không gian làm việc đều có toàn quyền dự án.",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex ffbd2626f3..ff8af9d38c 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2064,13 +2064,17 @@"expires-in-days_other": "{{days}} 天后到期","expires-today": "今天到期","grant-exemption": "授予豁免",+ "hide-raw-cel-condition": "隐藏原始 CEL 条件","level": "等级",+ "n-databases_one": "{{n}} 个数据库",+ "n-databases_other": "{{n}} 个数据库","n-exemptions_one": "{{n}} 项豁免","n-exemptions_other": "{{n}} 项豁免","no-exemptions": "尚未授予脱敏豁免","no-reason": "未提供原因","revoke-exemption-title": "是否撤销 {{member}} 的脱敏豁免?",- "self": "脱敏豁免"+ "self": "脱敏豁免",+ "show-raw-cel-condition": "显示原始 CEL 条件"},"members": {"description": "项目成员角色决定了操作项目中的数据库和工单的权限。项目的所有者,工作空间管理员和 DBA 都具有项目所有的权限。",diff --git a/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx b/frontend/src/routes/project/ProjectMaskingExemptionPage.tsxindex d5f576fd59..e8380ed18c 100644--- a/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx+++ b/frontend/src/routes/project/ProjectMaskingExemptionPage.tsx@@ -35,9 +35,11 @@ import { useTimeReading } from "@/hooks/useTimeReading";import {buildMemberSummary,generateGrantTitle,+ generateMemberScopeSummary,getConditionExpression,groupByMember,parseExpirationTimestamp,+ resourceCoversDatabase,} from "@/lib/sensitive-data/exemptionDataUtils";import type {AccessUser,@@ -209,7 +211,7 @@ export function ProjectMaskingExemptionPage({const matchesDb = (g: ExemptionGrant) =>!g.databaseResources ||g.databaseResources.length === 0 ||- g.databaseResources.some((r) => r.databaseFullName === dbScope);+ g.databaseResources.some((r) => resourceCoversDatabase(r, dbScope));result = result.map((m) => withFilteredGrants(m, m.grants.filter(matchesDb))).filter((m) => m.grants.length > 0);@@ -882,26 +884,11 @@ function ExemptionMemberItem({);const scopeSummary = useMemo(() => {- const n = member.grants.length;- const exemptionWord = t("project.masking-exemption.n-exemptions", {- n,- count: n,- });-- const realDbs = member.databaseNames.filter((name) => name !== "");- const hasAllDbs = member.databaseNames.includes("");-- const parts: string[] = [];- if (realDbs.length > 0) {- parts.push(realDbs.join(", "));- }- if (hasAllDbs) {- parts.push(t("database.all"));- }-- const scope = parts.join(", ");- return scope ? `${exemptionWord} · ${scope}` : exemptionWord;- }, [member, t]);+ return generateMemberScopeSummary(+ member.grants.length,+ member.databaseNames+ );+ }, [member]);const handleClick = () => {if (expandable) {@@ -1004,8 +991,8 @@ function ExemptionDetailPanel({const grantMatchesFilter = (grant: ExemptionGrant): boolean => {if (!databaseFilter) return false;return (- grant.databaseResources?.some(- (r) => r.databaseFullName === databaseFilter+ grant.databaseResources?.some((r) =>+ resourceCoversDatabase(r, databaseFilter)) ?? false);};@@ -1084,6 +1071,7 @@ function ExemptionGrantSection({}) {const { t } = useTranslation();const [expanded, setExpanded] = useState(defaultExpanded);+ const [showRawCondition, setShowRawCondition] = useState(false);const title = useMemo(() => generateGrantTitle(grant), [grant]);@@ -1181,6 +1169,28 @@ function ExemptionGrantSection({classificationLevel={grant.classificationLevel}/>)}++ {grant.conditionExpression && (+ <div className="mt-3 text-sm">+ <Button+ aria-expanded={showRawCondition}+ appearance="link"+ className="h-auto px-0"+ onClick={() => setShowRawCondition((show) => !show)}+ size="sm"+ type="button"+ >+ {showRawCondition+ ? t("project.masking-exemption.hide-raw-cel-condition")+ : t("project.masking-exemption.show-raw-cel-condition")}+ </Button>+ {showRawCondition && (+ <pre className="mt-1 whitespace-pre-wrap break-all rounded-xs border border-block-border bg-control-bg px-3 py-2 font-mono text-xs">+ {grant.conditionExpression}+ </pre>+ )}+ </div>+ )}</div>)}</div>diff --git a/frontend/src/routes/project/ProjectReleaseDashboardPage.test.tsx b/frontend/src/routes/project/ProjectReleaseDashboardPage.test.tsxnew file mode 100644index 0000000000..378f1158cd--- /dev/null+++ b/frontend/src/routes/project/ProjectReleaseDashboardPage.test.tsx@@ -0,0 +1,103 @@+import { act, createElement, type ReactNode } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";+import { ProjectReleaseDashboardPage } from "./ProjectReleaseDashboardPage";++(+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }+).IS_REACT_ACT_ENVIRONMENT = true;++const mocks = vi.hoisted(() => ({+ listReleaseCategories: vi.fn(async () => ({ categories: [] })),+ listReleasesByProject: vi.fn(async () => ({+ nextPageToken: "",+ releases: [],+ })),+ usePagedData: vi.fn(),+}));++vi.mock("react-i18next", async (importOriginal) => ({+ ...(await importOriginal<typeof import("react-i18next")>()),+ useTranslation: () => ({ t: (key: string) => key }),+}));++vi.mock("@/api", () => ({+ releaseServiceClientConnect: {+ listReleaseCategories: mocks.listReleaseCategories,+ },+}));++vi.mock("@/app/router", () => ({+ router: { push: vi.fn() },+}));++vi.mock("@/app/router/NavigationScrollRestoration", () => ({+ markListScrollRestorationEntry: vi.fn(),+ useListScrollRestorationLoadMore: () => undefined,+}));++vi.mock("@/components/ProjectPageLayout", () => ({+ ProjectPageContent: ({ children }: { children: ReactNode }) =>+ createElement("div", {}, children),+ ProjectPageFooter: ({ children }: { children: ReactNode }) =>+ createElement("footer", {}, children),+ ProjectPageInfo: () => null,+ ProjectPageLayout: ({ children }: { children: ReactNode }) =>+ createElement("div", {}, children),+}));++vi.mock("@/hooks/usePagedData", () => ({+ PagedTableFooter: () => null,+ usePagedData: mocks.usePagedData,+}));++vi.mock("@/hooks/useURLSearchParam", () => ({+ useURLSearchParam: () => ["", vi.fn()],+}));++vi.mock("@/stores/app", () => ({+ useAppStore: (+ selector: (state: { listReleasesByProject: typeof mocks.listReleasesByProject }) => unknown+ ) => selector({ listReleasesByProject: mocks.listReleasesByProject }),+}));++const pagedData = {+ dataList: [],+ hasMore: false,+ isFetchingMore: false,+ isLoading: false,+ loadMore: vi.fn(),+ onPageSizeChange: vi.fn(),+ pageSize: 50,+ pageSizeOptions: [50, 100],+ refresh: vi.fn(),+ removeCache: vi.fn(),+ updateCache: vi.fn(),+};++describe("ProjectReleaseDashboardPage", () => {+ let container: HTMLDivElement;+ let root: Root;++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ root = createRoot(container);+ mocks.usePagedData.mockReturnValue(pagedData);+ });++ afterEach(() => {+ act(() => root.unmount());+ container.remove();+ vi.clearAllMocks();+ });++ test("hides the category filter when the project has no release categories", async () => {+ await act(async () => {+ root.render(<ProjectReleaseDashboardPage projectId="foo" />);+ await Promise.resolve();+ });++ expect(container.querySelector('[role="combobox"]')).toBeNull();+ });+});diff --git a/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx b/frontend/src/routes/project/ProjectReleaseDashboardPage.tsxindex 49cc80869e..4c4c869e6d 100644--- a/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx+++ b/frontend/src/routes/project/ProjectReleaseDashboardPage.tsx@@ -111,24 +111,25 @@ export function ProjectReleaseDashboardPage({}/>- {/* Category filter */}- <div className="flex items-center gap-x-4">- <CategorySelect- value={selectedCategory}- onChange={setSelectedCategory}- categories={categories}- loading={categoriesLoading}- />- {selectedCategory && (- <Button- appearance="secondary"- size="sm"- onClick={() => setSelectedCategory("")}- >- {t("common.clear-filters")}- </Button>- )}- </div>+ {(categories.length > 0 || selectedCategory) && (+ <div className="flex items-center gap-x-4">+ <CategorySelect+ value={selectedCategory}+ onChange={setSelectedCategory}+ categories={categories}+ loading={categoriesLoading}+ />+ {selectedCategory && (+ <Button+ appearance="secondary"+ size="sm"+ onClick={() => setSelectedCategory("")}+ >+ {t("common.clear-filters")}+ </Button>+ )}+ </div>+ )}</div>{/* Release table */}diff --git a/frontend/src/utils/expr.test.ts b/frontend/src/utils/expr.test.tsnew file mode 100644index 0000000000..6d95b498b1--- /dev/null+++ b/frontend/src/utils/expr.test.ts@@ -0,0 +1,34 @@+import { describe, expect, test } from "vitest";+import type { Database } from "@/types/proto-es/v1/database_service_pb";+import { getDatabaseFullNameOption, getDatabaseNameOptionConfig } from "./expr";++describe("getDatabaseNameOptionConfig", () => {+ test("uses the instance title and database name as the display label", () => {+ const database = {+ name: "instances/mysql-instance/databases/bytebase_210",+ instanceResource: { title: "MySQL instance" },+ } as Database;++ expect(getDatabaseFullNameOption(database)).toEqual({+ label: "MySQL instance (mysql-instance) / bytebase_210",+ value: database.name,+ });+ });++ test("falls back to parsed resource IDs when the database cannot be fetched", () => {+ const config = getDatabaseNameOptionConfig("projects/project-a");++ expect(+ config.fallback?.("instances/mysql-instance/databases/bytebase_210")+ ).toEqual({+ label: "mysql-instance / bytebase_210",+ value: "instances/mysql-instance/databases/bytebase_210",+ });+ });++ test("keeps an empty database condition blank", () => {+ const config = getDatabaseNameOptionConfig("projects/project-a");++ expect(config.fallback?.("")).toEqual({ label: "", value: "" });+ });+});diff --git a/frontend/src/utils/expr.ts b/frontend/src/utils/expr.tsindex 770ee1c1ab..55fd675d7c 100644--- a/frontend/src/utils/expr.ts+++ b/frontend/src/utils/expr.ts@@ -52,10 +52,22 @@ const getProjectIdOption = (proj: Project): LabeledOption => {};};-const getDatabaseFullNameOption = (database: Database): LabeledOption => ({- label: database.name,- value: database.name,-});+export const getDatabaseFullNameOption = (+ database: Database+): LabeledOption => {+ const { instanceName, databaseName } = extractDatabaseResourceName(+ database.name+ );+ const instanceTitle = database.instanceResource?.title;+ const instanceLabel =+ instanceTitle && instanceTitle !== instanceName+ ? `${instanceTitle} (${instanceName})`+ : instanceName;+ return {+ label: `${instanceLabel} / ${databaseName}`,+ value: database.name,+ };+};const getInstanceIdOption = (ins: Instance): LabeledOption => {const instanceId = extractInstanceResourceName(ins.name);@@ -172,6 +184,16 @@ export const getDatabaseIdOptionConfig = (parent: string): OptionConfig => {export const getDatabaseNameOptionConfig = (parent: string): OptionConfig => {return {options: [],+ fallback: (value: string) => {+ if (!value) {+ return { label: "", value };+ }+ const { instanceName, databaseName } = extractDatabaseResourceName(value);+ return {+ label: `${instanceName} / ${databaseName}`,+ value,+ };+ },fetch: async (names: string[]) => {const databases = await batchGetOrFetchDatabases(names);return databases.map(getDatabaseFullNameOption);diff --git a/frontend/src/utils/issue/cel.test.ts b/frontend/src/utils/issue/cel.test.tsindex c29c3c6f15..0f86a5dfe0 100644--- a/frontend/src/utils/issue/cel.test.ts+++ b/frontend/src/utils/issue/cel.test.ts@@ -123,3 +123,47 @@ describe("readableCondition proves the environments and keeps the rest best-effo]);});});++describe("convertFromExpr with a column condition", () => {+ const columnIn = (...names: string[]) =>+ call("@in", resource("column_name"), list(...names.map(str)));+ const databaseResources = (expr: Expr) =>+ convertFromExpr(expr).databaseResources;++ test("keeps the columns of a column-only in condition", () => {+ expect(databaseResources(columnIn("email", "phone"))).toEqual([+ { databaseFullName: "", columns: ["email", "phone"] },+ ]);+ });++ test("collects columns from equality conditions joined by or", () => {+ expect(+ databaseResources(+ or(eq("column_name", "email"), eq("column_name", "phone"))+ )+ ).toEqual([{ databaseFullName: "", columns: ["email", "phone"] }]);+ });++ test("keeps disjunctive column scopes in separate resources", () => {+ expect(+ databaseResources(+ or(+ columnIn("a", "b"),+ and(+ eq("instance_id", "mysql-instance"),+ eq("database_name", "bytebase_210"),+ eq("column_name", "c")+ )+ )+ )+ ).toEqual([+ { databaseFullName: "", columns: ["a", "b"] },+ {+ databaseFullName: "instances/mysql-instance/databases/bytebase_210",+ instanceResourceId: "mysql-instance",+ databaseResourceId: "bytebase_210",+ columns: ["c"],+ },+ ]);+ });+});diff --git a/frontend/src/utils/issue/cel.ts b/frontend/src/utils/issue/cel.tsindex 5f478b9f75..18249b5a4a 100644--- a/frontend/src/utils/issue/cel.ts+++ b/frontend/src/utils/issue/cel.ts@@ -1,12 +1,13 @@import { create } from "@bufbuild/protobuf";import dayjs from "dayjs";import { cloneDeep, head, uniq } from "lodash-es";-import type { SimpleExpr } from "@/modules/cel";import {constantStringOf,+ ExprType,factorNameOf,isRawStringExpr,resolveCELExpr,+ type SimpleExpr,} from "@/modules/cel";import {databaseNamePrefix,@@ -443,8 +444,41 @@ const environmentClauseOf = (expr: Expr): string[] | undefined => {return ids && uniq(ids).map((id) => `${environmentNamePrefix}${id}`);};-export const convertFromExpr = (expr: Expr): ConditionExpression => {- const simpleExpr = resolveCELExpr(expr);+const splitTopLevelOr = (expr: SimpleExpr): SimpleExpr[] => {+ if (expr.type !== ExprType.ConditionGroup || expr.operator !== "_||_") {+ return [expr];+ }+ return expr.args.flatMap(splitTopLevelOr);+};++const mergeDatabaseResources = (+ resources: DatabaseResource[]+): DatabaseResource[] => {+ const merged = new Map<string, DatabaseResource>();+ for (const resource of resources) {+ const key = [+ resource.databaseFullName,+ resource.instanceResourceId,+ resource.databaseResourceId,+ resource.schema,+ resource.table,+ ].join("\u0000");+ const existing = merged.get(key);+ if (!existing) {+ merged.set(key, { ...resource, columns: resource.columns?.slice() });+ continue;+ }+ if (resource.columns) {+ existing.columns = uniq([+ ...(existing.columns ?? []),+ ...resource.columns,+ ]);+ }+ }+ return [...merged.values()];+};++const convertFromSimpleExpr = (simpleExpr: SimpleExpr): ConditionExpression => {const conditionExpression: ConditionExpression = {databaseResources: [],};@@ -509,16 +543,14 @@ export const convertFromExpr = (expr: Expr): ConditionExpression => {}case CEL_ATTRIBUTE_RESOURCE_COLUMN_NAME: {const databaseResource =- conditionExpression.databaseResources?.pop();- if (databaseResource) {- databaseResource.columns = [];- for (const value of values) {- if (value) {- databaseResource.columns.push(value as string);- }- }- conditionExpression.databaseResources!.push(databaseResource);- }+ conditionExpression.databaseResources?.pop() ?? {+ databaseFullName: "",+ };+ databaseResource.columns = uniq([+ ...(databaseResource.columns ?? []),+ ...values.filter(Boolean).map(String),+ ]);+ conditionExpression.databaseResources!.push(databaseResource);break;}}@@ -576,8 +608,11 @@ export const convertFromExpr = (expr: Expr): ConditionExpression => {break;}case CEL_ATTRIBUTE_RESOURCE_COLUMN_NAME: {- if (right) {- databaseResource.columns = [right];+ if (right && !databaseResource.columns?.includes(right)) {+ databaseResource.columns = [+ ...(databaseResource.columns ?? []),+ right,+ ];}break;}@@ -596,6 +631,32 @@ export const convertFromExpr = (expr: Expr): ConditionExpression => {return conditionExpression;};+export const convertFromExpr = (expr: Expr): ConditionExpression => {+ const branches = splitTopLevelOr(resolveCELExpr(expr));+ if (branches.length === 1) {+ return convertFromSimpleExpr(branches[0]);+ }++ const branchConditions = branches.map(convertFromSimpleExpr);+ const expiration = branchConditions.find(+ (condition) => condition.expiredTime+ )?.expiredTime;+ const environmentLists = branchConditions+ .map((condition) => condition.environments)+ .filter(+ (environments): environments is string[] => environments !== undefined+ );+ return {+ databaseResources: mergeDatabaseResources(+ branchConditions.flatMap((condition) => condition.databaseResources ?? [])+ ),+ ...(expiration ? { expiredTime: expiration } : {}),+ ...(environmentLists.length > 0+ ? { environments: uniq(environmentLists.flat()) }+ : {}),+ };+};+const mergeDatabaseLevelConditions = (conditions: DatabaseLevelCondition[]): DatabaseLevelCondition[] => {