From c222a007c1f7a150170850b8c2d7cb646910defd Mon Sep 17 00:00:00 2001 From: xiongxiao Date: Sun, 22 Mar 2026 02:03:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC=EF=BC=9B=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E4=BB=A4=E7=89=8C=EF=BC=9B=E6=B7=BB=E5=8A=A0=E8=BA=AB?= =?UTF-8?q?=E4=BB=BD=E9=AA=8C=E8=AF=81=E4=B8=AD=E9=97=B4=E4=BB=B6=E4=BB=A5?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bun.lock | 8 ++++---- package.json | 6 +++--- src/app.ts | 2 +- src/index.ts | 6 +++++- src/routes/cnb.ts | 30 ++++++++++++++++++++++++++---- src/routes/sync.ts | 14 ++++++++++++-- 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/bun.lock b/bun.lock index b58746a..7f7270b 100644 --- a/bun.lock +++ b/bun.lock @@ -5,16 +5,16 @@ "": { "name": "to-my-gitea", "dependencies": { - "@kevisual/cnb": "^0.0.53", + "@kevisual/cnb": "^0.0.56", "@kevisual/context": "^0.0.8", "@kevisual/gitea": "^0.0.6", - "@kevisual/router": "^0.1.4", + "@kevisual/router": "^0.1.6", "dayjs": "^1.11.20", }, }, }, "packages": { - "@kevisual/cnb": ["@kevisual/cnb@0.0.53", "https://registry.npmmirror.com/@kevisual/cnb/-/cnb-0.0.53.tgz", { "dependencies": { "@kevisual/query": "^0.0.53", "@kevisual/router": "^0.1.3", "@kevisual/use-config": "^1.0.30", "@opencode-ai/sdk": "^1.2.27", "es-toolkit": "^1.45.1", "nanoid": "^5.1.7", "unstorage": "^1.17.4", "ws": "npm:@kevisual/ws" }, "bin": { "cnb": "bin/index.js", "cloud": "bin/index.js", "cloud-npc": "bin/npc.js" } }, "sha512-moDiUkWNaHtOvQ2WXhILD2o7OdKv+/QW6fb4Iq8VXPBI+AYdVN4kmdBSIXR1RGulbYX82gaZcjypC/4vYDD/PQ=="], + "@kevisual/cnb": ["@kevisual/cnb@0.0.56", "https://registry.npmmirror.com/@kevisual/cnb/-/cnb-0.0.56.tgz", { "dependencies": { "@kevisual/query": "^0.0.53", "@kevisual/router": "^0.1.3", "@kevisual/use-config": "^1.0.30", "@opencode-ai/sdk": "^1.2.27", "es-toolkit": "^1.45.1", "nanoid": "^5.1.7", "unstorage": "^1.17.4", "ws": "npm:@kevisual/ws" }, "bin": { "cnb": "bin/index.js", "cloud": "bin/index.js", "cloud-npc": "bin/npc.js" } }, "sha512-Pa70TI/zVC7DOSecrRztB8QrRlf+iuFCGDH/O3YEQw/eMoyDdBtIxR3Ewnsu+n+BN0od6jcsSOhDW6kzqeX8cA=="], "@kevisual/context": ["@kevisual/context@0.0.8", "https://registry.npmmirror.com/@kevisual/context/-/context-0.0.8.tgz", {}, "sha512-DTJpyHI34NE76B7g6f+QlIqiCCyqI2qkBMQE736dzeRDGxOjnbe2iQY9W+Rt2PE6kmymM3qyOmSfNovyWyWrkA=="], @@ -24,7 +24,7 @@ "@kevisual/query": ["@kevisual/query@0.0.53", "https://registry.npmmirror.com/@kevisual/query/-/query-0.0.53.tgz", {}, "sha512-PAhpCLBr0emz0lGNlTVHMbJiC5wrtGLbInPddRzgKE35fiyNt+SWSsUWABiD0DeNrLN/OxWyAFobt880Z/e5MQ=="], - "@kevisual/router": ["@kevisual/router@0.1.4", "https://registry.npmmirror.com/@kevisual/router/-/router-0.1.4.tgz", { "dependencies": { "crypto-js": "^4.2.0", "es-toolkit": "^1.45.1", "zod": "^4.3.6" } }, "sha512-/Gz3N4KAyCRQmy+FgtK8OEhainYxBnck97LSKeazHLzQ5lOoQs46XCGDYR/AjrCuDTuUrmfD/l1u9pFe2rjN8Q=="], + "@kevisual/router": ["@kevisual/router@0.1.6", "https://registry.npmmirror.com/@kevisual/router/-/router-0.1.6.tgz", { "dependencies": { "crypto-js": "^4.2.0", "es-toolkit": "^1.45.1", "zod": "^4.3.6" } }, "sha512-uQYxDd4j0ZKuuPXduSMSvckjEKi99hVRp7vz5AUFVDVbEBmNQBgDGbwmz9+X/DR/Gjx++x3m8XvYcAwuEzPOKw=="], "@kevisual/use-config": ["@kevisual/use-config@1.0.30", "https://registry.npmmirror.com/@kevisual/use-config/-/use-config-1.0.30.tgz", { "dependencies": { "@kevisual/load": "^0.0.6" }, "peerDependencies": { "dotenv": "^17" } }, "sha512-kPdna0FW/X7D600aMdiZ5UTjbCo6d8d4jjauSc8RMmBwUU6WliFDSPUNKVpzm2BsDX5Nth1IXFPYMqH+wxqAmw=="], diff --git a/package.json b/package.json index 80c13ff..d0eb83a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "cli": "bun run src/cli.ts " }, "keywords": [], "author": "abearxiong (https://www.xiongxiao.me)", @@ -12,10 +12,10 @@ "packageManager": "pnpm@10.32.1", "type": "module", "dependencies": { - "@kevisual/cnb": "^0.0.53", + "@kevisual/cnb": "^0.0.56", "@kevisual/context": "^0.0.8", "@kevisual/gitea": "^0.0.6", - "@kevisual/router": "^0.1.5", + "@kevisual/router": "^0.1.6", "dayjs": "^1.11.20" } } diff --git a/src/app.ts b/src/app.ts index d86813f..f93951b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,9 +9,9 @@ export const app = useContextKey("app", () => { }); return app; }); +export const token = useKey("CNB_API_KEY") || useKey('CNB_TOKEN') export const cnb = useContextKey("cnb", () => { - const token = useKey("CNB_API_KEY") || useKey('CNB_TOKEN') return new CNB({ token }); }); diff --git a/src/index.ts b/src/index.ts index 9613780..17b1a99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,7 @@ export * from './app.ts'; +import { app } from './app.ts'; import './routes/cnb.ts'; -import './routes/sync.ts'; \ No newline at end of file +import './routes/sync.ts'; + +app.createAuth((ctx) => { }); +app.createRouteList() \ No newline at end of file diff --git a/src/routes/cnb.ts b/src/routes/cnb.ts index 5957bd3..5309d2d 100644 --- a/src/routes/cnb.ts +++ b/src/routes/cnb.ts @@ -1,3 +1,4 @@ +import { useRepoInfoEnv } from '@kevisual/cnb'; import { app, cnb } from '../app'; import dayjs from 'dayjs'; import { z } from 'zod'; @@ -17,6 +18,7 @@ app.route({ path: 'cnb', key: 'list-today', description: '获取今日更新的仓库列表', + middleware: ['auth'] }).define(async (ctx) => { const res = await cnb.repo.getRepoList({ status: 'active', page_size: 100 }); const list = res.data || []; @@ -46,6 +48,7 @@ app.route({ path: 'cnb', key: 'report', description: '上报仓库信息', + middleware: ['auth'], metadata: { args: { repoList: z.array(repoSchema).describe('仓库列表') @@ -63,16 +66,28 @@ app.route({ infoTable += `| ${repo.path} | [链接](${repo.web_url}) | ${repo.description} | ${topics} | ${repo.updatedAt} |\n`; }); console.log('上报的仓库信息表格:\n', infoTable); + const repoInfo = useRepoInfoEnv(); + let afterInfo = '来源:CNB,'; + if (repoInfo) { + afterInfo += `构建仓库:[${repoInfo.repoSlug}](${repoInfo.repoUrlHttps})`; + } cnb.issue.createIssue('kevisual/kevisual', { title: `仓库信息上报 - ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`, - body: `以下是上报的仓库信息:\n\n${infoTable}`, + body: `以下是上报的仓库信息:\n\n${infoTable}\n\n${afterInfo}`, // @ts-ignore labels: ['report'] }) } }).addTo(app) - -const closeOldReports = async () => { +app.route({ + path: 'cnb', + key: 'close-old-reports', + middleware: ['auth'], + description: '关闭过期的报告', +}).define(async (ctx) => { + await closeOldReports(); +}).addTo(app) +export const closeOldReports = async () => { const res = await cnb.issue.getList('kevisual/kevisual', { state: 'open', // @ts-ignore @@ -80,10 +95,17 @@ const closeOldReports = async () => { }); const issues = res.data || []; const oneDayAgo = dayjs().subtract(1, 'day'); + let closedCount = 0; for (const issue of issues) { const updatedAt = dayjs(issue.updated_at); if (updatedAt.isBefore(oneDayAgo)) { - await cnb.issue.updateIssue('kevisual/kevisual', issue.number, { state: 'closed', state_reason: 'done' }); + const res = await cnb.issue.updateIssue('kevisual/kevisual', issue.number, { state: 'closed', state_reason: 'completed' }); + if (res.code !== 200) { + console.error(`关闭 issue ${issue.number} 失败`, res); + } else { + closedCount++; + } } } + console.log(`已处理 ${issues.length} 个报告,关闭了 ${closedCount} 个过期报告`); } \ No newline at end of file diff --git a/src/routes/sync.ts b/src/routes/sync.ts index 1550fa2..a5c977e 100644 --- a/src/routes/sync.ts +++ b/src/routes/sync.ts @@ -1,4 +1,4 @@ -import { app, gitea, rootPath } from '../app' +import { app, gitea, rootPath, token } from '../app' import { Repo } from './cnb'; import { execSync } from 'node:child_process'; import path from 'node:path'; @@ -8,6 +8,7 @@ import { useKey } from '@kevisual/context'; app.route({ path: 'repo', key: 'sync', + middleware: ['auth'], description: '同步仓库数据', }).define(async (ctx) => { const res = await app.run({ path: 'cnb', key: 'list-today' }); @@ -49,7 +50,16 @@ app.route({ const sync = async (repo: Repo) => { const cwd = path.join(rootPath, repo.name); if (!fs.existsSync(cwd)) { - execSync(`git clone ${repo.web_url} ${cwd}`); + const webUrl = repo.web_url; + let cloneUrl: string; + if (webUrl.startsWith('http://')) { + cloneUrl = webUrl.replace('http://', `http://cnb:${token}@`); + } else if (webUrl.startsWith('https://')) { + cloneUrl = webUrl.replace('https://', `https://cnb:${token}@`); + } else { + cloneUrl = webUrl; + } + execSync(`git clone ${cloneUrl} ${cwd}`); } // 添加一个remote 叫做gitea的远程仓库,指向gitea的地址 const remoteUrl = repo.giteaUrl!;