zhaoyingbo 1cef20c2c1
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 28s
CI Monitor CI/CD / deploy (push) Successful in 28s
feat: 添加获取项目徽章的功能 & 抽象gitlab请求
2024-07-01 12:49:29 +00:00

78 lines
1.8 KiB
TypeScript

import netTool from "./netTool";
const AUTH_HEADER = { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" };
const BASE_URL = "https://git.n.xiaomi.com/api/v4";
const gitlabGet = async <T = any>(url: string, params: any, defaultValue: any): Promise<T> => {
try {
const response = (await netTool.get(
url,
params,
AUTH_HEADER
)) as T & GitlabError;
if (response.message === "404 Project Not Found") return defaultValue;
return response;
} catch {
return defaultValue;
}
}
/**
* 获取项目详情
* @param id
* @returns
*/
const fetchProjectDetails = async (id: number) => {
const URL = `${BASE_URL}/projects/${id}`;
return gitlabGet<GitlabProjDetail | null>(URL, {}, null);
};
/**
* 获取流水线列表
* @param project_id
* @param page
* @returns
*/
const fetchPipelines = async (project_id: number, page = 1) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines`;
const params = { scope: "finished", per_page: 100, page };
return gitlabGet<GitlabPipeline[]>(URL, params, []);
};
/**
* 获取流水线详情
* @param project_id
* @param pipeline_id
* @param created_at
* @returns
*/
const fetchPipelineDetails = async (
project_id: number,
pipeline_id: number,
created_at: string
) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`;
const res = gitlabGet<GitlabPipelineDetail | null>(URL, {}, null);
if (res === null) return null;
return { ...res, created_at };
};
/**
* 获取项目的所有徽章
* @param project_id
*/
const fetchProjectBadges = async (project_id: number) => {
const URL = `${BASE_URL}/projects/${project_id}/badges`;
return gitlabGet<GitlabBadge[]>(URL, {}, []);
}
const gitlab = {
fetchPipelines,
fetchProjectBadges,
fetchProjectDetails,
fetchPipelineDetails,
};
export default gitlab;