All checks were successful
CI Monitor MIflow / build-image (push) Successful in 1m1s
152 lines
5.6 KiB
TypeScript
152 lines
5.6 KiB
TypeScript
import { ProjectSchema } from "@gitbeaker/rest"
|
||
import pLimit from "p-limit"
|
||
|
||
import service from "../../service"
|
||
|
||
const projectList = [
|
||
// "cloud-ml/cloudml-maas",
|
||
// "cloud-ml/cloudml-permission",
|
||
// "cloud-ml/cloudml-base",
|
||
// "cloud-ml/cloudml-dataset",
|
||
// "cloud-ml/cloudml-dev",
|
||
// "cloud-ml/cloudml-train",
|
||
// "cloud-ml/cloudml-finetune",
|
||
// "cloud-ml/model-repository",
|
||
// "cloud-ml-public/cloudml-grpc-java",
|
||
// "cloud-ml/cloudml_runqueue",
|
||
// "cloud-ml/cloudml_syncqueue",
|
||
// "cloud-ml/resource-instance",
|
||
// "cloud-ml/ai-workbench-common",
|
||
// "cloud-ml/evaluate",
|
||
// "ai-service-data/ai-train-platform-service",
|
||
// "cloud-ml/embedding_management",
|
||
// "cloud-ml/knowledge-base",
|
||
// "cloud-ml/ai-proxy",
|
||
"cloud-ml/lark_auth",
|
||
]
|
||
|
||
interface BadgeSetParams {
|
||
groupId: number
|
||
linkUrl: string
|
||
imageUrl: string
|
||
name: string
|
||
}
|
||
|
||
const getNewProjectBadge = async (
|
||
projectDetail: ProjectSchema
|
||
): Promise<BadgeSetParams[]> => {
|
||
// 项目路径 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 = await service.gitlab.ProjectBadges.all(projectDetail.id).catch(
|
||
() => []
|
||
)
|
||
// 对badges进行补全,可能只有 name: "sonarqube_coverage" 的情况,把剩下的补全
|
||
const badgeNames = badges.map((badge) => badge.name)
|
||
const badgeNameSet = new Set(badgeNames)
|
||
const badgeNameList = [
|
||
"sonarqube_bugs",
|
||
"sonarqube_vulnerabilities",
|
||
"sonarqube_security_hotspots",
|
||
"sonarqube_coverage",
|
||
"sonarqube_quality_gate",
|
||
]
|
||
const diff = [...badgeNameList].filter((x) => !badgeNameSet.has(x))
|
||
const newBadges: BadgeSetParams[] = diff.map((name) => {
|
||
const linkUrl = encodeURI(
|
||
`https://sonarqube.mioffice.cn/dashboard?id=${sonarqubeId}`
|
||
)
|
||
const metric = name.replace("sonarqube_", "")
|
||
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 {
|
||
groupId: projectDetail.id,
|
||
linkUrl,
|
||
imageUrl,
|
||
name,
|
||
}
|
||
})
|
||
return newBadges
|
||
}
|
||
|
||
const addNewProjectBadge = async (badgeSetParamsList: 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)
|
||
)
|
||
)
|
||
)
|
||
}
|
||
|
||
const main = async () => {
|
||
const errorList: string[] = []
|
||
const badgeAddParamsList = await Promise.all(
|
||
projectList.map(async (projectName) => {
|
||
const projectDetail = await service.gitlab.Projects.show(
|
||
projectName
|
||
).catch(() => null)
|
||
if (!projectDetail) {
|
||
errorList.push(projectName)
|
||
return []
|
||
}
|
||
return await getNewProjectBadge(projectDetail)
|
||
})
|
||
)
|
||
|
||
await addNewProjectBadge(badgeAddParamsList.flat())
|
||
}
|
||
|
||
main()
|
||
|
||
// [
|
||
// {
|
||
// name: "sonarqube_bugs",
|
||
// link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-voiceprint-fe",
|
||
// image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-voiceprint-fe&metric=bugs",
|
||
// rendered_link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-voiceprint-fe",
|
||
// rendered_image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-voiceprint-fe&metric=bugs",
|
||
// id: 9725,
|
||
// kind: "project",
|
||
// }, {
|
||
// name: "sonarqube_vulnerabilities",
|
||
// link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=vulnerabilities",
|
||
// rendered_link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// rendered_image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=vulnerabilities",
|
||
// id: 10943,
|
||
// kind: "project",
|
||
// }, {
|
||
// name: "sonarqube_security_hotspots",
|
||
// link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=security_hotspots",
|
||
// rendered_link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// rendered_image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=security_hotspots",
|
||
// id: 10944,
|
||
// kind: "project",
|
||
// }, {
|
||
// name: "sonarqube_coverage",
|
||
// link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=coverage",
|
||
// rendered_link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// rendered_image_url: "https://sonarqube.mioffice.cn/api/badges/measure?key=miai-fe:fe:ai-shortcut-fe&metric=coverage",
|
||
// id: 10945,
|
||
// kind: "project",
|
||
// }, {
|
||
// name: "sonarqube_quality_gate",
|
||
// link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// image_url: "https://sonarqube.mioffice.cn/api/badges/gate?key=miai-fe:fe:ai-shortcut-fe",
|
||
// rendered_link_url: "https://sonarqube.mioffice.cn/dashboard?id=miai-fe:fe:ai-shortcut-fe",
|
||
// rendered_image_url: "https://sonarqube.mioffice.cn/api/badges/gate?key=miai-fe:fe:ai-shortcut-fe",
|
||
// id: 10946,
|
||
// kind: "project",
|
||
// }
|
||
// ]
|