feat: 添加获取项目徽章的功能 & 抽象gitlab请求
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 28s
CI Monitor CI/CD / deploy (push) Successful in 28s

This commit is contained in:
zhaoyingbo 2024-07-01 12:49:29 +00:00
parent fa0777b6ae
commit 1cef20c2c1
2 changed files with 40 additions and 34 deletions

View File

@ -4,6 +4,20 @@ 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
@ -11,17 +25,7 @@ const BASE_URL = "https://git.n.xiaomi.com/api/v4";
*/
const fetchProjectDetails = async (id: number) => {
const URL = `${BASE_URL}/projects/${id}`;
try {
const response = (await netTool.get(
URL,
{},
AUTH_HEADER
)) as GitlabProjDetail & GitlabError;
if (response.message === "404 Project Not Found") return null;
return response;
} catch {
return null;
}
return gitlabGet<GitlabProjDetail | null>(URL, {}, null);
};
/**
@ -33,17 +37,7 @@ const fetchProjectDetails = async (id: number) => {
const fetchPipelines = async (project_id: number, page = 1) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines`;
const params = { scope: "finished", per_page: 100, page };
try {
const response = (await netTool.get(
URL,
params,
AUTH_HEADER
)) as GitlabPipeline[] & GitlabError;
if (response?.message === "404 Project Not Found") return [];
return response;
} catch {
return [] as GitlabPipeline[];
}
return gitlabGet<GitlabPipeline[]>(URL, params, []);
};
/**
@ -59,22 +53,24 @@ const fetchPipelineDetails = async (
created_at: string
) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`;
try {
const response = (await netTool.get(
URL,
{},
AUTH_HEADER
)) as GitlabPipelineDetail & GitlabError;
if (response.message === "404 Project Not Found") return null;
return { ...response, created_at };
} catch {
return null;
}
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 = {
fetchProjectDetails,
fetchPipelines,
fetchProjectBadges,
fetchProjectDetails,
fetchPipelineDetails,
};

10
service/typings.d.ts vendored
View File

@ -50,3 +50,13 @@ interface GitlabPipelineDetail {
interface GitlabPipelineDetailWithCreateAt extends GitlabPipelineDetail {
created_at: string;
}
interface GitlabBadge {
id: number;
name: string;
link_url: string;
image_url: string;
rendered_link_url: string;
rendered_image_url: string;
kind: "project" | "group";
}