From 713b67a52c6cbb4ffd54676493af8faa61a1766a Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Fri, 9 Aug 2024 02:02:05 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.ts | 4 +- test/checkpath.test.ts | 29 +++++++++ ...{ci.test.ts => manageCIMonitorReq.test.ts} | 0 test/manageGitlabEventReq.test.ts | 64 +++++++++++++++++++ utils/pathTools.ts | 6 ++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 test/checkpath.test.ts rename test/{ci.test.ts => manageCIMonitorReq.test.ts} (100%) create mode 100644 test/manageGitlabEventReq.test.ts diff --git a/index.ts b/index.ts index 8cf7849..1a811dd 100644 --- a/index.ts +++ b/index.ts @@ -13,9 +13,9 @@ const server = Bun.serve({ async fetch(req) { try { // 路由处理 - const { exactCheck } = makeCheckPathTool(req.url, PREFIX) + const { exactCheck, fullCheck } = makeCheckPathTool(req.url, PREFIX) // 非根路径 - if (!exactCheck("/")) console.log("🚀 ~ serve ~ req.url", req.url) + if (!fullCheck("/")) console.log("🚀 ~ serve ~ req.url", req.url) // CI 监控 if (exactCheck("/ci")) return manageCIMonitorReq(req) // Gitlab 事件 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/ci.test.ts b/test/manageCIMonitorReq.test.ts similarity index 100% rename from test/ci.test.ts rename to test/manageCIMonitorReq.test.ts 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..65031c4 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)), + /** + * 检查完整路径 + * @param {string} path - 要检查的路径。 + * @returns {boolean} 如果路径与基础 URL 的路径完全匹配则返回 true,否则返回 false。 + */ + fullCheck: (path: string) => pathname === path, } }