zhaoyingbo 3f220f7943
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 29s
CI Monitor CI/CD / deploy (push) Successful in 34s
style: 优化项目结构
2024-07-24 10:35:50 +00:00

86 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import db from "../../db";
import service from "../../service";
import moment from "moment";
import { Gitlab } from "../../types/gitlab";
import { DB } from "../../types/db";
/**
* 获取全部的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;