zhaoyingbo 2154815761
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 28s
CI Monitor CI/CD / deploy (push) Successful in 28s
feat: 更新获取项目徽章的功能
2024-07-01 13:09:17 +00:00

97 lines
2.2 KiB
TypeScript

import netTool from "./netTool";
const AUTH_HEADER = { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" };
const BASE_URL = "https://git.n.xiaomi.com/api/v4";
const gitlabReq = async <T = any>(
url: string,
params: any,
defaultValue: any,
reqFunc: any = netTool.get
): Promise<T> => {
try {
const response = (await reqFunc(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 | string) => {
if (typeof id === "string") id = encodeURIComponent(id);
const URL = `${BASE_URL}/projects/${id}`;
return gitlabReq<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 gitlabReq<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 = gitlabReq<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 gitlabReq<GitlabBadge[]>(URL, {}, []);
};
/**
* 设置徽章
* @param project_id
* @param badge_id
* @param new_badge
*/
const setProjectBadge = async (
project_id: number,
badge_id: number,
new_badge: GitlabBadge
) => {
const URL = `${BASE_URL}/projects/${project_id}/badges/${badge_id}`;
return gitlabReq<GitlabBadge>(URL, new_badge, new_badge, netTool.put);
};
const gitlab = {
fetchPipelines,
setProjectBadge,
fetchProjectBadges,
fetchProjectDetails,
fetchPipelineDetails,
};
export default gitlab;