From f1157a8de54f1f7c653b3351923bba1fd5de6db4 Mon Sep 17 00:00:00 2001 From: Allen Zhang Date: Wed, 27 Dec 2023 11:45:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A6=82=E8=A7=88=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/canyon-backend/src/constant.ts | 26 +++ .../src/coverage/coverage.controller.ts | 14 +- .../retrieve-coverage-tree-summary.service.ts | 64 +++++-- .../services/trigger-agg-coverage.service.ts | 173 ++++++++++-------- 4 files changed, 177 insertions(+), 100 deletions(-) create mode 100644 packages/canyon-backend/src/constant.ts diff --git a/packages/canyon-backend/src/constant.ts b/packages/canyon-backend/src/constant.ts new file mode 100644 index 00000000..4bb3f47e --- /dev/null +++ b/packages/canyon-backend/src/constant.ts @@ -0,0 +1,26 @@ +export const emptyStatistics = { + newlines: { + total: 0, + covered: 0, + skipped: 0, + pct: 0, + }, + lines: { + total: 0, + covered: 0, + skipped: 0, + pct: 0, + }, + functions: { + total: 0, + covered: 0, + skipped: 0, + pct: 0, + }, + branches: { + total: 0, + covered: 0, + skipped: 0, + pct: 0, + }, +}; diff --git a/packages/canyon-backend/src/coverage/coverage.controller.ts b/packages/canyon-backend/src/coverage/coverage.controller.ts index b707f733..4e93a40b 100755 --- a/packages/canyon-backend/src/coverage/coverage.controller.ts +++ b/packages/canyon-backend/src/coverage/coverage.controller.ts @@ -55,13 +55,12 @@ export class CoverageController { reportId?: string; report_id?: string; reportID?: string; - commitSha?: string; - commit_sha?: string; + sha?: string; }, ) { return this.triggerAggCoverageService.invoke({ reportID: params.reportId || params.report_id || params.reportID, - commitSha: params.commitSha || params.commit_sha || null, + sha: params.sha, }); } @@ -91,12 +90,17 @@ export class CoverageController { commitsha?: string; commitSha?: string; commit_sha?: string; + sha?: string; }, ) { return this.retrieveCoverageTreeSummaryService.invoke({ reportID: params.reportId || params.report_id || params.reportID || null, - commitSha: - params.commitsha || params.commitSha || params.commit_sha || null, + sha: + params.sha || + params.commitsha || + params.commitSha || + params.commit_sha || + null, }); } diff --git a/packages/canyon-backend/src/coverage/services/retrieve-coverage-tree-summary.service.ts b/packages/canyon-backend/src/coverage/services/retrieve-coverage-tree-summary.service.ts index 643b7fdd..f84b5937 100755 --- a/packages/canyon-backend/src/coverage/services/retrieve-coverage-tree-summary.service.ts +++ b/packages/canyon-backend/src/coverage/services/retrieve-coverage-tree-summary.service.ts @@ -1,46 +1,82 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from '../../prisma/prisma.service'; -import { getProjectByID } from '../../adapter/gitlab.adapter'; import { calculateCoverageOverviewByConditionFilter } from '../../utils/summary'; import { removeNullKeys } from '../../utils/utils'; +import { emptyStatistics } from '../../constant'; @Injectable() export class RetrieveCoverageTreeSummaryService { constructor(private readonly prisma: PrismaService) {} - async invoke(params) { + async invoke(params: { reportID?: string; sha?: string }) { const redirectUri = process.env.REDIRECT_URI; - const { commitSha } = await this.prisma.summary.findFirst({ + const summaryFindFirst = await this.prisma.summary.findFirst({ where: removeNullKeys({ reportID: params.reportID, - commitSha: params.commitSha, - covType: 'all', + commitSha: params.sha, + covType: 'agg', }), }); - const cv = await this.prisma.coverage.findFirst({ + const commitSha = summaryFindFirst?.commitSha; + + if (!commitSha) { + const noCommitShaWithCoverage = await this.prisma.coverage.findFirst({ + where: removeNullKeys({ + reportID: params.reportID, + commitSha: params.sha, + }), + }); + return { + status: 'pending', + reportIDs: [], + sha: noCommitShaWithCoverage?.commitSha || '', + statistics: emptyStatistics, + }; + } + + const coverage = await this.prisma.coverage.findFirst({ where: { commitSha, }, }); - const repoGitlabInfo = await getProjectByID( - cv.projectID, - 'gdr7c7GAPjZ2JXWAZKXH', - ); + const summarys = await this.prisma.summary.findMany({ where: { commitSha, covType: 'all', }, }); - // 这边加一下状态,只能根据commit查,并且必须是所有关联的reportID都是success才返回 + + const tasks = await this.prisma.task.findMany({ + where: { + commitSha, + projectID: coverage.projectID, + }, + }); + function getStatus(tasks) { + const state = tasks.every((task) => task.status === 'success'); + if (state) { + return 'success'; + } else if ( + tasks.some( + (task) => task.status === 'running' || task.status === 'notstarted', + ) + ) { + return 'pending'; + } + return 'fail'; + } return { + status: getStatus(tasks), + reportIDs: tasks + .filter(({ status }) => status) + .map(({ reportID }) => reportID), reportUrl: `${(redirectUri || '').replace('/login', '')}/project/${ - cv.projectID + coverage.projectID }/commits/${commitSha}`, - commitSha: commitSha, + sha: commitSha, statistics: calculateCoverageOverviewByConditionFilter(summarys), - repo: repoGitlabInfo, }; } } diff --git a/packages/canyon-backend/src/coverage/services/trigger-agg-coverage.service.ts b/packages/canyon-backend/src/coverage/services/trigger-agg-coverage.service.ts index 15884766..60780954 100755 --- a/packages/canyon-backend/src/coverage/services/trigger-agg-coverage.service.ts +++ b/packages/canyon-backend/src/coverage/services/trigger-agg-coverage.service.ts @@ -9,102 +9,113 @@ function checkTasksStatus(tasks) { @Injectable() export class TriggerAggCoverageService { constructor(private readonly prisma: PrismaService) {} + // 传sha返回一个数组 + // 传reportID返回一个对象 + async invoke(params: { reportID?: string; sha?: string }) { + if (params.sha) { + const reportIDs = await this.prisma.coverage.findMany({ + where: { + commitSha: params.sha, + covType: 'normal', + }, + distinct: ['reportID'], + select: { + reportID: true, + }, + }); - async invoke(params) { - const { commitSha } = params; - - const reportIDs = await this.prisma.coverage.findMany({ + const results = []; + for (let i = 0; i < reportIDs.length; i++) { + const result = await this.createAggTaskByReportID( + reportIDs[i].reportID, + ); + results.push(result); + } + return results; + } else if (params.reportID) { + return await this.createAggTaskByReportID(params.reportID); + } else { + return { + msg: '参数错误', + data: [], + code: 0, + reportID: params.reportID, + }; + } + } + async createAggTaskByReportID(reportID) { + const tasks = await this.prisma.task.findMany({ where: { - commitSha, + reportID, }, - distinct: ['reportID'], - select: { - reportID: true, + }); + const coverages = await this.prisma.coverage.findMany({ + where: { + reportID: reportID, }, }); - - const arr = []; - for (let i = 0; i < reportIDs.length; i++) { - const s = await ffffffff.call(this, { reportID: reportIDs[i].reportID }); - arr.push(s); + if (coverages.length === 0) { + return { + msg: '未查询到reportId', + data: [], + code: 0, + reportID: reportID, + }; } - return arr; + // 如果有聚合在跑,就提示在聚合 + if (tasks.length > 0 && checkTasksStatus(tasks)) { + return { + msg: '报告聚合中', + data: [], + code: -1, + reportID: reportID, + }; + } else { + // 如果这个 reportID聚合完成了,就删除旧的聚合任务,创建新的聚合任务 + for (let i = 0; i < tasks.length; i++) { + await this.prisma.task.deleteMany({ + where: { + id: tasks[i].id, + }, + }); + console.log('删除旧的聚合任务' + tasks[i].id); + } + + // ********** 重要 ********** + // 关键 + // ********** 重要 ********** - async function ffffffff() { - const { reportID } = params; - const tasks = await this.prisma.task.findMany({ + const commitsAssociatedWithReport = await this.prisma.coverage.findMany({ where: { reportID, }, - }); - const coverages = await this.prisma.coverage.findMany({ - where: { - reportID: reportID, + distinct: ['commitSha'], // 使用 distinct 来确保返回唯一的 commitSha + select: { + commitSha: true, + projectID: true, // 添加 projectID 到返回结果中 }, }); - if (coverages.length === 0) { - return { - msg: '未查询到reportId', - data: [], - code: 0, - }; - } - - // 如果有聚合在跑,就提示在聚合 - if (tasks.length > 0 && checkTasksStatus(tasks)) { - return { - msg: '报告聚合中', - data: [], - code: -1, - }; - } else { - // 如果这个 reportID聚合完成了,就删除旧的聚合任务,创建新的聚合任务 - for (let i = 0; i < tasks.length; i++) { - await this.prisma.task.deleteMany({ - where: { - id: tasks[i].id, - }, - }); - console.log('删除旧的聚合任务' + tasks[i].id); - } - - // ********** 重要 ********** - // 关键 - // ********** 重要 ********** - - const commitsAssociatedWithReport = await this.prisma.coverage.findMany( - { - where: { - reportID, - }, - distinct: ['commitSha'], // 使用 distinct 来确保返回唯一的 commitSha - select: { - commitSha: true, - projectID: true, // 添加 projectID 到返回结果中 - }, + for (let i = 0; i < commitsAssociatedWithReport.length; i++) { + const { commitSha, projectID } = commitsAssociatedWithReport[i]; + await this.prisma.task.create({ + data: { + name: `Coverage Agg Task ${reportID},commitSha:${commitSha},projectID:${projectID}`, + status: 'notstarted', + reportID, + commitSha: commitSha, + projectID: projectID, + result: {}, }, - ); - for (let i = 0; i < commitsAssociatedWithReport.length; i++) { - const { commitSha, projectID } = commitsAssociatedWithReport[i]; - await this.prisma.task.create({ - data: { - name: `Coverage Agg Task ${reportID},commitSha:${commitSha},projectID:${projectID}`, - status: 'notstarted', - reportID, - commitSha: commitSha, - projectID: projectID, - result: {}, - }, - }); - } - - return { - msg: '聚合中', - data: [], - code: 1, - }; + }); } + + return { + msg: '聚合中', + data: [], + code: 1, + reportID: reportID, + }; } } }