feat: 优化GitLab徽章设置功能
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 3m0s
CI Monitor CI/CD / deploy (push) Successful in 1m4s

This commit is contained in:
zhaoyingbo 2024-07-04 02:41:11 +00:00
parent 9647f806ce
commit 524320c0e8
2 changed files with 17 additions and 19 deletions

View File

@ -14,8 +14,8 @@ const projectList = [
"cloud-ml/cloudml_syncqueue",
"cloud-ml/resource-instance",
"cloud-ml/ai-workbench-common",
"cloud-ml/evaluate.git",
"ai-service-data/ai-train-platform-service.git",
"cloud-ml/evaluate",
"ai-service-data/ai-train-platform-service",
"cloud-ml/embedding_management",
"cloud-ml/knowledge-base",
"cloud-ml/ai-proxy",
@ -44,10 +44,13 @@ const getNewProjectBadge = async (
];
const diff = [...badgeNameList].filter((x) => !badgeNameSet.has(x));
const newBadges: GitlabBadgeSetParams[] = diff.map((name) => {
const link_url = `https://sonarqube.mioffice.cn/dashboard?id=${sonarqubeId}`;
const link_url = encodeURI(
`https://sonarqube.mioffice.cn/dashboard?id=${sonarqubeId}`
);
const metric = name.replace("sonarqube_", "");
const image_url =
name !== "sonarqube_quality_gate"
? `https://sonarqube.mioffice.cn/api/badges/measure?key=${sonarqubeId}&metric=${name}`
? `https://sonarqube.mioffice.cn/api/badges/measure?key=${sonarqubeId}&metric=${metric}`
: `https://sonarqube.mioffice.cn/api/badges/gate?key=${sonarqubeId}`;
return {
id: projectDetail.id,

View File

@ -4,21 +4,16 @@ 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,
default_value: any,
reqFunc: any = netTool.get
): Promise<T> => {
const gitlabReqWarp = async <T = any>(func: Function, default_value: any) => {
try {
const response = (await reqFunc(url, params, AUTH_HEADER)) as T &
GitlabError;
let response = {} as T & GitlabError;
response = (await func()) as T & GitlabError;
if (response.message === "404 Project Not Found") return default_value;
return response;
} catch {
return default_value;
}
};
}
/**
*
@ -28,7 +23,7 @@ const gitlabReq = async <T = any>(
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);
return gitlabReqWarp<GitlabProjDetail>(() => netTool.get(URL, {}, AUTH_HEADER), null);
};
/**
@ -40,7 +35,7 @@ const fetchProjectDetails = async (id: number | string) => {
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, []);
return gitlabReqWarp<GitlabPipeline[]>(() => netTool.get(URL, params, AUTH_HEADER), []);
};
/**
@ -56,7 +51,7 @@ const fetchPipelineDetails = async (
created_at: string
) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`;
const res = gitlabReq<GitlabPipelineDetail | null>(URL, {}, null);
const res = gitlabReqWarp<GitlabPipelineDetail>(() => netTool.get(URL, {}, AUTH_HEADER), null);
if (res === null) return null;
return { ...res, created_at };
};
@ -67,7 +62,7 @@ const fetchPipelineDetails = async (
*/
const fetchProjectBadges = async (project_id: number) => {
const URL = `${BASE_URL}/projects/${project_id}/badges`;
return gitlabReq<GitlabBadge[]>(URL, {}, []);
return gitlabReqWarp<GitlabBadge[]>(() => netTool.get(URL, {}, AUTH_HEADER), []);
};
/**
@ -76,7 +71,7 @@ const fetchProjectBadges = async (project_id: number) => {
*/
const setProjectBadge = async (badge: GitlabBadgeSetParams) => {
const URL = `${BASE_URL}/projects/${badge.id}/badges/${badge.badge_id}`;
return gitlabReq<GitlabBadge>(URL, badge, null, netTool.put);
return gitlabReqWarp<GitlabBadge>(() => netTool.put(URL, badge, AUTH_HEADER), null);
};
/**
@ -85,7 +80,7 @@ const setProjectBadge = async (badge: GitlabBadgeSetParams) => {
*/
const addProjectBadge = async (badge: GitlabBadgeSetParams) => {
const URL = `${BASE_URL}/projects/${badge.id}/badges`;
return gitlabReq<GitlabBadge>(URL, badge, null, netTool.post);
return gitlabReqWarp<GitlabBadge>(() => netTool.post(URL, badge, {}, AUTH_HEADER), null);
};
const gitlab = {