zhaoyingbo 7d90bd368e
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 23s
CI Monitor CI/CD / deploy (push) Successful in 30s
feat: 添加通过ChatID发送CI报告的功能
2024-05-21 06:54:31 +00:00

108 lines
2.4 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&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 sendMessage = async (body: string) => {
try {
const response = await fetch("https://egg.imoaix.cn/message", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body,
});
const res = await response.json();
return res;
} catch {
return null;
}
};
sendMessage.byGroupId = async (group_id: string, content: string) => {
const body = JSON.stringify({
group_id,
msg_type: "interactive",
content,
});
return sendMessage(body);
};
sendMessage.byChatId = async (chat_id: string, content: string) => {
const body = JSON.stringify({
receive_id: chat_id,
receive_id_type: "chat_id",
msg_type: "interactive",
content,
});
return sendMessage(body);
};
const service = {
fetchProjectDetails,
fetchPipelines,
fetchPipelineDetails,
sendMessage,
};
export default service;