feat: Webhook「发送测试告警」(P0-2,设置页入口+结果化推送)

- AlertWebhookSender 新增 sendForResult:结果化推送(送达与否 + 原因摘要),
  超时/网络错误/非 2xx/URL 非法一律归一为失败结果,永不抛出;
  send(fire-and-forget)复用同一实现,真实告警路径语义不变
- 新增 AlertTestEndpoint:POST /apis/.../alerts/-/test 以 type=test 测试消息
  推送到已保存的 Webhook 地址并同步等待结果返回;Webhook 未配置/非法返回 400;
  RBAC 资源组合 alerts + alerts/test(create 动词)不在只读角色模板授权内
- 设置页「告警通知」区新增「发送测试告警」自定义 FormKit 输入
  (Halo 2.25+ formkit.inputs,@formkit/vue 为宿主共享外部模块),
  推送结果以设置页提示 + 运行日志反馈,ignore:true 不新增设置项
- 更新 README(功能/设置表/告警负载/技术说明)
This commit is contained in:
2026-11-22 00:11:24 +08:00
parent 08e7bd8a12
commit 3269bafbc4
13 changed files with 612 additions and 15 deletions
+108
View File
@@ -6,6 +6,7 @@
"": {
"name": "plugin-operation-audit-ui",
"dependencies": {
"@formkit/vue": "^1.7.2",
"@halo-dev/api-client": "2.26.0",
"@halo-dev/components": "2.26.0",
"@halo-dev/console-shared": "^2.21.0",
@@ -535,6 +536,113 @@
"integrity": "sha512-HpCo8tmWzLVad5s2d19EhAz5zqrrQ6s69qd6moPMQvkOuSwDT1YgRfWSVuc4ennqrgv3OHppiOGMQ7oC13yIww==",
"license": "MIT"
},
"node_modules/@formkit/core": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/core/-/core-1.7.2.tgz",
"integrity": "sha512-XDRVqkDtOziU3z44hdvgWEqGpiv6nTNeCmP8cnESKkr24a4keQuEwmfDVvibYV0ywyTMg6ylp2lyklCYeRHykQ==",
"license": "MIT",
"dependencies": {
"@formkit/utils": "^1.7.2"
}
},
"node_modules/@formkit/dev": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/dev/-/dev-1.7.2.tgz",
"integrity": "sha512-W38xbbFS4h4LTV22kUC6ZbPHBmlM2lVbAz5PSYU3SPaNc4FeXp4GTe4GzLEmcS82B+5L1zbSOgGB43kWZts7wA==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/utils": "^1.7.2"
}
},
"node_modules/@formkit/i18n": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/i18n/-/i18n-1.7.2.tgz",
"integrity": "sha512-Zs6f+rtP2j2Nnt1HkNrL85WU6rtS1l1Q0BAQjCMGQPsbrppiL7/TZ5bNIrVm0DTntLgG3firDS7bNWAxPSwerQ==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/utils": "^1.7.2",
"@formkit/validation": "^1.7.2"
}
},
"node_modules/@formkit/inputs": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/inputs/-/inputs-1.7.2.tgz",
"integrity": "sha512-4xs7RJN8EFGctTCNRXBTvor2O8RvuEEK3q2Hl/RMU5lhhn5tmck90fkkeAr9o+rWRoSNiV8XgbLQArQ/B1IYPw==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/utils": "^1.7.2"
}
},
"node_modules/@formkit/observer": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/observer/-/observer-1.7.2.tgz",
"integrity": "sha512-KUr5mcu2SAeHOOK1FaMyFigtA8hkHLsUkaPFWpTC81j79o1AUjJppmfPaX6PbtlHLeMs2Zq8Qhp6VAhi7D2WJg==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/utils": "^1.7.2"
}
},
"node_modules/@formkit/rules": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/rules/-/rules-1.7.2.tgz",
"integrity": "sha512-2e5qKlXzmL9LAetncFp7xkHEX/UAJsU3bo1iL3idloH3HALf5fIdg3lgOKwCNDMdLjbfiKJ6lVwFaeFfFpcDKw==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/utils": "^1.7.2",
"@formkit/validation": "^1.7.2"
}
},
"node_modules/@formkit/themes": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/themes/-/themes-1.7.2.tgz",
"integrity": "sha512-AaZHy7l9D44Ya3cQRqZ8RIpaTTsCjTB1gX13NlpqYSs6yG0yBTmJ7L7kW60bOGdhs/vmIwSel578EBwz98nybQ==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2"
}
},
"node_modules/@formkit/utils": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/utils/-/utils-1.7.2.tgz",
"integrity": "sha512-bBF6alUBOqFfJHjVB95Vck0hp36vlw4QfFJxGfTO6BX68AEaFzzzabtpwfy0DbcHtwHh4Yn7l/rOWGxXEve+QQ==",
"license": "MIT"
},
"node_modules/@formkit/validation": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/validation/-/validation-1.7.2.tgz",
"integrity": "sha512-JJsLP7AI/++VujdwtBeUcPhCqY3FXSKZ7hajT7Ow5Feab7JLqVPjxVMFbXDgEOLjq0ex1NX/I+EQHfm5sqMRsA==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/observer": "^1.7.2",
"@formkit/utils": "^1.7.2"
}
},
"node_modules/@formkit/vue": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/@formkit/vue/-/vue-1.7.2.tgz",
"integrity": "sha512-jrvXl2ZhS6X5klTbP4N5zc0Dg37AjbAf8SCVoz7mfligVkmwrrF4SN3waMBcH+n9gqT+vrkOYb6bQXdvjUdnCQ==",
"license": "MIT",
"dependencies": {
"@formkit/core": "^1.7.2",
"@formkit/dev": "^1.7.2",
"@formkit/i18n": "^1.7.2",
"@formkit/inputs": "^1.7.2",
"@formkit/observer": "^1.7.2",
"@formkit/rules": "^1.7.2",
"@formkit/themes": "^1.7.2",
"@formkit/utils": "^1.7.2",
"@formkit/validation": "^1.7.2"
},
"peerDependencies": {
"vue": "^3.4.0"
}
},
"node_modules/@halo-dev/api-client": {
"version": "2.26.0",
"resolved": "https://registry.npmjs.org/@halo-dev/api-client/-/api-client-2.26.0.tgz",
+1
View File
@@ -10,6 +10,7 @@
"test": "vitest run"
},
"dependencies": {
"@formkit/vue": "^1.7.2",
"@halo-dev/api-client": "2.26.0",
"@halo-dev/components": "2.26.0",
"@halo-dev/console-shared": "^2.21.0",
@@ -0,0 +1,39 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
// 结构性回归测试:不渲染组件,仅检查 SFC 源码中的测试告警链路。
// 「发送测试告警」为插件设置「告警通知」区的自定义 FormKit 输入(功能评审 P0-2):
// 点击 → POST 测试告警端点(无请求体)→ 同步反馈结果提示(成功/失败两色),
// 期间按钮 loading。历史上验证 webhook 配置只能等真实告警触发,本测试锁定
// 端点路径与反馈链路形态,防止路径漂移或链路残缺。
const sfc = readFileSync(
fileURLToPath(new URL('./TestAlertButton.vue', import.meta.url)),
'utf-8',
)
describe('TestAlertButton 测试告警', () => {
it('声明与后端一致的测试告警端点路径', () => {
expect(sfc).toContain(
"const TEST_ALERT_ENDPOINT = '/apis/audit.guichumo.com/v1alpha1/alerts/-/test'",
)
})
it('点击 POST 测试告警端点,按钮 loading、防重复点击', () => {
expect(sfc).toContain("await fetch(TEST_ALERT_ENDPOINT, { method: 'POST' })")
expect(sfc).toContain('if (sending.value)')
expect(sfc).toMatch(/<VButton :loading="sending"[^>]*@click="sendTestAlert">/)
expect(sfc).toContain('发送测试告警')
})
it('推送结果以提示反馈:成功/失败两色,非 2xx 抛出后进失败提示', () => {
expect(sfc).toContain('hintType.value = body.success ? \'success\' : \'error\'')
expect(sfc).toMatch(/throw new Error\(body\.message \|\| `发送失败(HTTP \$\{response\.status\})`\)/)
expect(sfc).toContain("hintType.value = 'error'")
expect(sfc).toContain('hint-success')
expect(sfc).toContain('hint-error')
expect(sfc).toMatch(/v-if="hint"/)
// 无论成败都复位按钮 loading
expect(sfc).toMatch(/finally\s*\{\s*sending\.value = false\s*\}/)
})
})
@@ -0,0 +1,66 @@
<script setup lang="ts">
import { ref } from 'vue'
import { VButton } from '@halo-dev/components'
/**
* 「发送测试告警」自定义 FormKit 输入(功能评审 P0-2):
* 在插件设置「告警通知」区提供手动推送入口——向当前已保存的 Webhook 地址
* 推送一条 type=test 测试消息,并同步反馈发送结果(成功/失败 + 原因摘要)。
* 不承载任何配置值(settings.yaml 中以 ignore: true 声明,不新增设置项)。
*/
/** 测试告警端点(与后端 AlertTestEndpoint 保持一致)。 */
const TEST_ALERT_ENDPOINT = '/apis/audit.guichumo.com/v1alpha1/alerts/-/test'
const sending = ref(false)
const hint = ref('')
const hintType = ref<'success' | 'error'>('success')
async function sendTestAlert() {
if (sending.value) {
return
}
sending.value = true
hint.value = ''
try {
const response = await fetch(TEST_ALERT_ENDPOINT, { method: 'POST' })
const body = (await response.json()) as { success?: boolean; message?: string }
if (!response.ok) {
throw new Error(body.message || `发送失败(HTTP ${response.status})`)
}
hintType.value = body.success ? 'success' : 'error'
hint.value = body.success ? `发送成功(${body.message})` : `发送失败:${body.message}`
} catch (e) {
hintType.value = 'error'
hint.value = e instanceof Error ? e.message : String(e)
} finally {
sending.value = false
}
}
</script>
<template>
<div class="test-alert">
<VButton :loading="sending" size="sm" @click="sendTestAlert">发送测试告警</VButton>
<span v-if="hint" class="test-alert-hint" :class="`hint-${hintType}`">{{ hint }}</span>
</div>
</template>
<style scoped>
.test-alert {
display: flex;
align-items: center;
gap: 0.75rem;
}
.test-alert-hint {
font-size: 0.75rem;
line-height: 1.4;
word-break: break-all;
}
.hint-success {
color: #0f766e;
}
.hint-error {
color: #d71d1d;
}
</style>
+21 -1
View File
@@ -2,9 +2,15 @@ import { describe, expect, it, vi } from 'vitest'
import type { RouteRecordAppend } from '@halo-dev/console-shared'
// 结构性回归测试:无需真实渲染,将视图组件与图标库打桩,
// 仅断言 definePlugin 暴露的路由注册形态。
// 仅断言 definePlugin 暴露的路由注册与自定义 FormKit 输入注册形态。
vi.mock('@/views/AuditLogList.vue', () => ({ default: { name: 'AuditLogListStub' } }))
vi.mock('@/components/TestAlertButton.vue', () => ({
default: { name: 'TestAlertButtonStub' },
}))
vi.mock('@halo-dev/components', () => ({ IconEye: { name: 'IconEyeStub' } }))
vi.mock('@formkit/vue', () => ({
createInput: (component: unknown) => ({ wrapped: component }),
}))
const { default: plugin } = await import('./index')
@@ -46,3 +52,17 @@ describe('console 路由注册', () => {
expect(menu.icon).toBeDefined()
})
})
describe('自定义 FormKit 输入注册(功能评审 P0-2)', () => {
const formkit = (plugin as typeof plugin & {
formkit?: { inputs: Record<string, unknown> }
}).formkit
it('注册「发送测试告警」输入类型(带插件标识前缀,避免与内置类型冲突)', () => {
expect(formkit).toBeDefined()
const input = formkit?.inputs.operationAuditTestAlert
expect(input).toBeDefined()
// createInput 包装后的定义携带被包装组件(打桩后可识别)
expect(input).toMatchObject({ wrapped: { name: 'TestAlertButtonStub' } })
})
})
+20 -2
View File
@@ -1,10 +1,26 @@
import { createInput } from '@formkit/vue'
import { definePlugin } from '@halo-dev/console-shared'
import type { PluginModule } from '@halo-dev/console-shared'
import { markRaw } from 'vue'
import { IconEye } from '@halo-dev/components'
import AuditLogList from '@/views/AuditLogList.vue'
import TestAlertButton from '@/components/TestAlertButton.vue'
export default definePlugin({
// 自定义 FormKit 输入(功能评审 P0-2):注册「发送测试告警」按钮类型,
// 供 settings.yaml 告警通知组引用($formkit: operationAuditTestAlert)。
// Halo 2.25+ 支持插件经 formkit.inputs 注册自定义输入(bundler kit 已将
// @formkit/vue 外部化为宿主共享模块,运行时由控制台提供)。
const formkitInputs = {
operationAuditTestAlert: createInput(TestAlertButton),
}
// PluginModule 未声明 formkit 字段(Halo 运行时契约先行于类型定义):
// 以变量 + 交叉类型绕过对象字面量的多余属性检查
const plugin: PluginModule & { formkit: { inputs: typeof formkitInputs } } = {
components: {},
formkit: {
inputs: formkitInputs,
},
routes: [
{
// 追加到控制台 Root 路由(BasicLayout)下,页面才能保留左侧导航,
@@ -30,4 +46,6 @@ export default definePlugin({
},
],
extensionPoints: {},
})
}
export default definePlugin(plugin)