Skip to content

Commit 84cbfa9

Browse files
committed
feat(core): 添加仓库绑定功能并重构数据库结构
- 新增BindRepo命令,支持通过#git绑定仓库[platform]:owner/repo格式绑定仓库 - 重构数据库结构,将repo表中的botId和groupId字段分离到新的session表中 - 新增bind表用于管理群组与仓库的绑定关系 - 修改push表结构,添加sessionId字段关联session表 - 更新release表结构,添加sessionId字段关联session表 - 优化AddRepo和RemoveRepo命令的数据查询逻辑 - 添加默认分支获取异常处理,失败时使用main分支作为默认值 - 修复重复订阅提示信息,改为更准确的错误提示 - 更新帮助文档中的命令格式说明 refactor(config): 重构配置文件结构 - 将原有配置文件中的proxy和token字段提取到独立的proxy.yaml和token.yaml文件 - 更新配置类型定义,分离cron、proxy和token配置接口 - 为各个平台(github、gitee、gitcode、cnbcool)添加web配置界面 - 新增web.config.ts文件提供完整的前端配置界面 feat(models): 实现session和bind数据模型 - 新增session表用于管理机器人会话信息 - 新增bind表用于管理群组与仓库的绑定关系 - 实现session和bind相关的CRUD操作函数 - 更新push、release等表的外键关系,使用session表进行关联 - 优化数据库初始化脚本,添加必要的索引和约束 fix(commit): 优化提交信息查询逻辑 - 添加仓库绑定检查机制,支持未指定仓库时使用绑定的仓库 - 统一各平台token配置路径,从平台特定配置改为统一token配置 - 优化错误处理,提供更友好的错误提示信息 - 限制commit命令仅在群聊环境中执行 - 修复SHA参数传递问题,确保正确的提交信息获取
1 parent 6575fc9 commit 84cbfa9

35 files changed

Lines changed: 646 additions & 244 deletions

.github/workflows/issue_close.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

.github/workflows/issue_similarity.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
cron: '0 */5 * * * *'
2-
proxy: ''
3-
token: ''
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
cron: '0 */5 * * * *'
2-
proxy: ''
3-
token: ''
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
cron: '0 */5 * * * *'
2-
proxy: ''
3-
token: ''
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
cron: '0 */5 * * * *'
2-
proxy: ''
3-
reverseProxy: ''
4-
token: ''
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 系统代理
2+
proxy:
3+
4+
# 反向代理
5+
reverseProxy:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Github 访问令牌 https://github.com/settings/tokens
2+
github:
3+
4+
# Gitee 访问令牌 https://gitee.com/profile/personal_access_tokens
5+
gitee:
6+
7+
# GitCode 访问令牌 https://gitcode.com/setting/token-classic
8+
gitcode:
9+
10+
# CnbCool 访问令牌 https://cnb.cool/profile/token
11+
cnbcool:

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/express": "^5.0.6",
4949
"@types/react": "^19.2.9",
5050
"cross-env": "^7.0.3",
51-
"node-karin": "^1.13.0"
51+
"node-karin": "^1.14.2"
5252
},
5353
"karin": {
5454
"main": "src/index.ts",

packages/core/src/apps/admin.ts

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,43 @@ export const AddRepo = karin.command(
4545
return await e.reply('未找到该平台, 请重试')
4646
}
4747

48-
let repoInfo = await db.repo.GetRepo(
49-
platformName,
50-
owner,
51-
repo,
52-
botId,
53-
groupId,
54-
)
48+
let [repoInfo, sessionInfo] = await Promise.all([
49+
db.repo.GetRepo(platformName, owner, repo),
50+
db.session.GetSession(botId, groupId),
51+
])
5552
if (!repoInfo) {
56-
await db.repo.AddRepo(platformName, owner, repo, botId, groupId)
57-
repoInfo = await db.repo.GetRepo(
58-
platformName,
59-
owner,
60-
repo,
61-
botId,
62-
groupId,
63-
)
53+
await db.repo.AddRepo(platformName, owner, repo)
54+
repoInfo = await db.repo.GetRepo(platformName, owner, repo)
55+
}
56+
if (!sessionInfo) {
57+
await db.session.AddSession(botId, groupId)
58+
sessionInfo = await db.session.GetSession(botId, groupId)
6459
}
65-
if (!repoInfo) return await e.reply('添加订阅仓库失败,请重试')
60+
if (!repoInfo || !sessionInfo)
61+
return await e.reply('添加订阅仓库失败,请重试')
6662

6763
let msg = `添加订阅仓库成功, 平台: ${platformName}, 仓库: ${owner}/${repo}, 订阅类型: ${eventType.join(',')}`
6864

6965
const PushEvent = eventType.includes(EventType.Push)
7066
if (PushEvent) {
7167
const repoClient = client.repo()
72-
const PushBranch = (await repoClient.info({ owner, repo })).defaultBranch
73-
const pushRepo = await db.push.GetPush(repoInfo.id, PushBranch)
68+
let defaultBranch: string
69+
try {
70+
const repo_info = await repoClient.info({ owner, repo })
71+
defaultBranch = repo_info.defaultBranch
72+
} catch (e) {
73+
defaultBranch = 'main'
74+
}
75+
const pushRepo = await db.push.GetPush(
76+
repoInfo.id,
77+
sessionInfo.id,
78+
defaultBranch,
79+
)
7480
if (!pushRepo) {
75-
await db.push.AddPush(repoInfo.id, PushBranch)
76-
msg += `, 分支: ${PushBranch}`
81+
await db.push.AddPush(repoInfo.id, sessionInfo.id, defaultBranch)
82+
msg += `, 分支: ${defaultBranch}`
7783
} else {
78-
msg = `仓库 ${owner}/${repo} 的推送订阅已存在,平台: ${platformName}, 分支: ${PushBranch}`
84+
msg = `仓库 ${owner}/${repo} 的推送订阅已存在, 请勿重复订阅`
7985
}
8086
}
8187

@@ -91,6 +97,50 @@ export const AddRepo = karin.command(
9197
},
9298
)
9399

100+
export const BindRepo = karin.command(
101+
/^#?git(?:|bind)(?:|repo)(?:([a-zA-Z]+):([^/\s]+)\/([^:\s]+))$/i,
102+
async (e) => {
103+
const [, platform, owner, repo] = e.msg.match(BindRepo!.reg)!
104+
let platformName = Platform.GitHub
105+
106+
switch (platform.toLowerCase()) {
107+
case 'github':
108+
platformName = Platform.GitHub
109+
break
110+
case 'gitcode':
111+
platformName = Platform.GitCode
112+
break
113+
case 'gitee':
114+
platformName = Platform.Gitee
115+
break
116+
case 'cnb':
117+
case 'cnbcool':
118+
platformName = Platform.CnbCool
119+
break
120+
default:
121+
return await e.reply('未找到该平台, 请重试')
122+
}
123+
let repoInfo = await db.repo.GetRepo(platformName, owner, repo)
124+
if (!repoInfo) {
125+
await db.repo.AddRepo(platformName, owner, repo)
126+
repoInfo = await db.repo.GetRepo(platformName, owner, repo)
127+
}
128+
if (!repoInfo) {
129+
return await e.reply('绑定仓库失败, 请重试')
130+
}
131+
const BindInfo = await db.bind.GetBind(e.groupId)
132+
if (BindInfo) {
133+
return await e.reply('该群已绑定该仓库, 请勿重复绑定')
134+
}
135+
await db.bind.AddBind(e.groupId, repoInfo.id)
136+
await e.reply(`绑定仓库成功, 平台: ${platformName}, 仓库: ${owner}/${repo}`)
137+
},
138+
{
139+
name: 'admin:bindRepo',
140+
event: 'message.group',
141+
},
142+
)
143+
94144
export const RemoveRepo = karin.command(
95145
/^#?git(?:||remove)(?:|repo)(?:([a-zA-Z]+):([^/\s]+)\/([^:\s]+))$/i,
96146
async (e) => {
@@ -117,17 +167,14 @@ export const RemoveRepo = karin.command(
117167
return await e.reply('未找到该平台, 请重试')
118168
}
119169

120-
const repoInfo = await db.repo.GetRepo(
121-
platformName,
122-
botId,
123-
groupId,
124-
owner,
125-
repo,
126-
)
127-
if (!repoInfo) {
170+
const [repoInfo, sessionInfo] = await Promise.all([
171+
db.repo.GetRepo(platformName, owner, repo),
172+
db.session.GetSession(botId, groupId),
173+
])
174+
if (!repoInfo || !sessionInfo) {
128175
return await e.reply('未找到该订阅仓库, 删除失败,请重试')
129176
}
130-
await db.push.RemovePush(repoInfo.id)
177+
await db.push.RemovePush(repoInfo.id, sessionInfo.id)
131178

132179
await e.reply(`删除订阅仓库成功`)
133180
},

0 commit comments

Comments
 (0)