Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(frontend): support custom sql review rule description (#4922)

Liu Ji285b18ac83ec
diff --git a/frontend/src/components/Issue/TaskCheckBar.vue b/frontend/src/components/Issue/TaskCheckBar.vue
index a913f0e63e..b05fe1165b 100644
--- a/frontend/src/components/Issue/TaskCheckBar.vue
+++ b/frontend/src/components/Issue/TaskCheckBar.vue
@@ -59,6 +59,7 @@
<TaskCheckRunPanel
v-if="selectedTaskCheckRun"
:task-check-run="selectedTaskCheckRun"
+ :task="task"
/>
<div class="pt-4 flex justify-end">
<button
diff --git a/frontend/src/components/Issue/TaskCheckRunPanel.vue b/frontend/src/components/Issue/TaskCheckRunPanel.vue
index e049ca4907..4d4a02844c 100644
--- a/frontend/src/components/Issue/TaskCheckRunPanel.vue
+++ b/frontend/src/components/Issue/TaskCheckRunPanel.vue
@@ -40,10 +40,20 @@
{{ row.title }}
</BBTableCell>
<BBTableCell class="w-auto">
- {{ row.checkResult.content }}
+ <span>{{ row.checkResult.content }}</span>
+ <template
+ v-if="row.checkResult.namespace === 'bb.advisor' && getActiveRule(row.checkResult.title as RuleType)"
+ >
+ <span
+ class="ml-1 normal-link"
+ @click="setActiveRule(row.checkResult.title as RuleType)"
+ >{{ $t("sql-review.rule-detail") }}</span
+ >
+ <span class="border-r border-control-border ml-1"></span>
+ </template>
<a
v-if="row.link"
- class="normal-link"
+ class="ml-1 normal-link"
:href="row.link.url"
:target="row.link.target"
>
@@ -52,11 +62,20 @@
</BBTableCell>
</template>
</BBTable>
+
+ <SQLRuleEditDialog
+ v-if="state.activeRule"
+ :editable="false"
+ :rule="state.activeRule.rule"
+ :payload="state.activeRule.payload"
+ :disabled="false"
+ @cancel="state.activeRule = undefined"
+ />
</div>
</template>
<script lang="ts" setup>
-import { computed, PropType } from "vue";
+import { computed, PropType, reactive } from "vue";
import { useI18n } from "vue-i18n";
import {
TaskCheckStatus,
@@ -67,9 +86,15 @@ import {
getRuleLocalization,
SQLReviewPolicyErrorCode,
RuleType,
+ RuleTemplate,
+ Task,
+ findRuleTemplate,
} from "@/types";
import type { BBTableColumn } from "@/bbkit";
import { LocalizedSQLRuleErrorCodes } from "./const";
+import { SQLRuleEditDialog } from "@/components/SQLReview/components";
+import { PayloadValueType } from "../SQLReview/components/RuleConfigComponents";
+import { useReviewPolicyByEnvironmentId } from "@/store";
interface ErrorCodeLink {
title: string;
@@ -77,6 +102,11 @@ interface ErrorCodeLink {
url: string;
}
+type PreviewSQLReviewRule = {
+ rule: RuleTemplate;
+ payload: PayloadValueType[];
+};
+
type TableRow = {
checkResult: TaskCheckResult;
category: string;
@@ -84,11 +114,23 @@ type TableRow = {
link: ErrorCodeLink | undefined;
};
+type LocalState = {
+ activeRule: PreviewSQLReviewRule | undefined;
+};
+
const props = defineProps({
taskCheckRun: {
required: true,
type: Object as PropType<TaskCheckRun>,
},
+ task: {
+ required: true,
+ type: Object as PropType<Task>,
+ },
+});
+
+const state = reactive<LocalState>({
+ activeRule: undefined,
});
const { t } = useI18n();
@@ -217,4 +259,36 @@ const COLUMN_LIST = computed((): BBTableColumn[] => {
}
return [STATUS, TITLE, CONTENT];
});
+
+const reviewPolicy = useReviewPolicyByEnvironmentId(
+ computed(() => props.task.instance.environment.id)
+);
+const getActiveRule = (type: RuleType): PreviewSQLReviewRule | undefined => {
+ const rule = reviewPolicy.value?.ruleList.find((rule) => rule.type === type);
+ if (!rule) {
+ return undefined;
+ }
+
+ const ruleTemplate = findRuleTemplate(type);
+ if (!ruleTemplate) {
+ return undefined;
+ }
+ ruleTemplate.comment = rule.comment;
+ const { componentList } = ruleTemplate;
+ const payload = componentList.reduce<PayloadValueType[]>(
+ (list, component) => {
+ list.push(component.payload.value ?? component.payload.default);
+ return list;
+ },
+ []
+ );
+
+ return {
+ rule: ruleTemplate,
+ payload: payload,
+ };
+};
+const setActiveRule = (type: RuleType) => {
+ state.activeRule = getActiveRule(type);
+};
</script>
diff --git a/frontend/src/components/SQLReview/SQLReviewConfig.vue b/frontend/src/components/SQLReview/SQLReviewConfig.vue
index 99f7d56e6b..e618c5bef7 100644
--- a/frontend/src/components/SQLReview/SQLReviewConfig.vue
+++ b/frontend/src/components/SQLReview/SQLReviewConfig.vue
@@ -11,6 +11,7 @@
:editable="true"
@level-change="onLevelChange"
@payload-change="onPayloadChange"
+ @comment-change="onCommentChange"
/>
</div>
</template>
@@ -44,6 +45,7 @@ const emit = defineEmits<{
componentList: RuleConfigComponent[]
): void;
(event: "level-change", rule: RuleTemplate, level: RuleLevel): void;
+ (event: "comment-change", rule: RuleTemplate, comment: string): void;
}>();
const {
@@ -66,4 +68,8 @@ const onPayloadChange = (
const onLevelChange = (rule: RuleTemplate, level: RuleLevel) => {
emit("level-change", rule, level);
};
+
+const onCommentChange = (rule: RuleTemplate, comment: string) => {
+ emit("comment-change", rule, comment);
+};
</script>
diff --git a/frontend/src/components/SQLReview/SQLReviewCreation.vue b/frontend/src/components/SQLReview/SQLReviewCreation.vue
index aee1a5b355..6242937b54 100644
--- a/frontend/src/components/SQLReview/SQLReviewCreation.vue
+++ b/frontend/src/components/SQLReview/SQLReviewCreation.vue
@@ -28,6 +28,7 @@
:selected-rule-list="state.selectedRuleList"
@level-change="onLevelChange"
@payload-change="onPayloadChange"
+ @comment-change="onCommentChange"
/>
</template>
</BBStepTab>
@@ -312,4 +313,8 @@ const onPayloadChange = (
const onLevelChange = (rule: RuleTemplate, level: RuleLevel) => {
change(rule, { level });
};
+
+const onCommentChange = (rule: RuleTemplate, comment: string) => {
+ change(rule, { comment });
+};
</script>
diff --git a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
index df7847792c..85e403fbcd 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleEditDialog.vue
@@ -98,12 +98,32 @@
@update:value="state.payload[index] = $event"
/>
</div>
- <div v-if="getRuleLocalization(rule.type).description" class="space-y-1">
+ <div v-if="editable" class="space-y-1">
<h3 class="text-lg text-control font-medium">
{{ $t("common.description") }}
</h3>
- <div class="textinfolabel flex items-center gap-x-2">
- {{ getRuleLocalization(rule.type).description }}
+ <div class="flex flex-col gap-x-2">
+ <input
+ v-model="state.comment"
+ type="text"
+ :disabled="disabled"
+ :class="[
+ 'shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full border-gray-300 rounded-md',
+ disabled && 'cursor-not-allowed',
+ ]"
+ :placeholder="
+ getRuleLocalization(rule.type).description ||
+ $t('common.description')
+ "
+ />
+ </div>
+ </div>
+ <div v-else-if="displayDescription" class="space-y-1">
+ <h3 class="text-lg text-control font-medium">
+ {{ $t("common.description") }}
+ </h3>
+ <div class="flex flex-col gap-x-2">
+ {{ displayDescription }}
</div>
</div>
<div v-if="editable" class="mt-4 pt-2 border-t flex justify-end">
@@ -128,7 +148,7 @@
<script lang="ts" setup>
import { cloneDeep } from "lodash-es";
-import { nextTick, reactive, watch } from "vue";
+import { computed, nextTick, reactive, watch } from "vue";
import {
getRuleLocalization,
@@ -152,6 +172,7 @@ import { useI18n } from "vue-i18n";
type LocalState = {
payload: PayloadValueType[];
level: RuleLevel;
+ comment: string;
};
const props = defineProps<{
@@ -164,6 +185,7 @@ const props = defineProps<{
const emit = defineEmits<{
(event: "update:payload", payload: PayloadValueType[]): void;
(event: "update:level", level: RuleLevel): void;
+ (event: "update:comment", comment: string): void;
(event: "cancel"): void;
}>();
@@ -172,6 +194,11 @@ const { t } = useI18n();
const state = reactive<LocalState>({
payload: cloneDeep(props.payload),
level: props.rule.level,
+ comment: props.rule.comment ?? "",
+});
+
+const displayDescription = computed(() => {
+ return state.comment || getRuleLocalization(props.rule.type).description;
});
const configTitle = (config: RuleConfigComponent): string => {
@@ -201,6 +228,7 @@ watch(
const confirm = () => {
emit("update:level", state.level);
emit("update:payload", state.payload);
+ emit("update:comment", state.comment);
nextTick(() => {
emit("cancel");
});
diff --git a/frontend/src/components/SQLReview/components/SQLRuleTable.vue b/frontend/src/components/SQLReview/components/SQLRuleTable.vue
index 7552533a29..5f82bed412 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleTable.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleTable.vue
@@ -94,6 +94,7 @@
@cancel="state.activeRule = undefined"
@update:payload="updatePayload(state.activeRule!, $event)"
@update:level="updateLevel(state.activeRule!, $event)"
+ @update:comment="updateComment(state.activeRule!, $event)"
/>
</div>
</template>
@@ -139,6 +140,7 @@ const emit = defineEmits<{
payload: PayloadValueType[]
): void;
(event: "level-change", rule: RuleTemplate, level: RuleLevel): void;
+ (event: "comment-change", rule: RuleTemplate, comment: string): void;
}>();
const { t } = useI18n();
@@ -198,4 +200,7 @@ const updatePayload = (rule: RuleTemplate, payload: PayloadValueType[]) => {
const updateLevel = (rule: RuleTemplate, level: RuleLevel) => {
emit("level-change", rule, level);
};
+const updateComment = (rule: RuleTemplate, comment: string) => {
+ emit("comment-change", rule, comment);
+};
</script>
diff --git a/frontend/src/components/SQLReview/components/index.ts b/frontend/src/components/SQLReview/components/index.ts
index d455e83e6f..2a2f0d02cc 100644
--- a/frontend/src/components/SQLReview/components/index.ts
+++ b/frontend/src/components/SQLReview/components/index.ts
@@ -1,7 +1,13 @@
import SQLReviewTemplateSelector from "./SQLReviewTemplateSelector.vue";
import SQLRuleTable from "./SQLRuleTable.vue";
import SQLRuleFilter from "./SQLRuleFilter.vue";
+import SQLRuleEditDialog from "./SQLRuleEditDialog.vue";
-export { SQLReviewTemplateSelector, SQLRuleTable, SQLRuleFilter };
+export {
+ SQLReviewTemplateSelector,
+ SQLRuleTable,
+ SQLRuleFilter,
+ SQLRuleEditDialog,
+};
export * from "./useSQLRuleFilter";
export * from "./utils";
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 5a9aee4b4c..a1c6f1d11f 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1811,7 +1811,8 @@
"rule": {
"active": "Active"
},
- "enabled-rules": "Enabled rules"
+ "enabled-rules": "Enabled rules",
+ "rule-detail": "Rule detail"
},
"debug-log": {
"title": "Debug Log",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index d567926ec1..cb1d958080 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1810,7 +1810,8 @@
"rule": {
"active": "开启"
},
- "enabled-rules": "开启的规则数"
+ "enabled-rules": "开启的规则数",
+ "rule-detail": "规则详情"
},
"debug-log": {
"title": "Debug 日志",
diff --git a/frontend/src/store/modules/sqlReview.ts b/frontend/src/store/modules/sqlReview.ts
index e417502d20..f023b190c9 100644
--- a/frontend/src/store/modules/sqlReview.ts
+++ b/frontend/src/store/modules/sqlReview.ts
@@ -12,10 +12,11 @@ import {
SQLReviewPolicyPayload,
SQLReviewPolicy,
IdType,
+ MaybeRef,
} from "@/types";
import { defineStore } from "pinia";
import { usePolicyStore } from "./policy";
-import { computed, watchEffect } from "vue";
+import { computed, unref, watchEffect } from "vue";
const convertToSQLReviewPolicy = (
policy: Policy
@@ -32,6 +33,7 @@ const convertToSQLReviewPolicy = (
const rule: SchemaPolicyRule = {
type: r.type,
level: r.level,
+ comment: r.comment,
};
if (r.payload && r.payload !== "{}") {
rule.payload = JSON.parse(r.payload);
@@ -239,6 +241,15 @@ export const useSQLReviewStore = defineStore("sqlReview", {
}
return reviewPolicy;
},
+ async getOrFetchReviewPolicyByEnvironmentId(
+ environmentId: EnvironmentId
+ ): Promise<SQLReviewPolicy | undefined> {
+ const existed = this.getReviewPolicyByEnvironmentId(environmentId);
+ if (existed) {
+ return existed;
+ }
+ return this.fetchReviewPolicyByEnvironmentId(environmentId);
+ },
},
});
@@ -251,3 +262,16 @@ export const useSQLReviewPolicyList = () => {
return computed(() => store.reviewPolicyList);
};
+
+export const useReviewPolicyByEnvironmentId = (
+ environmentId: MaybeRef<EnvironmentId>
+) => {
+ const store = useSQLReviewStore();
+ watchEffect(() => {
+ store.getOrFetchReviewPolicyByEnvironmentId(unref(environmentId));
+ });
+
+ return computed(() =>
+ store.getReviewPolicyByEnvironmentId(unref(environmentId))
+ );
+};
diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.ts
index c5fe6fe52f..a971f2e332 100644
--- a/frontend/src/types/policy.ts
+++ b/frontend/src/types/policy.ts
@@ -52,6 +52,7 @@ export type SQLReviewPolicyPayload = {
type: RuleType;
level: RuleLevel;
payload: string;
+ comment: string;
}[];
};
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 928af7514e..05d30ed0eb 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -183,6 +183,7 @@ export interface SchemaPolicyRule {
| StringArrayLimitPayload
| CommentFormatPayload
| NumberLimitPayload;
+ comment: string;
}
// The API for SQL review policy in backend.
@@ -205,6 +206,7 @@ export interface RuleTemplate {
engineList: SchemaRuleEngineType[];
componentList: RuleConfigComponent[];
level: RuleLevel;
+ comment?: string;
}
// SQLReviewPolicyTemplate is the rule template set
@@ -271,6 +273,15 @@ export const TEMPLATE_LIST: SQLReviewPolicyTemplate[] = (function () {
});
})();
+export const findRuleTemplate = (type: RuleType) => {
+ for (let i = 0; i < TEMPLATE_LIST.length; i++) {
+ const template = TEMPLATE_LIST[i];
+ const rule = template.ruleList.find((rule) => rule.type === type);
+ if (rule) return rule;
+ }
+ return undefined;
+};
+
export const ruleTemplateMap: Map<RuleType, RuleTemplate> =
TEMPLATE_LIST.reduce((map, template) => {
for (const rule of template.ruleList) {
@@ -324,7 +335,11 @@ export const convertPolicyRuleToRuleTemplate = (
);
}
- const res = { ...ruleTemplate, level: policyRule.level };
+ const res = {
+ ...ruleTemplate,
+ level: policyRule.level,
+ comment: policyRule.comment,
+ };
if (ruleTemplate.componentList.length === 0) {
return res;
@@ -532,6 +547,7 @@ export const convertRuleTemplateToPolicyRule = (
const base: SchemaPolicyRule = {
type: rule.type,
level: rule.level,
+ comment: rule.comment ?? "",
};
if (rule.componentList.length === 0) {
return base;
diff --git a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
index d36d0a1662..ffb25592a3 100644
--- a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
+++ b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue
@@ -93,6 +93,7 @@
:editable="hasPermission"
@level-change="onLevelChange"
@payload-change="onPayloadChange"
+ @comment-change="onCommentChange"
/>
<BBButtonConfirm
class="mt-2"
@@ -178,6 +179,7 @@ import {
payloadValueListToComponentList,
SQLRuleFilter,
useSQLRuleFilter,
+ SQLRuleTable,
} from "../components/SQLReview/components";
import ProductionEnvironmentIcon from "@/components/Environment/ProductionEnvironmentIcon.vue";
import { PayloadValueType } from "@/components/SQLReview/components/RuleConfigComponents";
@@ -339,6 +341,10 @@ const onLevelChange = (rule: RuleTemplate, level: RuleLevel) => {
markChange(rule, { level });
};
+const onCommentChange = (rule: RuleTemplate, comment: string) => {
+ markChange(rule, { comment });
+};
+
const onCancelChanges = () => {
state.ruleList = cloneDeep(ruleListOfPolicy.value);
};