Skip to content

Commit

Permalink
[feature]: Enable custom rules support in the rule knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
LZS911 committed Oct 11, 2023
1 parent acb9b50 commit 9a3ef01
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/api/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export interface IAuditResDataV1 {
}

export interface IAuditResult {
db_type?: string;

level?: string;

message?: string;
Expand Down
10 changes: 9 additions & 1 deletion src/components/AuditResultErrorMessage/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ import { renderWithRouter } from '../../testUtils/customRender';

describe('AuditResultErrorMessage', () => {
const auditResult: IAuditResult[] = [
{ level: 'error', message: 'schema 不存在', rule_name: '' },
{
level: 'error',
message: 'schema 不存在',
rule_name: '',
db_type: 'MySQL',
},
{
level: 'error',
message:
'禁止使用没有where条件的sql语句或者使用where 1=1等变相没有条件的sql',
rule_name: 'all_check_where_is_invalid',
db_type: 'MySQL',
},
{
level: 'warn',
message: 'select 语句必须带limit,且限制数不得超过1000',
rule_name: 'dml_check_select_limit',
db_type: 'MySQL',
},
];

Expand Down Expand Up @@ -55,6 +62,7 @@ describe('AuditResultErrorMessage', () => {
expect(getRulesSpy).toBeCalledTimes(1);
expect(getRulesSpy).nthCalledWith(1, {
filter_rule_names: 'all_check_where_is_invalid,dml_check_select_limit',
filter_db_type: 'MySQL',
});

fireEvent.mouseEnter(
Expand Down
13 changes: 10 additions & 3 deletions src/components/AuditResultErrorMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ const AuditResultErrorMessage: React.FC<AuditResultErrorMessageProps> = (
[props.auditResult]
);

const filterDbType = useMemo(() => {
return props.auditResult?.find((v) => !!v.db_type)?.db_type;
}, [props.auditResult]);

const { data: ruleInfo } = useRequest(
() =>
rule_template
.getRuleListV1({
filter_rule_names: filterRuleNames.join(','),
filter_db_type: filterDbType,
})
.then((res) => res.data.data),
{ ready: !!filterRuleNames.length, refreshDeps: [filterRuleNames] }
{
ready: !!filterRuleNames.length,
refreshDeps: [filterRuleNames, filterDbType],
}
);

return (
Expand Down Expand Up @@ -58,8 +66,7 @@ const AuditResultErrorMessage: React.FC<AuditResultErrorMessageProps> = (

{/* IFTRUE_isEE */}
{' '}
{/* 暂时不支持自定义规则 */}
<EmptyBox if={!rule?.is_custom_rule && !!rule?.db_type}>
<EmptyBox if={!!rule?.db_type}>
<Link
target="_blank"
to={`rule/knowledge/${v.rule_name}?db_type=${rule?.db_type}`}
Expand Down
3 changes: 1 addition & 2 deletions src/components/RuleList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ const RuleList: React.FC<RuleListProps> = (props) => {
{item.annotation}
{/* IFTRUE_isEE */}
{' '}
{/* 暂时不支持自定义规则 */}
<EmptyBox if={!item?.is_custom_rule && !!item?.db_type}>
<EmptyBox if={!!item?.db_type}>
<Link
target="_blank"
to={`rule/knowledge/${item.rule_name}?db_type=${item?.db_type}`}
Expand Down
1 change: 1 addition & 0 deletions src/page/Order/AuditResult/__test__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('Order/Detail/AuditResult', () => {
expect(getAllRulesSpy).toBeCalledTimes(1);
expect(getAllRulesSpy).nthCalledWith(1, {
filter_rule_names: 'all_check_where_is_invalid',
filter_db_type: 'MySQL',
});
});
});
1 change: 1 addition & 0 deletions src/page/Order/Detail/__testData__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ export const taskSqls = [
message:
'禁止使用没有where条件的sql语句或者使用where 1=1等变相没有条件的sql',
rule_name: 'all_check_where_is_invalid',
db_type: 'MySQL',
},
],
audit_level: 'error',
Expand Down
20 changes: 14 additions & 6 deletions src/page/RuleKnowledge/RuleUnderstand/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const RuleUnderstand: React.FC<RuleUnderstandProps> = ({
dbType,
loading,
isAdmin,
isCustomRule,
}) => {
const { t } = useTranslation();
const [modifyFlag, { setTrue: startModify, setFalse: modifyFinish }] =
Expand Down Expand Up @@ -45,12 +46,19 @@ const RuleUnderstand: React.FC<RuleUnderstandProps> = ({
};
const submit = () => {
startSubmit();
rule_template
.updateRuleKnowledge({
rule_name: ruleName,
knowledge_content: editValue,
db_type: dbType,
})
const request = isCustomRule
? rule_template.updateCustomRuleKnowledge({
rule_name: ruleName,
knowledge_content: editValue,
db_type: dbType,
})

Check warning on line 54 in src/page/RuleKnowledge/RuleUnderstand/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
: rule_template.updateRuleKnowledge({
rule_name: ruleName,
knowledge_content: editValue,
db_type: dbType,
});

Check warning on line 59 in src/page/RuleKnowledge/RuleUnderstand/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 59 in src/page/RuleKnowledge/RuleUnderstand/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

request
.then((res) => {
if (res.data.code === ResponseCode.SUCCESS) {
message.success(t('ruleKnowledge.successTips'));
Expand Down
1 change: 1 addition & 0 deletions src/page/RuleKnowledge/RuleUnderstand/index.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type RuleUnderstandProps = {
dbType: string;
loading: boolean;
isAdmin: boolean;
isCustomRule: boolean;
};

export type EditKnowledgeContentProps = {
Expand Down
46 changes: 40 additions & 6 deletions src/page/RuleKnowledge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,65 @@ import RuleUnderstand from './RuleUnderstand';
import { useEffect, useState } from 'react';
import EmptyBox from '../../components/EmptyBox';
import useCurrentUser from '../../hooks/useCurrentUser';
import { ResponseCode } from '../../data/common';

const RuleKnowledge: React.FC = () => {
const { t } = useTranslation();
const location = useLocation();
const theme = useTheme();
const { ruleName = '' } = useParams<{ ruleName: string }>();
const [dbType, setDbType] = useState<string>();
const [isCustomRule, setIsCustomRule] = useState(false);

Check warning on line 19 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const { isAdmin } = useCurrentUser();

const {
data: ruleKnowledgeInfo,
loading,
refresh,
run: getRuleKnowledge,
} = useRequest(
() =>
rule_template
.getRuleKnowledgeV1({ rule_name: ruleName, db_type: dbType! })
(isCustomType: boolean, dbType: string) => {

Check warning on line 28 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
if (isCustomType) {
return rule_template
.getCustomRuleKnowledgeV1({ rule_name: ruleName, db_type: dbType })
.then((res) => {

Check warning on line 32 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return res.data.data;

Check warning on line 33 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
});

Check warning on line 34 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 35 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 35 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return rule_template
.getRuleKnowledgeV1({ rule_name: ruleName, db_type: dbType })
.then((res) => {
return res.data.data;
}),
});

Check warning on line 40 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
},
{
ready: !!ruleName && !!dbType,
refreshDeps: [ruleName, dbType],
manual: true,
}
);

Check warning on line 45 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

useEffect(() => {

Check warning on line 47 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
if (!!ruleName && !!dbType) {

Check warning on line 48 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 48 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
rule_template
.getRuleListV1({
filter_rule_names: ruleName,
filter_db_type: dbType,
})
.then((res) => {

Check warning on line 54 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
if (
res.data.code === ResponseCode.SUCCESS &&

Check warning on line 56 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
res.data.data?.length === 1

Check warning on line 57 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
) {
const ruleInfo = res.data.data[0];

Check warning on line 59 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
if (!!ruleInfo.db_type) {
setIsCustomRule(!!ruleInfo.is_custom_rule);

Check warning on line 61 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
getRuleKnowledge(!!ruleInfo.is_custom_rule, ruleInfo.db_type);

Check warning on line 62 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 63 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 63 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

Check warning on line 64 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 64 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
});

Check warning on line 65 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 66 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 66 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dbType]);

Check warning on line 68 in src/page/RuleKnowledge/index.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

useEffect(() => {
const searchParams = new URLSearchParams(location.search);

Expand Down Expand Up @@ -73,6 +106,7 @@ const RuleKnowledge: React.FC = () => {
refresh={refresh}
dbType={dbType!}
isAdmin={isAdmin}
isCustomRule={isCustomRule}
/>
</EmptyBox>
</Space>
Expand Down

0 comments on commit 9a3ef01

Please sign in to comment.