117 lines
2.6 KiB
TypeScript
117 lines
2.6 KiB
TypeScript
import netTool from "./netTool";
|
|
|
|
const AUTH_HEADER = { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" };
|
|
|
|
const BASE_URL = "https://git.n.xiaomi.com/api/v4";
|
|
|
|
const gitlabReqWarp = async <T = any>(
|
|
func: Function,
|
|
default_value: any
|
|
): Promise<T> => {
|
|
try {
|
|
let response = {} as T & GitlabError;
|
|
response = (await func()) as T & GitlabError;
|
|
if (response.message === "404 Project Not Found") return default_value;
|
|
return response;
|
|
} catch {
|
|
return default_value;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 获取项目详情
|
|
* @param id
|
|
* @returns
|
|
*/
|
|
const fetchProjectDetails = async (id: number | string) => {
|
|
if (typeof id === "string") id = encodeURIComponent(id);
|
|
const URL = `${BASE_URL}/projects/${id}`;
|
|
return gitlabReqWarp<GitlabProjDetail>(
|
|
() => netTool.get(URL, {}, AUTH_HEADER),
|
|
null
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 获取流水线列表
|
|
* @param project_id
|
|
* @param page
|
|
* @returns
|
|
*/
|
|
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 gitlabReqWarp<GitlabPipeline[]>(
|
|
() => netTool.get(URL, params, AUTH_HEADER),
|
|
[]
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 获取流水线详情
|
|
* @param project_id
|
|
* @param pipeline_id
|
|
* @param created_at
|
|
* @returns
|
|
*/
|
|
const fetchPipelineDetails = async (
|
|
project_id: number,
|
|
pipeline_id: number,
|
|
created_at: string
|
|
) => {
|
|
const URL = `${BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}`;
|
|
const res = await gitlabReqWarp<GitlabPipelineDetail>(
|
|
() => netTool.get(URL, {}, AUTH_HEADER),
|
|
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 gitlabReqWarp<GitlabBadge[]>(
|
|
() => netTool.get(URL, {}, AUTH_HEADER),
|
|
[]
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 设置徽章
|
|
* @param badge
|
|
*/
|
|
const setProjectBadge = async (badge: GitlabBadgeSetParams) => {
|
|
const URL = `${BASE_URL}/projects/${badge.id}/badges/${badge.badge_id}`;
|
|
return gitlabReqWarp<GitlabBadge>(
|
|
() => netTool.put(URL, badge, AUTH_HEADER),
|
|
null
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 添加徽章
|
|
* @param badge
|
|
*/
|
|
const addProjectBadge = async (badge: GitlabBadgeSetParams) => {
|
|
const URL = `${BASE_URL}/projects/${badge.id}/badges`;
|
|
return gitlabReqWarp<GitlabBadge>(
|
|
() => netTool.post(URL, badge, {}, AUTH_HEADER),
|
|
null
|
|
);
|
|
};
|
|
|
|
const gitlab = {
|
|
fetchPipelines,
|
|
setProjectBadge,
|
|
addProjectBadge,
|
|
fetchProjectBadges,
|
|
fetchProjectDetails,
|
|
fetchPipelineDetails,
|
|
};
|
|
|
|
export default gitlab;
|