zhaoyingbo da1c5f7580
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 32s
CI Monitor CI/CD / deploy (push) Successful in 40s
feat: 支持对PipeLine WebHooks的处理 #1
2024-07-26 07:17:32 +00:00

122 lines
3.2 KiB
TypeScript

import service from "../../service"
import netTool from "../../service/netTool"
import { Gitlab } from "../../types/gitlab"
import { Notify } from "../../types/notify"
import { sec2minStr } from "../../utils/timeTools"
/**
* 判断是否是合并请求
* @param pipeline
* @returns
*/
const checkIsMergeCommit = (pipeline: Gitlab.PipelineEvent) => {
const regex = /^Merge branch '.*' into '.*'$/
return regex.test(pipeline.commit.title)
}
/**
* 判断是否是成功的CI
* @param pipeline
* @returns
*/
const checkIsSuccess = (pipeline: Gitlab.PipelineEvent) => {
return pipeline.object_attributes.status === "success"
}
/**
* 获取合并请求
* @param pipeline
* @returns
*/
const getMergeRequest = async (pipeline: Gitlab.PipelineEvent) => {
if (!checkIsMergeCommit(pipeline)) return null
const res = await service.gitlab.commit.getMr(
pipeline.project.id,
pipeline.object_attributes.sha
)
if (res.length === 0) return null
return res.find((mr) => mr.merge_commit_sha === pipeline.commit.id) || null
}
/**
* 获取用户信息
* @param pipeline
* @param mergeRequest
* @returns
*/
const getUserInfo = (
pipeline: Gitlab.PipelineEvent,
mergeRequest: Gitlab.MergeRequest | null
) => {
let participant = pipeline.user.name
const receiver = [pipeline.user.username]
// 有MR且用户不同
if (mergeRequest && mergeRequest.author.username !== pipeline.user.username) {
participant += `${mergeRequest.author.name}`
receiver.push(mergeRequest.author.username)
}
return { participant, receiver }
}
/**
* 获取机器人消息
* @param variable 模板变量
* @returns
*/
const getRobotMsg = async (variable: Notify.CardVariable) =>
JSON.stringify({
type: "template",
data: {
config: {
update_multi: true,
},
template_id: "ctp_AA36QafWyob2",
template_variable: variable,
},
})
/**
* 发送通知消息
* @param pipeline
* @param apiKey
* @returns
*/
const sendNotifyMsg = async (
pipeline: Gitlab.PipelineEvent,
apiKey: string
) => {
// 只处理成功的CICD
if (!checkIsSuccess(pipeline)) return netTool.ok()
// 获取对应的合并请求
const mergeRequest = await getMergeRequest(pipeline)
// 获取用户信息
const { participant, receiver } = getUserInfo(pipeline, mergeRequest)
// 获取模板变量
const variable: Notify.CardVariable = {
project: pipeline.project.path_with_namespace,
project_link: pipeline.project.web_url,
pipeline: pipeline.object_attributes.id.toString(),
pipeline_link: `${pipeline.project.web_url}/-/pipelines`,
ref: pipeline.object_attributes.ref,
ref_link: `${pipeline.project.web_url}/-/commits/${pipeline.object_attributes.ref}`,
commit_user: pipeline.user.name,
duration: sec2minStr(pipeline.object_attributes.duration),
participant,
commit_message: pipeline.commit.title,
mr: mergeRequest ? mergeRequest.references.full : "无关联的MR",
mr_link: mergeRequest ? mergeRequest.web_url : "",
}
// 获取机器人消息
const robotMsg = await getRobotMsg(variable)
// 发送消息
await service.message.byUserIdList(receiver, robotMsg, apiKey)
// 返回成功
return netTool.ok()
}
const managePipelineEvent = {
sendNotifyMsg,
}
export default managePipelineEvent