feat: 更新获取项目徽章的功能
This commit is contained in:
parent
1cef20c2c1
commit
2154815761
@ -4,28 +4,31 @@ 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 gitlabGet = async <T = any>(url: string, params: any, defaultValue: any): Promise<T> => {
|
const gitlabReq = async <T = any>(
|
||||||
|
url: string,
|
||||||
|
params: any,
|
||||||
|
defaultValue: any,
|
||||||
|
reqFunc: any = netTool.get
|
||||||
|
): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const response = (await netTool.get(
|
const response = (await reqFunc(url, params, AUTH_HEADER)) as T &
|
||||||
url,
|
GitlabError;
|
||||||
params,
|
|
||||||
AUTH_HEADER
|
|
||||||
)) as T & GitlabError;
|
|
||||||
if (response.message === "404 Project Not Found") return defaultValue;
|
if (response.message === "404 Project Not Found") return defaultValue;
|
||||||
return response;
|
return response;
|
||||||
} catch {
|
} catch {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取项目详情
|
* 获取项目详情
|
||||||
* @param id
|
* @param id
|
||||||
* @returns
|
* @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}`;
|
const URL = `${BASE_URL}/projects/${id}`;
|
||||||
return gitlabGet<GitlabProjDetail | null>(URL, {}, null);
|
return gitlabReq<GitlabProjDetail | null>(URL, {}, null);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,7 +40,7 @@ const fetchProjectDetails = async (id: number) => {
|
|||||||
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 gitlabGet<GitlabPipeline[]>(URL, params, []);
|
return gitlabReq<GitlabPipeline[]>(URL, params, []);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,7 +56,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 = gitlabGet<GitlabPipelineDetail | null>(URL, {}, null);
|
const res = gitlabReq<GitlabPipelineDetail | null>(URL, {}, null);
|
||||||
if (res === null) return null;
|
if (res === null) return null;
|
||||||
return { ...res, created_at };
|
return { ...res, created_at };
|
||||||
};
|
};
|
||||||
@ -64,11 +67,27 @@ 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 gitlabGet<GitlabBadge[]>(URL, {}, []);
|
return gitlabReq<GitlabBadge[]>(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<GitlabBadge>(URL, new_badge, new_badge, netTool.put);
|
||||||
|
};
|
||||||
|
|
||||||
const gitlab = {
|
const gitlab = {
|
||||||
fetchPipelines,
|
fetchPipelines,
|
||||||
|
setProjectBadge,
|
||||||
fetchProjectBadges,
|
fetchProjectBadges,
|
||||||
fetchProjectDetails,
|
fetchProjectDetails,
|
||||||
fetchPipelineDetails,
|
fetchPipelineDetails,
|
||||||
|
@ -89,6 +89,9 @@ netTool.post = <T = any>(
|
|||||||
headers?: any
|
headers?: any
|
||||||
): Promise<T> => netTool({ url, method: "post", data, params, headers });
|
): Promise<T> => netTool({ url, method: "post", data, params, headers });
|
||||||
|
|
||||||
|
netTool.put = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
|
||||||
|
netTool({ url, method: "put", data, headers });
|
||||||
|
|
||||||
netTool.del = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
|
netTool.del = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
|
||||||
netTool({ url, method: "delete", data, headers });
|
netTool({ url, method: "delete", data, headers });
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user