zhaoyingbo 92fa30ef3d
All checks were successful
CI Monitor MIflow / build-image (push) Successful in 2m42s
feat: 支持初步的CR
2024-08-12 12:24:45 +00:00

36 lines
1.3 KiB
TypeScript

import { Logger } from "winston"
import manageMrEvent from "../../controllers/manageMrEvent"
import managePipelineEvent from "../../controllers/managePipelineEvent"
import netTool from "../../service/netTool"
import { Gitlab } from "../../types/gitlab"
/**
* 处理管理Gitlab事件的请求。
* @param {Request} req - 请求对象。
* @returns {Promise<Response>} - 响应对象。
*/
export const manageGitlabEventReq = async (
req: Request,
logger: Logger
): Promise<Response> => {
const apiKey = req.headers.get("x-gitlab-token")
logger.info(`x-gitlab-token: ${apiKey}`)
if (!apiKey) return netTool.badRequest("x-gitlab-token is required!")
const eventType = req.headers.get("x-gitlab-event")
logger.info(`x-gitlab-event: ${eventType}`)
// 处理流水线钩子
if (eventType === "Pipeline Hook") {
const body = (await req.json()) as Gitlab.PipelineEvent
const params = new URLSearchParams(req.url.split("?")[1])
return managePipelineEvent.manageRawEvent(body, apiKey, params)
}
// 处理MR钩子
if (eventType === "Merge Request Hook") {
const body = (await req.json()) as Gitlab.MergeRequestEvent
logger.debug(`body: ${JSON.stringify(body)}`)
return manageMrEvent.manageRawEvent(body, logger)
}
return netTool.ok()
}