All checks were successful
CI Monitor MIflow / build-image (push) Successful in 1m1s
103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import { ExpandedPipelineSchema, PipelineSchema } from "@gitbeaker/rest"
|
||
import moment from "moment"
|
||
import pLimit from "p-limit"
|
||
|
||
import db from "../../db"
|
||
import service from "../../service"
|
||
import { DB } from "../../types/db"
|
||
|
||
/**
|
||
* 获取全部的pipeline列表
|
||
* @param {DB.Project} project - 项目对象
|
||
* @returns {Promise<(ExpandedPipelineSchema & { created_at: string })[]>} - 返回包含详细信息的pipeline列表
|
||
*/
|
||
const getFullPipelineList = async (project: DB.Project) => {
|
||
// 先获取最新的pipelineID
|
||
const latestOne = await db.pipeline.getLatestOne(project.id)
|
||
// 获取本次数据获取的截止时间,如果没有,则获取从20240101到现在所有pipeline信息
|
||
const latestTime = moment(
|
||
latestOne?.created_at || "2024-01-01T00:00:00.000+08:00"
|
||
)
|
||
// 获取pipeline列表并保存
|
||
const fullPipelineList: PipelineSchema[] = []
|
||
let page = 1
|
||
let hasBeforeLatestTime = false
|
||
while (!hasBeforeLatestTime) {
|
||
const pipelines = await service.gitlab.Pipelines.all(project.project_id, {
|
||
page: page++,
|
||
perPage: 100,
|
||
scope: "finished",
|
||
}).catch(() => [])
|
||
// 如果当前页没有数据,则直接跳出
|
||
if (pipelines.length === 0) break
|
||
pipelines.forEach((pipeline) => {
|
||
// 如果已经有了比最新的pipeline还要早的pipeline,则跳出
|
||
if (hasBeforeLatestTime) return
|
||
if (moment(pipeline.created_at).isSameOrBefore(latestTime)) {
|
||
hasBeforeLatestTime = true
|
||
} else {
|
||
fullPipelineList.push(pipeline)
|
||
}
|
||
})
|
||
}
|
||
const limit = pLimit(10)
|
||
const fullPipelineDetailList = await Promise.all(
|
||
fullPipelineList.map(({ project_id, id, created_at }) =>
|
||
limit(() =>
|
||
service.gitlab.Pipelines.show(project_id, id)
|
||
.catch(() => null)
|
||
.then((res) => (res ? { ...res, created_at } : null))
|
||
)
|
||
)
|
||
)
|
||
return fullPipelineDetailList.filter((v) => v) as (ExpandedPipelineSchema & {
|
||
created_at: string
|
||
})[]
|
||
}
|
||
|
||
/**
|
||
* 插入全部的pipeline列表到数据库
|
||
* @param {(ExpandedPipelineSchema & { created_at: string })[][]} fullPipelineList - 包含详细信息的pipeline列表
|
||
* @param {Record<string, string>} fullUserMap - 用户映射表
|
||
* @param {Record<string, string>} fullProjectMap - 项目映射表
|
||
* @returns {Promise<void>}
|
||
*/
|
||
const insertFullPipelineList = async (
|
||
fullPipelineList: (ExpandedPipelineSchema & { created_at: string })[][],
|
||
fullUserMap: Record<string, string>,
|
||
fullProjectMap: Record<string, string>
|
||
) => {
|
||
const dbPipelineList: Partial<DB.Pipeline> = []
|
||
|
||
fullPipelineList.forEach((pipelineList) => {
|
||
pipelineList.forEach((pipeline) => {
|
||
// 如果没有时间信息,则跳过
|
||
if (!pipeline.created_at || !pipeline.started_at || !pipeline.finished_at)
|
||
return
|
||
dbPipelineList.push({
|
||
project_id: fullProjectMap[pipeline.project_id],
|
||
user_id: fullUserMap[pipeline.user.id],
|
||
pipeline_id: pipeline.id,
|
||
ref: pipeline.ref,
|
||
status: pipeline.status,
|
||
web_url: pipeline.web_url,
|
||
created_at: pipeline.created_at,
|
||
started_at: pipeline.started_at,
|
||
finished_at: pipeline.finished_at,
|
||
duration: pipeline.duration,
|
||
queued_duration: pipeline.queued_duration,
|
||
})
|
||
})
|
||
})
|
||
await Promise.all(
|
||
dbPipelineList.map((v: Partial<DB.Pipeline>) => db.pipeline.create(v))
|
||
)
|
||
}
|
||
|
||
const managePipeline = {
|
||
getFullPipelineList,
|
||
insertFullPipelineList,
|
||
}
|
||
|
||
export default managePipeline
|