This commit is contained in:
parent
50121dc073
commit
713b67a52c
4
index.ts
4
index.ts
@ -13,9 +13,9 @@ const server = Bun.serve({
|
|||||||
async fetch(req) {
|
async fetch(req) {
|
||||||
try {
|
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 监控
|
// CI 监控
|
||||||
if (exactCheck("/ci")) return manageCIMonitorReq(req)
|
if (exactCheck("/ci")) return manageCIMonitorReq(req)
|
||||||
// Gitlab 事件
|
// Gitlab 事件
|
||||||
|
29
test/checkpath.test.ts
Normal file
29
test/checkpath.test.ts
Normal file
@ -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)
|
||||||
|
})
|
64
test/manageGitlabEventReq.test.ts
Normal file
64
test/manageGitlabEventReq.test.ts
Normal file
@ -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())
|
||||||
|
})
|
@ -22,5 +22,11 @@ export const makeCheckPathTool = (url: string, prefix?: string) => {
|
|||||||
* @returns {boolean} 如果路径以基础 URL 的路径为前缀则返回 true,否则返回 false。
|
* @returns {boolean} 如果路径以基础 URL 的路径为前缀则返回 true,否则返回 false。
|
||||||
*/
|
*/
|
||||||
startsWithCheck: (path: string) => pathname.startsWith(makePath(path)),
|
startsWithCheck: (path: string) => pathname.startsWith(makePath(path)),
|
||||||
|
/**
|
||||||
|
* 检查完整路径
|
||||||
|
* @param {string} path - 要检查的路径。
|
||||||
|
* @returns {boolean} 如果路径与基础 URL 的路径完全匹配则返回 true,否则返回 false。
|
||||||
|
*/
|
||||||
|
fullCheck: (path: string) => pathname === path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user