142 lines
5.7 KiB
TypeScript
142 lines
5.7 KiB
TypeScript
import service from "../../service"
|
||
import { BadgeSetParams } from "../../service/gitlab/badge"
|
||
import { Gitlab } from "../../types/gitlab"
|
||
|
||
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",
|
||
]
|
||
|
||
const getNewProjectBadge = async (
|
||
projectDetail: Gitlab.ProjDetail
|
||
): 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: Gitlab.Badge[] = await service.gitlab.badge.get(
|
||
projectDetail.id
|
||
)
|
||
// 对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 link_url = encodeURI(
|
||
`https://sonarqube.mioffice.cn/dashboard?id=${sonarqubeId}`
|
||
)
|
||
const metric = name.replace("sonarqube_", "")
|
||
const image_url =
|
||
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,
|
||
name,
|
||
}
|
||
})
|
||
return newBadges
|
||
}
|
||
|
||
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))
|
||
)
|
||
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)
|
||
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",
|
||
// }
|
||
// ]
|