更新依赖版本;重构代码以使用环境变量中的令牌;添加身份验证中间件以增强安全性
This commit is contained in:
@@ -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 });
|
||||
});
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
export * from './app.ts';
|
||||
import { app } from './app.ts';
|
||||
import './routes/cnb.ts';
|
||||
import './routes/sync.ts';
|
||||
import './routes/sync.ts';
|
||||
|
||||
app.createAuth((ctx) => { });
|
||||
app.createRouteList()
|
||||
@@ -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} 个过期报告`);
|
||||
}
|
||||
@@ -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!;
|
||||
|
||||
Reference in New Issue
Block a user