zhaoyingbo ea389da7bb
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 30s
CI Monitor CI/CD / deploy (push) Successful in 28s
feat: 添加code-spell-checker插件
2024-07-01 12:16:39 +00:00

80 lines
2.5 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 { 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.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.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;