87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import moment from "moment"
|
||
|
||
import db from "../../db"
|
||
import service from "../../service"
|
||
import { DB } from "../../types/db"
|
||
import { Gitlab } from "../../types/gitlab"
|
||
|
||
/**
|
||
* 获取全部的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: Gitlab.Pipeline[] = []
|
||
let page = 1
|
||
let hasBeforeLatestTime = false
|
||
while (!hasBeforeLatestTime) {
|
||
const pipelines = await service.gitlab.pipeline.getList(
|
||
project.project_id,
|
||
page++
|
||
)
|
||
// 如果当前页没有数据,则直接跳出
|
||
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 fullPipelineDetailList = await Promise.all(
|
||
fullPipelineList.map(({ project_id, id, created_at }) =>
|
||
service.gitlab.pipeline.getDetail(project_id, id, created_at)
|
||
)
|
||
)
|
||
return fullPipelineDetailList.filter((v) => v) as (Gitlab.PipelineDetail & {
|
||
created_at: string
|
||
})[]
|
||
}
|
||
|
||
const insertFullPipelineList = async (
|
||
fullPipelineList: (Gitlab.PipelineDetail & { 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
|