From 1cef20c2c1bfe28d84379f5e0c46b9cfb23b7390 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Mon, 1 Jul 2024 12:49:29 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=BE=BD=E7=AB=A0=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=20&=20=E6=8A=BD=E8=B1=A1gitlab=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/gitlab.ts | 64 +++++++++++++++++++++----------------------- service/typings.d.ts | 10 +++++++ 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/service/gitlab.ts b/service/gitlab.ts index 0c08095..87ee493 100644 --- a/service/gitlab.ts +++ b/service/gitlab.ts @@ -4,6 +4,20 @@ const AUTH_HEADER = { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" }; const BASE_URL = "https://git.n.xiaomi.com/api/v4"; +const gitlabGet = async (url: string, params: any, defaultValue: any): Promise => { + 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(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(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(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(URL, {}, []); +} + const gitlab = { - fetchProjectDetails, fetchPipelines, + fetchProjectBadges, + fetchProjectDetails, fetchPipelineDetails, }; diff --git a/service/typings.d.ts b/service/typings.d.ts index 10f524b..91eb74d 100644 --- a/service/typings.d.ts +++ b/service/typings.d.ts @@ -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"; +}