83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import db from "../../db";
|
||
import { PipelineRecordModel } from "../../db/pipeline";
|
||
import { ProjectRecordModel } from "../../db/project";
|
||
import service from "../../service";
|
||
import moment from "moment";
|
||
|
||
/**
|
||
* 获取全部的pipeline列表
|
||
*/
|
||
const getFullPipelineList = async (project: ProjectRecordModel) => {
|
||
// 先获取最新的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: GitlabPipeline[] = [];
|
||
let page = 1;
|
||
let hasBeforeLatestTime = false;
|
||
while (!hasBeforeLatestTime) {
|
||
const pipelines = await service.gitlab.fetchPipelines(
|
||
project.project_id,
|
||
page++
|
||
);
|
||
// 如果当前页没有数据,则直接跳出
|
||
if (pipelines.length === 0) break;
|
||
pipelines.forEach((pipeline) => {
|
||
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.fetchPipelineDetails(project_id, id, created_at)
|
||
)
|
||
);
|
||
return fullPipelineDetailList.filter(
|
||
(v) => v
|
||
) as GitlabPipelineDetailWithCreateAt[];
|
||
};
|
||
|
||
const insertFullPipelineList = async (
|
||
fullPipelineList: GitlabPipelineDetailWithCreateAt[][],
|
||
fullUserMap: Record<string, string>,
|
||
fullProjectMap: Record<string, string>
|
||
) => {
|
||
const dbPipelineList: Partial<PipelineRecordModel> = [];
|
||
|
||
fullPipelineList.forEach((pipelineList) => {
|
||
pipelineList.forEach((pipeline) => {
|
||
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<PipelineRecordModel>) =>
|
||
db.pipeline.create(v)
|
||
)
|
||
);
|
||
};
|
||
|
||
const managePipeline = {
|
||
getFullPipelineList,
|
||
insertFullPipelineList,
|
||
};
|
||
|
||
export default managePipeline;
|