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
@@ -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)