From 21548157613d36b27a3271f3af7586ab494d24cc Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Mon, 1 Jul 2024 13:09:17 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/gitlab.ts | 45 ++++++++++++++++++++++++++++++++------------- service/netTool.ts | 3 +++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/service/gitlab.ts b/service/gitlab.ts index 87ee493..6e24746 100644 --- a/service/gitlab.ts +++ b/service/gitlab.ts @@ -4,28 +4,31 @@ 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 => { +const gitlabReq = async ( + url: string, + params: any, + defaultValue: any, + reqFunc: any = netTool.get +): Promise => { try { - const response = (await netTool.get( - url, - params, - AUTH_HEADER - )) as T & GitlabError; + const response = (await reqFunc(url, params, AUTH_HEADER)) as T & + GitlabError; if (response.message === "404 Project Not Found") return defaultValue; return response; } catch { return defaultValue; } -} +}; /** * 获取项目详情 * @param id * @returns */ -const fetchProjectDetails = async (id: number) => { +const fetchProjectDetails = async (id: number | string) => { + if (typeof id === "string") id = encodeURIComponent(id); const URL = `${BASE_URL}/projects/${id}`; - return gitlabGet(URL, {}, null); + return gitlabReq(URL, {}, null); }; /** @@ -37,7 +40,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 }; - return gitlabGet(URL, params, []); + return gitlabReq(URL, params, []); }; /** @@ -53,7 +56,7 @@ const fetchPipelineDetails = async ( created_at: string ) => { const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`; - const res = gitlabGet(URL, {}, null); + const res = gitlabReq(URL, {}, null); if (res === null) return null; return { ...res, created_at }; }; @@ -64,11 +67,27 @@ const fetchPipelineDetails = async ( */ const fetchProjectBadges = async (project_id: number) => { const URL = `${BASE_URL}/projects/${project_id}/badges`; - return gitlabGet(URL, {}, []); -} + return gitlabReq(URL, {}, []); +}; + +/** + * 设置徽章 + * @param project_id + * @param badge_id + * @param new_badge + */ +const setProjectBadge = async ( + project_id: number, + badge_id: number, + new_badge: GitlabBadge +) => { + const URL = `${BASE_URL}/projects/${project_id}/badges/${badge_id}`; + return gitlabReq(URL, new_badge, new_badge, netTool.put); +}; const gitlab = { fetchPipelines, + setProjectBadge, fetchProjectBadges, fetchProjectDetails, fetchPipelineDetails, diff --git a/service/netTool.ts b/service/netTool.ts index 256cbf3..8c494af 100644 --- a/service/netTool.ts +++ b/service/netTool.ts @@ -89,6 +89,9 @@ netTool.post = ( headers?: any ): Promise => netTool({ url, method: "post", data, params, headers }); +netTool.put = (url: string, data: any, headers?: any): Promise => + netTool({ url, method: "put", data, headers }); + netTool.del = (url: string, data: any, headers?: any): Promise => netTool({ url, method: "delete", data, headers });