147 lines
4.2 KiB
TypeScript
147 lines
4.2 KiB
TypeScript
import { GitlabProjectModel } from "../../db/gitlabProject/index."
|
||
import { Context } from "../../types"
|
||
|
||
/**
|
||
* 刷新项目的Gitlab Event
|
||
* @param param0
|
||
* @param project
|
||
* @returns
|
||
*/
|
||
const refreshProjectHooks = async (
|
||
{ gitlabService }: Context,
|
||
project: GitlabProjectModel
|
||
) => {
|
||
gitlabService.setProjectId(project.projectId)
|
||
const eventList = await gitlabService.hook.getList()
|
||
const HOOK_URL =
|
||
Bun.env.NODE_ENV === "production"
|
||
? "https://lark-egg.ai.xiaomi.com/gitlab"
|
||
: "https://lark-egg-preview.ai.xiaomi.com/gitlab"
|
||
// 找到目标Event
|
||
const targetEvent = eventList.find((event) => event.url === HOOK_URL)
|
||
|
||
// 清除Event
|
||
if (!project.openCICDNotify && !project.openMRSummary) {
|
||
if (!targetEvent) return
|
||
await gitlabService.hook.delete(targetEvent.id)
|
||
return
|
||
}
|
||
|
||
// 添加Event
|
||
if (targetEvent) return
|
||
await gitlabService.hook.add({
|
||
url: HOOK_URL,
|
||
push_events: false,
|
||
job_events: true,
|
||
merge_requests_events: true,
|
||
})
|
||
}
|
||
|
||
const openFunc = async (
|
||
ctx: Context,
|
||
projectId: number,
|
||
func: "openCICDNotify" | "openMRSummary"
|
||
) => {
|
||
const { logger, larkBody, larkService, larkCard, db } = ctx
|
||
const cardGender = larkCard.child("gitlabAgent")
|
||
if (!projectId) {
|
||
logger.error("项目ID格式错误", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genErrorCard("项目ID格式错误,请检查项目ID是否正确")
|
||
)
|
||
return
|
||
}
|
||
const project = await db.gitlabProject.getAndCreate(
|
||
projectId,
|
||
ctx.gitlabService
|
||
)
|
||
if (!project) {
|
||
logger.error("项目不存在", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genErrorCard(
|
||
"该项目不存在,请检查项目ID是否正确,或确认 @itp-cr-agent 是否具有对项目的Maintainer权限"
|
||
)
|
||
)
|
||
return
|
||
}
|
||
const newProj = await db.gitlabProject.update(project.id, {
|
||
[func]: true,
|
||
})
|
||
if (!newProj) {
|
||
logger.error("更新项目失败", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genErrorCard("操作失败,请稍后再试")
|
||
)
|
||
return
|
||
}
|
||
await refreshProjectHooks(ctx, newProj)
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genSuccessCard(
|
||
`已开启${func === "openCICDNotify" ? "CI/CD成功提醒" : "MR自动总结"}`
|
||
)
|
||
)
|
||
}
|
||
|
||
const closeFunc = async (
|
||
ctx: Context,
|
||
projectId: number,
|
||
func: "openCICDNotify" | "openMRSummary"
|
||
) => {
|
||
const { logger, larkBody, larkService, larkCard, db } = ctx
|
||
const cardGender = larkCard.child("gitlabAgent")
|
||
if (!projectId) {
|
||
logger.error("项目ID格式错误", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genErrorCard("项目ID格式错误,请检查项目ID是否正确")
|
||
)
|
||
return
|
||
}
|
||
const project = await db.gitlabProject.getByProjectId(projectId)
|
||
if (!project) {
|
||
logger.error("项目不存在", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genSuccessCard(
|
||
`已关闭${func === "openCICDNotify" ? "CI/CD成功提醒" : "MR自动总结"}`
|
||
)
|
||
)
|
||
return
|
||
}
|
||
const newProj = await db.gitlabProject.update(project.id, {
|
||
[func]: false,
|
||
})
|
||
if (!newProj) {
|
||
logger.error("更新项目失败", { projectId })
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genErrorCard("操作失败,请稍后再试")
|
||
)
|
||
return
|
||
}
|
||
await refreshProjectHooks(ctx, newProj)
|
||
await larkService.message.replyCard(
|
||
larkBody.messageId,
|
||
cardGender.genSuccessCard(
|
||
`已关闭${func === "openCICDNotify" ? "CI/CD成功提醒" : "MR自动总结"}`
|
||
)
|
||
)
|
||
}
|
||
|
||
const register = {
|
||
openCICDNotify: async (ctx: Context, projectId: number) =>
|
||
openFunc(ctx, projectId, "openCICDNotify"),
|
||
closeCICDNotify: async (ctx: Context, projectId: number) =>
|
||
closeFunc(ctx, projectId, "openCICDNotify"),
|
||
openMRSummary: async (ctx: Context, projectId: number) =>
|
||
openFunc(ctx, projectId, "openMRSummary"),
|
||
closeMRSummary: async (ctx: Context, projectId: number) =>
|
||
closeFunc(ctx, projectId, "openMRSummary"),
|
||
}
|
||
|
||
export default register
|