diff --git a/controllers/managePipelineEvent/index.ts b/controllers/managePipelineEvent/index.ts index 9d8e1f1..b503c4b 100644 --- a/controllers/managePipelineEvent/index.ts +++ b/controllers/managePipelineEvent/index.ts @@ -282,19 +282,19 @@ const manageRawEvent = async ( const action = await getNextAction(pipeline, stage) // 发送通知 if (action === NEXT_ACTION.NOTIFY) { - sendNotify(pipeline, apiKey) + await sendNotify(pipeline, apiKey) } // 添加监控 if (action === NEXT_ACTION.ADD_MONITOR) { - addMonitor(pipeline, apiKey, stage!) + await addMonitor(pipeline, apiKey, stage!) } // 删除监控 if (action === NEXT_ACTION.REMOVE_MONITOR) { - removeMonitor(pipeline, apiKey, stage!) + await removeMonitor(pipeline, apiKey, stage!) } // 删除监控并发送通知 if (action === NEXT_ACTION.NOTIFY_AND_REMOVE_MONITOR) { - removeMonitorAndNotify(pipeline, apiKey, stage!) + await removeMonitorAndNotify(pipeline, apiKey, stage!) } // 返回成功 return netTool.ok() diff --git a/index.ts b/index.ts index ae67950..cd6ee79 100644 --- a/index.ts +++ b/index.ts @@ -12,10 +12,10 @@ const PREFIX = "/gitlab_monitor" const server = Bun.serve({ async fetch(req) { try { - // 打印当前路由 - console.log("🚀 ~ serve ~ req.url", req.url) // 路由处理 - const { exactCheck } = makeCheckPathTool(req.url, PREFIX) + const { exactCheck, fullCheck } = makeCheckPathTool(req.url, PREFIX) + // 非根路由打印 + if (!fullCheck("/")) console.log("🚀 ~ serve ~ req.url", req.url) // CI 监控 if (exactCheck("/ci")) return manageCIMonitorReq(req) // Gitlab 事件 diff --git a/routes/ci/index.ts b/routes/ci/index.ts index 9cd1727..6a1acef 100644 --- a/routes/ci/index.ts +++ b/routes/ci/index.ts @@ -6,10 +6,10 @@ import netTool from "../../service/netTool" * @param {Request} req - 请求对象。 * @returns {Response} - 响应对象。 */ -export const manageCIMonitorReq = (req: Request): Response => { +export const manageCIMonitorReq = async (req: Request): Promise => { const url = new URL(req.url) const chat_id = url.searchParams.get("chat_id") if (!chat_id) return netTool.badRequest("chat_id is required!") - manageRobot.sendCIReportByChatId(chat_id) + await manageRobot.sendCIReportByChatId(chat_id) return netTool.ok("reporting...") } diff --git a/test/checkpath.test.ts b/test/checkpath.test.ts new file mode 100644 index 0000000..70779e5 --- /dev/null +++ b/test/checkpath.test.ts @@ -0,0 +1,29 @@ +import { expect, test } from "bun:test" + +import { makeCheckPathTool } from "../utils/pathTools" + +test("makeCheckPathTool exactCheck", () => { + const tool = makeCheckPathTool("https://example.com/base") + expect(tool.exactCheck("/base")).toBe(true) + expect(tool.exactCheck("/base/extra")).toBe(false) +}) + +test("makeCheckPathTool startsWithCheck", () => { + const tool = makeCheckPathTool("https://example.com/base") + expect(tool.startsWithCheck("/base")).toBe(true) + expect(tool.startsWithCheck("/base/extra")).toBe(false) + expect(tool.startsWithCheck("/other")).toBe(false) +}) + +test("makeCheckPathTool fullCheck", () => { + const tool = makeCheckPathTool("https://example.com/") + expect(tool.fullCheck("/")).toBe(true) + expect(tool.fullCheck("/base")).toBe(false) +}) + +test("makeCheckPathTool with prefix", () => { + const tool = makeCheckPathTool("https://example.com/prefix/base", "/prefix") + expect(tool.exactCheck("/base")).toBe(true) + expect(tool.startsWithCheck("/base")).toBe(true) + expect(tool.fullCheck("/prefix")).toBe(false) +}) diff --git a/test/manageCIMonitorReq.test.ts b/test/manageCIMonitorReq.test.ts new file mode 100644 index 0000000..e2f61bb --- /dev/null +++ b/test/manageCIMonitorReq.test.ts @@ -0,0 +1,12 @@ +import { expect, test } from "bun:test" + +import { manageCIMonitorReq } from "../routes/ci" +import netTool from "../service/netTool" + +test("manageCIMonitorReq", async () => { + const req = new Request( + "https://ci-monitor.xiaomiwh.cn/gitlab/ci?chat_id=oc_8c789ce8f4ecc6695bb63ca6ec4c61ea" + ) + const res = await manageCIMonitorReq(req) + expect(res).toEqual(netTool.ok("reporting...")) +}) diff --git a/test/manageGitlabEventReq.test.ts b/test/manageGitlabEventReq.test.ts new file mode 100644 index 0000000..cef1277 --- /dev/null +++ b/test/manageGitlabEventReq.test.ts @@ -0,0 +1,64 @@ +import { expect, test } from "bun:test" + +import { manageGitlabEventReq } from "../routes/event" +import netTool from "../service/netTool" +import { Gitlab } from "../types/gitlab" + +test("manageGitlabEventReq", async () => { + const headers = new Headers({ + "x-gitlab-token": "uwnpzb9hvoft28h", + "x-gitlab-event": "Pipeline Hook", + }) + + const body: Gitlab.PipelineEvent = { + object_attributes: { + id: 12345, + ref: "main", + sha: "abc123def456", + status: "success", + created_at: "2023-10-01T12:00:00Z", + finished_at: "2023-10-01T12:05:00Z", + duration: 300, + }, + user: { + name: "John Doe", + username: "zhaoyingbo", + }, + project: { + id: 67890, + web_url: "https://gitlab.example.com/johndoe/project", + path_with_namespace: "johndoe/project", + }, + commit: { + id: "abc123def456", + title: "Fix bug in pipeline", + url: "https://gitlab.example.com/johndoe/project/commit/abc123def456", + }, + builds: [ + { + id: 111, + stage: "build", + status: "success", + finished_at: "2023-10-01T12:02:00Z", + }, + { + id: 112, + stage: "test", + status: "success", + finished_at: "2023-10-01T12:04:00Z", + }, + ], + } + + const req = new Request( + "https://lark-egg.ai.xiaomi.com/gitlab_monitor/event", + { + method: "POST", + headers: headers, + body: JSON.stringify(body), + } + ) + + const res = await manageGitlabEventReq(req) + expect(res).toEqual(netTool.ok()) +}) diff --git a/utils/pathTools.ts b/utils/pathTools.ts index aebd26f..144a754 100644 --- a/utils/pathTools.ts +++ b/utils/pathTools.ts @@ -22,5 +22,11 @@ export const makeCheckPathTool = (url: string, prefix?: string) => { * @returns {boolean} 如果路径以基础 URL 的路径为前缀则返回 true,否则返回 false。 */ startsWithCheck: (path: string) => pathname.startsWith(makePath(path)), + /** + * 检查完整路径是否与基础 URL 的路径精确匹配。 + * @param {string} path - 要检查的路径。 + * @returns {boolean} 如果完整路径与基础 URL 的路径精确匹配则返回 true,否则返回 false。 + */ + fullCheck: (path: string) => pathname === path, } }