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"; +}