diff --git a/.vscode/settings.json b/.vscode/settings.json index eef5140..113f533 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,10 +11,12 @@ "devcontainers", "eamodio", "esbenp", + "gitbeaker", "Gruntfuggly", "micr", "mioffice", "oxlint", + "sonarqube", "tseslint", "wlpbbgiky", "wujiali", diff --git a/bun.lockb b/bun.lockb index 2ea389c..b9e1f44 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/controllers/managePipeLine/index.ts b/controllers/managePipeLine/index.ts index 6b701bd..cfe831d 100644 --- a/controllers/managePipeLine/index.ts +++ b/controllers/managePipeLine/index.ts @@ -1,14 +1,15 @@ +import { ExpandedPipelineSchema, PipelineSchema } from "@gitbeaker/rest" import moment from "moment" +import pLimit from "p-limit" import db from "../../db" import service from "../../service" import { DB } from "../../types/db" -import { Gitlab } from "../../types/gitlab" /** * 获取全部的pipeline列表 * @param {DB.Project} project - 项目对象 - * @returns {Promise<(Gitlab.PipelineDetail & { created_at: string })[]>} - 返回包含详细信息的pipeline列表 + * @returns {Promise<(ExpandedPipelineSchema & { created_at: string })[]>} - 返回包含详细信息的pipeline列表 */ const getFullPipelineList = async (project: DB.Project) => { // 先获取最新的pipelineID @@ -18,14 +19,15 @@ const getFullPipelineList = async (project: DB.Project) => { latestOne?.created_at || "2024-01-01T00:00:00.000+08:00" ) // 获取pipeline列表并保存 - const fullPipelineList: Gitlab.Pipeline[] = [] + const fullPipelineList: PipelineSchema[] = [] let page = 1 let hasBeforeLatestTime = false while (!hasBeforeLatestTime) { - const pipelines = await service.gitlab.pipeline.getList( - project.project_id, - page++ - ) + const pipelines = await service.gitlab.Pipelines.all(project.project_id, { + page: page++, + perPage: 100, + scope: "finished", + }).catch(() => []) // 如果当前页没有数据,则直接跳出 if (pipelines.length === 0) break pipelines.forEach((pipeline) => { @@ -38,25 +40,30 @@ const getFullPipelineList = async (project: DB.Project) => { } }) } + const limit = pLimit(10) const fullPipelineDetailList = await Promise.all( fullPipelineList.map(({ project_id, id, created_at }) => - service.gitlab.pipeline.getDetail(project_id, id, created_at) + limit(() => + service.gitlab.Pipelines.show(project_id, id) + .catch(() => null) + .then((res) => (res ? { ...res, created_at } : null)) + ) ) ) - return fullPipelineDetailList.filter((v) => v) as (Gitlab.PipelineDetail & { + return fullPipelineDetailList.filter((v) => v) as (ExpandedPipelineSchema & { created_at: string })[] } /** * 插入全部的pipeline列表到数据库 - * @param {(Gitlab.PipelineDetail & { created_at: string })[][]} fullPipelineList - 包含详细信息的pipeline列表 + * @param {(ExpandedPipelineSchema & { created_at: string })[][]} fullPipelineList - 包含详细信息的pipeline列表 * @param {Record} fullUserMap - 用户映射表 * @param {Record} fullProjectMap - 项目映射表 * @returns {Promise} */ const insertFullPipelineList = async ( - fullPipelineList: (Gitlab.PipelineDetail & { created_at: string })[][], + fullPipelineList: (ExpandedPipelineSchema & { created_at: string })[][], fullUserMap: Record, fullProjectMap: Record ) => { diff --git a/controllers/managePipelineEvent/index.ts b/controllers/managePipelineEvent/index.ts index 9d8e1f1..0aa1387 100644 --- a/controllers/managePipelineEvent/index.ts +++ b/controllers/managePipelineEvent/index.ts @@ -1,3 +1,5 @@ +import type { MergeRequestSchema } from "@gitbeaker/rest" + import db from "../../db" import service from "../../service" import netTool from "../../service/netTool" @@ -54,10 +56,9 @@ const getNextAction = async ( } // 在流水线为`running`时,且该stage全部的job有非结束状态时即`created`、`pending`、`running`、`manual`、`scheduled`时,添加监控 if (pipeline.object_attributes.status === "running") { - const jobs = await service.gitlab.pipeline.getJobs( - pipeline.project.id, - pipeline.object_attributes.id - ) + const jobs = await service.gitlab.Jobs.all(pipeline.project.id, { + pipelineId: pipeline.object_attributes.id, + }).catch(() => []) if ( jobs.some( (job) => @@ -75,16 +76,16 @@ const getNextAction = async ( /** * 获取合并请求 * @param {Gitlab.PipelineEvent} pipeline - GitLab 流水线事件对象 - * @returns {Promise} - 返回合并请求对象或 null + * @returns {Promise} - 返回合并请求对象或 null */ const getMergeRequest = async ( pipeline: Gitlab.PipelineEvent -): Promise => { +): Promise => { if (!checkIsMergeCommit(pipeline)) return null - const res = await service.gitlab.commit.getMr( + const res = await service.gitlab.Commits.allMergeRequests( pipeline.project.id, pipeline.object_attributes.sha - ) + ).catch(() => []) if (res.length === 0) return null return res.find((mr) => mr.merge_commit_sha === pipeline.commit.id) || null } @@ -92,12 +93,12 @@ const getMergeRequest = async ( /** * 获取用户信息 * @param {Gitlab.PipelineEvent} pipeline - GitLab 流水线事件对象 - * @param {Gitlab.MergeRequest | null} mergeRequest - 合并请求对象或 null + * @param {MergeRequestSchema | null} mergeRequest - 合并请求对象或 null * @returns {{ participant: string, receiver: string[] }} - 返回包含参与者和接收者信息的对象 */ const getUserInfo = ( pipeline: Gitlab.PipelineEvent, - mergeRequest: Gitlab.MergeRequest | null + mergeRequest: MergeRequestSchema | null ): { participant: string; receiver: string[] } => { let participant = pipeline.user.name const receiver = [pipeline.user.username] diff --git a/controllers/manageProject/index.ts b/controllers/manageProject/index.ts index 7ece550..e6e5784 100644 --- a/controllers/manageProject/index.ts +++ b/controllers/manageProject/index.ts @@ -8,13 +8,15 @@ import { DB } from "../../types/db" * @returns {Promise} - 返回填充后的项目对象 */ const fillProj = async (project: DB.Project): Promise => { - const projDetail = await service.gitlab.project.getDetail(project.project_id) + const projDetail = await service.gitlab.Projects.show( + project.project_id + ).catch(() => null) if (!projDetail) { return project } const useFulParams: Partial = { ...project, - avatar_url: projDetail.avatar_url, + avatar_url: projDetail.avatar_url || "", description: projDetail.description, name: projDetail.name, path_with_namespace: projDetail.path_with_namespace, diff --git a/controllers/manageUser/index.ts b/controllers/manageUser/index.ts index 3406ccc..57f70ce 100644 --- a/controllers/manageUser/index.ts +++ b/controllers/manageUser/index.ts @@ -1,13 +1,15 @@ +import { ExpandedPipelineSchema } from "@gitbeaker/rest" + import db from "../../db" import { Gitlab } from "../../types/gitlab" /** * 获取完整的用户映射表 - * @param {Gitlab.PipelineDetail[][]} fullPipelineList - 完整的pipeline列表 + * @param {ExpandedPipelineSchema[][]} fullPipelineList - 完整的pipeline列表 * @returns {Promise>} - 返回用户映射表 */ const getFullUserMap = async ( - fullPipelineList: Gitlab.PipelineDetail[][] + fullPipelineList: ExpandedPipelineSchema[][] ): Promise> => { const userList: Gitlab.User[] = [] fullPipelineList.forEach((fullPipeline) => { diff --git a/package.json b/package.json index b727c31..2f442d8 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "typescript": "^5.0.0" }, "dependencies": { + "@gitbeaker/rest": "^40.1.2", "lodash": "^4.17.21", "moment": "^2.30.1", "node-schedule": "^2.1.1", diff --git a/schedule/monitorJob.ts b/schedule/monitorJob.ts index 880dfef..e749c4e 100644 --- a/schedule/monitorJob.ts +++ b/schedule/monitorJob.ts @@ -15,10 +15,9 @@ const doMonitor = async (monitor: DB.Monitor): Promise => { const { project_id, pipeline_id, api_key, stage, receiver, variable } = monitor // 获取Job列表 - const jobList = await service.gitlab.pipeline.getJobs( - Number(project_id), - Number(pipeline_id) - ) + const jobList = await service.gitlab.Jobs.all(project_id, { + pipelineId: Number(pipeline_id), + }).catch(() => []) // 是否所有Stage关联的Job都成功了 const isAllSuccess = jobList.every( (job) => job.stage === stage && job.status === "success" @@ -28,10 +27,10 @@ const doMonitor = async (monitor: DB.Monitor): Promise => { // 先删除监控 await db.monitor.del(monitor.id) // 获取最新的执行时长 - const pipelineDetail = await service.gitlab.pipeline.getDetail( - Number(project_id), + const pipelineDetail = await service.gitlab.Pipelines.show( + project_id, Number(pipeline_id) - ) + ).catch(() => null) if (pipelineDetail) { variable["duration"] = sec2minStr(pipelineDetail.duration) } diff --git a/script/badge/cloudml.ts b/script/badge/cloudml.ts index 78ea223..6a59374 100644 --- a/script/badge/cloudml.ts +++ b/script/badge/cloudml.ts @@ -1,6 +1,7 @@ +import { ProjectSchema } from "@gitbeaker/rest" +import pLimit from "p-limit" + import service from "../../service" -import { BadgeSetParams } from "../../service/gitlab/badge" -import { Gitlab } from "../../types/gitlab" const projectList = [ // "cloud-ml/cloudml-maas", @@ -24,16 +25,23 @@ const projectList = [ "cloud-ml/lark_auth", ] +interface BadgeSetParams { + groupId: number + linkUrl: string + imageUrl: string + name: string +} + const getNewProjectBadge = async ( - projectDetail: Gitlab.ProjDetail + projectDetail: ProjectSchema ): Promise => { // 项目路径 cloud-ml/cloudml-dev const projectPath = projectDetail.path_with_namespace // 根据项目路径获取sonarqubeId 类似于 cloud-ml/cloudml-dev -> cloud-ml:cloudml-dev const sonarqubeId = projectPath.replace("/", ":") // 获取项目的badges - const badges: Gitlab.Badge[] = await service.gitlab.badge.get( - projectDetail.id + const badges = await service.gitlab.ProjectBadges.all(projectDetail.id).catch( + () => [] ) // 对badges进行补全,可能只有 name: "sonarqube_coverage" 的情况,把剩下的补全 const badgeNames = badges.map((badge) => badge.name) @@ -47,20 +55,18 @@ const getNewProjectBadge = async ( ] const diff = [...badgeNameList].filter((x) => !badgeNameSet.has(x)) const newBadges: BadgeSetParams[] = diff.map((name) => { - const link_url = encodeURI( + const linkUrl = encodeURI( `https://sonarqube.mioffice.cn/dashboard?id=${sonarqubeId}` ) const metric = name.replace("sonarqube_", "") - const image_url = + const imageUrl = name !== "sonarqube_quality_gate" ? `https://sonarqube.mioffice.cn/api/badges/measure?key=${sonarqubeId}&metric=${metric}` : `https://sonarqube.mioffice.cn/api/badges/gate?key=${sonarqubeId}` return { - id: projectDetail.id, - link_url, - image_url, - rendered_image_url: image_url, - rendered_link_url: link_url, + groupId: projectDetail.id, + linkUrl, + imageUrl, name, } }) @@ -68,21 +74,25 @@ const getNewProjectBadge = async ( } const addNewProjectBadge = async (badgeSetParamsList: BadgeSetParams[]) => { - const chunkSize = 5 // 每次并发请求的数量 - for (let i = 0; i < badgeSetParamsList.length; i += chunkSize) { - const chunk = badgeSetParamsList.slice(i, i + chunkSize) - const res = await Promise.all( - chunk.map((badgeSetParams) => service.gitlab.badge.add(badgeSetParams)) + const limit = pLimit(5) + await Promise.all( + badgeSetParamsList.map((b) => + limit(() => + service.gitlab.ProjectBadges.add(b.groupId, b.linkUrl, b.imageUrl, { + name: b.name, + }).catch(() => null) + ) ) - console.log(res) - } + ) } const main = async () => { const errorList: string[] = [] const badgeAddParamsList = await Promise.all( projectList.map(async (projectName) => { - const projectDetail = await service.gitlab.project.getDetail(projectName) + const projectDetail = await service.gitlab.Projects.show( + projectName + ).catch(() => null) if (!projectDetail) { errorList.push(projectName) return [] diff --git a/script/badge/miai.ts b/script/badge/miai.ts deleted file mode 100644 index 1098bc8..0000000 --- a/script/badge/miai.ts +++ /dev/null @@ -1,113 +0,0 @@ -import service from "../../service" -import { BadgeSetParams } from "../../service/gitlab/badge" -import { Gitlab } from "../../types/gitlab" - -const projectList = [ - "miai-fe/fe/ai-admin-fe", - "miai-fe/fe/ai-operation-fe", - "miai-fe/fe/ai-dripback-fe", - "miai-fe/fe/ai-open-admin-fe", - "miai-fe/fe/ai-model-deployment-fe", - "miai-fe/fe/ai-workbench", - "miai-fe/fe/ai-review-fe", - "miai-fe/fe/cms", - "miai-fe/fe/xiaoai-open-Platform", - "miai-fe/fe/ai-robot-fe", - "miai-fe/fe/ai-scene-review-fe", - "miai-fe/fe/houyi", - "miai-fe/fe/kms", - "miai-fe/fe/ai-productive-fe", - "miai-fe/fe/ai-sliding-label-fe", - "miai-fe/fe/ai-label-control-fe", - "miai-fe/fe/asr-fe", - "miai-fe/fe/feedback-platform-fe", - "miai-fe/fe/statisfaction-evaluation-platform-fe", - "miai-fe/fe/ai-quality-score-fe", - "miai-fe/fe/wakeup", - "miai-fe/fe/ai-badcase-label-fe", - "miai-fe/fe/func-manager", - "miai-fe/service/ado-server", - "miai-fe/fe/ado-fe", - "miai-fe/pwa/ai-rn-domain", - "miai-fe/pwa/ai-ak-fe", - "miai-fe/fe/ai-user-info-fe", - "miai-fe/fe-infra/xiaoai-jssdk", - "miai-fe/fe-infra/ai-ui", - "miai-fe/fe/ai-class-sheducle-fe", - "miai-fe/service/ai-schedule-service", - "miai-fe/fe/ai-schedule-devtool-v3", - "miai-fe/ai-scenes-fe", - "miai-fe/activity/om-music-tone", - "miai-fe/activity/ug-guide-test", - "miai-fe/activity/om-mood-blindbox", - "miai-fe/activity/om-love-poem", - "miai-fe/fe/ai-introduce", - "miai-fe/fe/ai-phonecall-guider-fe", - "miai-fe/activity/om-spring-couplets", - "miai-fe/activity/ug-play-assistant", - "miai-fe/fe/ai-iot-introduce-fe", - "miai-fe/fe/ai-rookie-play-fe", - "miai-fe/fe/ai-custom-tts-fe", - "miai-fe/fe-infra/split-rpk-sdk", - "miai-fe/fe/ai-full-animation-template-fe", - "miai-fe/fe/ai-schedule-manager-fe", - "miai-fe/fe/ai-voiceprint-fe", - "miai-fe/fe/ai-shortcut-fe", -] - -const getProjectId = async (projectName: string) => { - const res = await service.gitlab.project.getDetail(projectName) - return res?.id -} - -const getNewProjectBadge = async ( - projectId: number -): Promise => { - const badges: Gitlab.Badge[] = await service.gitlab.badge.get(projectId) - - const replacePath = (value: string) => - value.replace( - "https://sonarqube.mioffice.cn", - "http://scan.sonarqube.xiaomi.srv" - ) - - return badges.map((badge: Gitlab.Badge) => ({ - id: projectId, - badge_id: badge.id, - link_url: replacePath(badge.link_url), - image_url: badge.image_url, - rendered_image_url: badge.rendered_image_url, - rendered_link_url: replacePath(badge.rendered_link_url), - })) -} - -const setNewProjectBadge = async (badgeSetParamsList: BadgeSetParams[]) => { - const chunkSize = 5 // 每次并发请求的数量 - for (let i = 0; i < badgeSetParamsList.length; i += chunkSize) { - const chunk = badgeSetParamsList.slice(i, i + chunkSize) - const res = await Promise.all( - chunk.map((badgeSetParams) => service.gitlab.badge.set(badgeSetParams)) - ) - console.log(res) - } -} - -const main = async () => { - const errorList: string[] = [] - const badgeSetParamsList = await Promise.all( - projectList.map(async (projectName) => { - const projectId = await getProjectId(projectName) - if (!projectId) { - errorList.push(projectName) - return [] - } - return await getNewProjectBadge(projectId) - }) - ) - - await setNewProjectBadge(badgeSetParamsList.flat()) - - console.log("errorList", errorList) -} - -main() diff --git a/service/gitlab/badge.ts b/service/gitlab/badge.ts deleted file mode 100644 index 07e7423..0000000 --- a/service/gitlab/badge.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { Gitlab } from "../../types/gitlab" -import netTool from "../netTool" -import { GITLAB_AUTH_HEADER, GITLAB_BASE_URL, gitlabReqWarp } from "./tools" - -/** - * 代表设置 GitLab 徽章的参数。 - */ -export type BadgeSetParams = Omit & { - badge_id?: number -} - -/** - * 为特定项目检索 GitLab 徽章。 - * @param {number} project_id - 项目的 ID。 - * @returns {Promise} 一个承诺,解析为 GitLab 徽章的数组。 - */ -const get = async (project_id: number): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${project_id}/badges` - return gitlabReqWarp( - () => netTool.get(URL, {}, GITLAB_AUTH_HEADER), - [] - ) -} - -/** - * 设置 GitLab 徽章。 - * @param {BadgeSetParams} badge - 徽章参数。 - * @returns {Promise} 一个承诺,解析为更新后的徽章。 - */ -const set = async (badge: BadgeSetParams): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${badge.id}/badges/${badge.badge_id}` - return gitlabReqWarp( - () => netTool.put(URL, badge, {}, GITLAB_AUTH_HEADER), - null - ) -} - -/** - * 添加 GitLab 徽章。 - * @param {BadgeSetParams} badge - 徽章参数。 - * @returns {Promise} 一个承诺,解析为添加的徽章。 - */ -const add = async (badge: BadgeSetParams): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${badge.id}/badges` - return gitlabReqWarp( - () => netTool.post(URL, badge, {}, GITLAB_AUTH_HEADER), - null - ) -} - -/** - * 代表一系列与徽章相关的函数。 - */ -const badge = { - get, - set, - add, -} - -export default badge diff --git a/service/gitlab/commit.ts b/service/gitlab/commit.ts deleted file mode 100644 index 4e14532..0000000 --- a/service/gitlab/commit.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Gitlab } from "../../types/gitlab" -import netTool from "../netTool" -import { GITLAB_AUTH_HEADER, GITLAB_BASE_URL, gitlabReqWarp } from "./tools" - -/** - * 检索与特定提交关联的合并请求。 - * @param {number} project_id - 项目的ID。 - * @param {string} sha - 提交的SHA。 - * @returns {Promise} 一个解析为合并请求数组的promise。 - */ -const getMr = async ( - project_id: number, - sha: string -): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${project_id}/repository/commits/${sha}/merge_requests` - return gitlabReqWarp( - () => netTool.get(URL, {}, GITLAB_AUTH_HEADER), - [] - ) -} - -const commit = { - getMr, -} - -export default commit diff --git a/service/gitlab/index.ts b/service/gitlab/index.ts index c247995..6239caa 100644 --- a/service/gitlab/index.ts +++ b/service/gitlab/index.ts @@ -1,13 +1,8 @@ -import badge from "./badge" -import commit from "./commit" -import pipeline from "./pipeline" -import project from "./project" +import { Gitlab } from "@gitbeaker/rest" -const gitlab = { - project, - badge, - commit, - pipeline, -} +const gitlab = new Gitlab({ + token: "Zd1UASPcMwVox5tNS6ep", + host: "https://git.n.xiaomi.com", +}) export default gitlab diff --git a/service/gitlab/pipeline.ts b/service/gitlab/pipeline.ts deleted file mode 100644 index 273d6ae..0000000 --- a/service/gitlab/pipeline.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { Gitlab } from "../../types/gitlab" -import netTool from "../netTool" -import { GITLAB_AUTH_HEADER, GITLAB_BASE_URL, gitlabReqWarp } from "./tools" - -/** - * 获取特定GitLab流水线的详细信息。 - * @param {number} project_id - 项目的ID。 - * @param {number} pipeline_id - 流水线的ID。 - * @param {string} created_at - 流水线的创建日期。 - */ -const getDetail = async ( - project_id: number, - pipeline_id: number, - created_at?: string -) => { - const URL = `${GITLAB_BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}` - const res = await gitlabReqWarp( - () => netTool.get(URL, {}, GITLAB_AUTH_HEADER), - null - ) - if (res === null) return null - return { ...res, created_at } -} - -/** - * 获取特定项目的GitLab流水线列表。 - * @param {number} project_id - 项目的ID。 - * @param {number} [page=1] - 结果的页码(默认值:1)。 - * @returns {Promise} 一个解析为流水线数组的promise。 - */ -const getList = async ( - project_id: number, - page = 1 -): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${project_id}/pipelines` - const params = { scope: "finished", per_page: 100, page } - return gitlabReqWarp( - () => netTool.get(URL, params, GITLAB_AUTH_HEADER), - [] - ) -} - -/** - * 获取特定GitLab流水线的任务列表。 - * @param {number} project_id - 项目的ID。 - * @param {number} pipeline_id - 流水线的ID。 - * @returns {Promise} 一个解析为任务数组的promise。 - */ -const getJobs = async ( - project_id: number, - pipeline_id: number -): Promise => { - const URL = `${GITLAB_BASE_URL}/projects/${project_id}/pipelines/${pipeline_id}/jobs` - return gitlabReqWarp( - () => netTool.get(URL, {}, GITLAB_AUTH_HEADER), - [] - ) -} - -const pipeline = { - getDetail, - getList, - getJobs, -} - -export default pipeline diff --git a/service/gitlab/project.ts b/service/gitlab/project.ts deleted file mode 100644 index 7e61529..0000000 --- a/service/gitlab/project.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Gitlab } from "../../types/gitlab" -import netTool from "../netTool" -import { GITLAB_AUTH_HEADER, GITLAB_BASE_URL, gitlabReqWarp } from "./tools" - -/** - * 获取 GitLab 项目的详细信息。 - * @param {number | string} project_id - 项目的 ID 或 URL-encoded 路径。 - * @returns {Promise} 一个解析为项目详细信息的 promise。 - */ -const getDetail = async ( - project_id: number | string -): Promise => { - if (typeof project_id === "string") - project_id = encodeURIComponent(project_id) - const URL = `${GITLAB_BASE_URL}/projects/${project_id}` - return gitlabReqWarp( - () => netTool.get(URL, {}, GITLAB_AUTH_HEADER), - null - ) -} - -const project = { - getDetail, -} - -export default project diff --git a/service/gitlab/tools.ts b/service/gitlab/tools.ts deleted file mode 100644 index 3424b95..0000000 --- a/service/gitlab/tools.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Gitlab } from "../../types/gitlab" - -/** - * 包装一个 GitLab 请求函数,处理错误并返回默认值。 - * @template T - * @param {() => Promise} func - 要执行的请求函数。 - * @param {any} default_value - 请求失败时返回的默认值。 - * @returns {Promise} 一个解析为请求结果或默认值的 promise。 - */ -export const gitlabReqWarp = async ( - func: () => Promise, - default_value: any -): Promise => { - try { - let response = {} as T & Gitlab.Error - response = (await func()) as T & Gitlab.Error - if (response.message === "404 Project Not Found") return default_value - return response - } catch { - return default_value - } -} - -/** - * GitLab API 的基础 URL。 - * @type {string} - */ -export const GITLAB_BASE_URL = "https://git.n.xiaomi.com/api/v4" - -/** - * GitLab API 的认证头。 - * @type {object} - */ -export const GITLAB_AUTH_HEADER = { "PRIVATE-TOKEN": "Zd1UASPcMwVox5tNS6ep" } diff --git a/service/index.ts b/service/index.ts index 41c86b2..83ca18d 100644 --- a/service/index.ts +++ b/service/index.ts @@ -2,8 +2,8 @@ import gitlab from "./gitlab" import message from "./message" const service = { - gitlab, message, + gitlab, } export default service diff --git a/types/gitlab.ts b/types/gitlab.ts index 6535915..1873d20 100644 --- a/types/gitlab.ts +++ b/types/gitlab.ts @@ -1,11 +1,4 @@ export namespace Gitlab { - /* 错误 */ - export interface Error { - /** - * 错误消息 - */ - message: string - } /* 用户 */ export interface User { /** @@ -34,164 +27,6 @@ export namespace Gitlab { web_url: string } - /* 项目详情 */ - export interface ProjDetail { - /** - * 项目ID - */ - id: number - /** - * 项目描述 - */ - description: string - /** - * 项目名称 - */ - name: string - /** - * 带命名空间的项目路径 - */ - path_with_namespace: string - /** - * 项目网页URL - */ - web_url: string - /** - * 项目头像URL(可选) - */ - avatar_url?: any - /** - * 消息(可选) - */ - message?: string - } - - /* 管道详情 */ - export interface PipelineDetail { - /** - * 管道ID - */ - id: number - /** - * 项目ID - */ - project_id: number - /** - * 分支名 - */ - ref: string - /** - * 状态 - */ - status: string - /** - * 管道网页URL - */ - web_url: string - /** - * 用户信息 - */ - user: User - /** - * 开始时间 - */ - started_at: string - /** - * 完成时间 - */ - finished_at: string - /** - * 持续时间 - */ - duration: number - /** - * 排队持续时间 - */ - queued_duration: number - /** - * 消息(可选) - */ - message?: string - } - - /* 管道 */ - export interface Pipeline { - /** - * 管道ID - */ - id: number - /** - * 项目ID - */ - project_id: number - /** - * SHA值 - */ - sha: string - /** - * 分支名 - */ - ref: string - /** - * 状态 - */ - status: string - /** - * 来源 - */ - source: string - /** - * 创建时间 - */ - created_at: string - /** - * 更新时间 - */ - updated_at: string - /** - * 管道网页URL - */ - web_url: string - } - - /* 任务 */ - export interface Job { - status: string - stage: string - } - - /* 徽章 */ - export interface Badge { - /** - * 徽章ID - */ - id: number - /** - * 徽章名称 - */ - name: string - /** - * 链接URL - */ - link_url: string - /** - * 图片URL - */ - image_url: string - /** - * 渲染后的链接URL - */ - rendered_link_url: string - /** - * 渲染后的图片URL - */ - rendered_image_url: string - /** - * 类型(项目或组) - */ - kind: "project" | "group" - } - /* 管道事件 */ export interface PipelineEvent { /** @@ -297,54 +132,4 @@ export namespace Gitlab { finished_at: string | null }[] } - - /* 合并请求 */ - export interface MergeRequest { - /** - * 合并请求的标题 - */ - title: string - /** - * 目标分支 - */ - target_branch: string - /** - * 源分支 - */ - source_branch: string - /** - * 作者信息 - */ - author: { - /** - * 用户名 - */ - username: string - /** - * 用户姓名 - */ - name: string - } - /** - * 提交的SHA值 - */ - sha: string - /** - * 合并提交的SHA值 - */ - merge_commit_sha: string - /** - * 引用信息 - */ - references: { - /** - * 完整引用 - */ - full: string - } - /** - * 合并请求的web URL - */ - web_url: string - } }