72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
const fetchGetParams = {
|
|
method: "GET",
|
|
headers: { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" },
|
|
};
|
|
|
|
/**
|
|
* 获取项目详情
|
|
* @param id 项目id
|
|
*/
|
|
const fetchProjectDetails = async (id: number) => {
|
|
try {
|
|
const response = await fetch(
|
|
`https://git.n.xiaomi.com/api/v4/projects/${id}`,
|
|
fetchGetParams
|
|
);
|
|
const body = (await response.json()) as GitlabProjDetail;
|
|
if (body.message === "404 Project Not Found") return null;
|
|
return body;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 获取流水线列表
|
|
*/
|
|
const fetchPipelines = async (project_id: number, page = 1) => {
|
|
try {
|
|
const response = await fetch(
|
|
`https://git.n.xiaomi.com/api/v4/projects/${project_id}/pipelines?scope=finished&status=success&per_page=100&page=${page}`,
|
|
fetchGetParams
|
|
);
|
|
const body = (await response.json()) as GitlabPipeline[] & GitlabError;
|
|
if (body?.message === "404 Project Not Found") return [];
|
|
return body;
|
|
} catch {
|
|
return [] as GitlabPipeline[];
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 获取流水线详情
|
|
*/
|
|
const fetchPipelineDetails = async (
|
|
project_id: number,
|
|
pipeline_id: number,
|
|
created_at: string
|
|
) => {
|
|
try {
|
|
const response = await fetch(
|
|
`https://git.n.xiaomi.com/api/v4/projects/${project_id}/pipelines/${pipeline_id}`,
|
|
fetchGetParams
|
|
);
|
|
const body = (await response.json()) as GitlabPipelineDetail;
|
|
if (body.message === "404 Project Not Found") return null;
|
|
return {
|
|
...body,
|
|
created_at,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const service = {
|
|
fetchProjectDetails,
|
|
fetchPipelines,
|
|
fetchPipelineDetails,
|
|
};
|
|
|
|
export default service;
|