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 });