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/cloudml_syncqueue",
"cloud-ml/resource-instance", "cloud-ml/resource-instance",
"cloud-ml/ai-workbench-common", "cloud-ml/ai-workbench-common",
"cloud-ml/evaluate.git", "cloud-ml/evaluate",
"ai-service-data/ai-train-platform-service.git", "ai-service-data/ai-train-platform-service",
"cloud-ml/embedding_management", "cloud-ml/embedding_management",
"cloud-ml/knowledge-base", "cloud-ml/knowledge-base",
"cloud-ml/ai-proxy", "cloud-ml/ai-proxy",
@ -44,10 +44,13 @@ const getNewProjectBadge = async (
]; ];
const diff = [...badgeNameList].filter((x) => !badgeNameSet.has(x)); const diff = [...badgeNameList].filter((x) => !badgeNameSet.has(x));
const newBadges: GitlabBadgeSetParams[] = diff.map((name) => { 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 = const image_url =
name !== "sonarqube_quality_gate" 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}`; : `https://sonarqube.mioffice.cn/api/badges/gate?key=${sonarqubeId}`;
return { return {
id: projectDetail.id, 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 BASE_URL = "https://git.n.xiaomi.com/api/v4";
const gitlabReq = async <T = any>( const gitlabReqWarp = async <T = any>(func: Function, default_value: any) => {
url: string,
params: any,
default_value: any,
reqFunc: any = netTool.get
): Promise<T> => {
try { try {
const response = (await reqFunc(url, params, AUTH_HEADER)) as T & let response = {} as T & GitlabError;
GitlabError; response = (await func()) as T & GitlabError;
if (response.message === "404 Project Not Found") return default_value; if (response.message === "404 Project Not Found") return default_value;
return response; return response;
} catch { } catch {
return default_value; return default_value;
} }
}; }
/** /**
* *
@ -28,7 +23,7 @@ const gitlabReq = async <T = any>(
const fetchProjectDetails = async (id: number | string) => { const fetchProjectDetails = async (id: number | string) => {
if (typeof id === "string") id = encodeURIComponent(id); if (typeof id === "string") id = encodeURIComponent(id);
const URL = `${BASE_URL}/projects/${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 fetchPipelines = async (project_id: number, page = 1) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines`; const URL = `${BASE_URL}/projects/${project_id}/pipelines`;
const params = { scope: "finished", per_page: 100, page }; 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 created_at: string
) => { ) => {
const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`; 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; if (res === null) return null;
return { ...res, created_at }; return { ...res, created_at };
}; };
@ -67,7 +62,7 @@ const fetchPipelineDetails = async (
*/ */
const fetchProjectBadges = async (project_id: number) => { const fetchProjectBadges = async (project_id: number) => {
const URL = `${BASE_URL}/projects/${project_id}/badges`; 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 setProjectBadge = async (badge: GitlabBadgeSetParams) => {
const URL = `${BASE_URL}/projects/${badge.id}/badges/${badge.badge_id}`; 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 addProjectBadge = async (badge: GitlabBadgeSetParams) => {
const URL = `${BASE_URL}/projects/${badge.id}/badges`; 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 = { const gitlab = {