From e360cbdd0674d4d2f0118be68067fca03fd37d7c Mon Sep 17 00:00:00 2001 From: tiulpin Date: Tue, 20 Jun 2023 16:21:28 +0200 Subject: [PATCH] :zap: Add fresh coverage support --- common/qodana.ts | 22 +++++++++--- scan/__tests__/data/empty.sarif.json | 5 ++- scan/__tests__/main.test.ts | 8 ++--- scan/dist/index.js | 50 ++++++++++++++++++---------- scan/src/output.ts | 41 +++++++++++++++-------- vsts/QodanaScan/index.js | 14 +++++--- 6 files changed, 95 insertions(+), 45 deletions(-) diff --git a/common/qodana.ts b/common/qodana.ts index 6bca287c..b10b3b63 100644 --- a/common/qodana.ts +++ b/common/qodana.ts @@ -173,6 +173,9 @@ export interface Coverage { totalCoverage: number totalLines: number totalCoveredLines: number + freshCoverage: number + freshLines: number + freshCoveredLines: number } /** @@ -187,16 +190,27 @@ export function getCoverageFromSarif(sarifPath: string): Coverage { if (sarifContents.runs[0].properties['coverage']) { return { totalCoverage: - sarifContents.runs[0].properties['coverage']['totalCoverage'], - totalLines: sarifContents.runs[0].properties['coverage']['totalLines'], + sarifContents.runs[0].properties['coverage']['totalCoverage'] || 0, + totalLines: + sarifContents.runs[0].properties['coverage']['totalLines'] || 0, totalCoveredLines: - sarifContents.runs[0].properties['coverage']['totalCoveredLines'] + sarifContents.runs[0].properties['coverage']['totalCoveredLines'] || + 0, + freshCoverage: + sarifContents.runs[0].properties['coverage']['freshCoverage'] || 0, + freshLines: + sarifContents.runs[0].properties['coverage']['freshLines'] || 0, + freshCoveredLines: + sarifContents.runs[0].properties['coverage']['freshCoveredLines'] || 0 } } else { return { totalCoverage: 0, totalLines: 0, - totalCoveredLines: 0 + totalCoveredLines: 0, + freshCoverage: 0, + freshLines: 0, + freshCoveredLines: 0 } } } diff --git a/scan/__tests__/data/empty.sarif.json b/scan/__tests__/data/empty.sarif.json index f32b214f..f210772f 100644 --- a/scan/__tests__/data/empty.sarif.json +++ b/scan/__tests__/data/empty.sarif.json @@ -8042,7 +8042,10 @@ "coverage": { "totalCoverage": 0, "totalLines": 100, - "totalCoveredLines": 0 + "totalCoveredLines": 0, + "freshCoverage": 0, + "freshLines": 100, + "freshCoveredLines": 0 } } } diff --git a/scan/__tests__/main.test.ts b/scan/__tests__/main.test.ts index 247c7fef..46655995 100644 --- a/scan/__tests__/main.test.ts +++ b/scan/__tests__/main.test.ts @@ -245,8 +245,7 @@ Contact us at [qodana-support@jetbrains.com](mailto:qodana-support@jetbrains.com function passedCoverageFixture(): string { return `\`\`\`diff @@ Code coverage @@ -✅ PASSED, required line coverage needs to be more than 50% -+ 70% lines covered ++ 70% total lines covered 124 lines analyzed, 87 lines covered # Calculated according to the filters of your coverage tool \`\`\`` @@ -255,8 +254,9 @@ function passedCoverageFixture(): string { function failedCoverageFixture(): string { return `\`\`\`diff @@ Code coverage @@ -❌ FAILED, required line coverage needs to be more than 50% -- 0% lines covered +- 0% total lines covered +100 lines analyzed, 0 lines covered +! 0 fresh lines covered 100 lines analyzed, 0 lines covered # Calculated according to the filters of your coverage tool \`\`\`` diff --git a/scan/dist/index.js b/scan/dist/index.js index 6cb34d65..6c9899e4 100644 --- a/scan/dist/index.js +++ b/scan/dist/index.js @@ -2884,15 +2884,21 @@ function getCoverageFromSarif(sarifPath) { ); if (sarifContents.runs[0].properties["coverage"]) { return { - totalCoverage: sarifContents.runs[0].properties["coverage"]["totalCoverage"], - totalLines: sarifContents.runs[0].properties["coverage"]["totalLines"], - totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] + totalCoverage: sarifContents.runs[0].properties["coverage"]["totalCoverage"] || 0, + totalLines: sarifContents.runs[0].properties["coverage"]["totalLines"] || 0, + totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] || 0, + freshCoverage: sarifContents.runs[0].properties["coverage"]["freshCoverage"] || 0, + freshLines: sarifContents.runs[0].properties["coverage"]["freshLines"] || 0, + freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0 }; } else { return { totalCoverage: 0, totalLines: 0, - totalCoveredLines: 0 + totalCoveredLines: 0, + freshCoverage: 0, + freshLines: 0, + freshCoveredLines: 0 }; } } @@ -63769,23 +63775,31 @@ ${message} \`\`\``; } __name(wrapToDiffBlock, "wrapToDiffBlock"); - function coverageConclusion(totalCoverage, threshold) { - if (totalCoverage < threshold) { - return `\u274C FAILED, required line coverage needs to be more than ${threshold}% -- ${totalCoverage}% lines covered`; - } - return `\u2705 PASSED, required line coverage needs to be more than ${threshold}% -+ ${totalCoverage}% lines covered`; - } - __name(coverageConclusion, "coverageConclusion"); function getCoverageStats(c, threshold) { - if (c.totalLines === 0) { + if (c.totalLines === 0 && c.totalCoveredLines === 0) { return ""; } - return wrapToDiffBlock(`@@ Code coverage @@ -${coverageConclusion(c.totalCoverage, threshold)} -${c.totalLines} lines analyzed, ${c.totalCoveredLines} lines covered -# Calculated according to the filters of your coverage tool`); + let stats = ""; + if (c.totalLines !== 0) { + let conclusion = `${c.totalCoverage}% total lines covered`; + if (c.totalCoverage < threshold) { + conclusion = `- ${conclusion}`; + } else { + conclusion = `+ ${conclusion}`; + } + stats += `${conclusion} +${c.totalLines} lines analyzed, ${c.totalCoveredLines} lines covered`; + } + if (c.freshLines !== 0) { + stats += ` +! ${c.freshCoverage} fresh lines covered +${c.freshLines} lines analyzed, ${c.freshCoveredLines} lines covered`; + } + return wrapToDiffBlock([ + `@@ Code coverage @@`, + `${stats}`, + `# Calculated according to the filters of your coverage tool` + ].join("\n")); } __name(getCoverageStats, "getCoverageStats"); exports2.getCoverageStats = getCoverageStats; diff --git a/scan/src/output.ts b/scan/src/output.ts index 848f2935..119117bf 100644 --- a/scan/src/output.ts +++ b/scan/src/output.ts @@ -58,23 +58,36 @@ ${message} \`\`\`` } -function coverageConclusion(totalCoverage: number, threshold: number): string { - if (totalCoverage < threshold) { - return `❌ FAILED, required line coverage needs to be more than ${threshold}% -- ${totalCoverage}% lines covered` - } - return `✅ PASSED, required line coverage needs to be more than ${threshold}% -+ ${totalCoverage}% lines covered` -} - export function getCoverageStats(c: Coverage, threshold: number): string { - if (c.totalLines === 0) { + if (c.totalLines === 0 && c.totalCoveredLines === 0) { return '' } - return wrapToDiffBlock(`@@ Code coverage @@ -${coverageConclusion(c.totalCoverage, threshold)} -${c.totalLines} lines analyzed, ${c.totalCoveredLines} lines covered -# Calculated according to the filters of your coverage tool`) + + let stats = '' + if (c.totalLines !== 0) { + let conclusion = `${c.totalCoverage}% total lines covered` + if (c.totalCoverage < threshold) { + conclusion = `- ${conclusion}` + } else { + conclusion = `+ ${conclusion}` + } + stats += `${conclusion} +${c.totalLines} lines analyzed, ${c.totalCoveredLines} lines covered` + } + + if (c.freshLines !== 0) { + stats += ` +! ${c.freshCoverage} fresh lines covered +${c.freshLines} lines analyzed, ${c.freshCoveredLines} lines covered` + } + + return wrapToDiffBlock( + [ + `@@ Code coverage @@`, + `${stats}`, + `# Calculated according to the filters of your coverage tool` + ].join('\n') + ) } /** diff --git a/vsts/QodanaScan/index.js b/vsts/QodanaScan/index.js index cf17fbeb..bcdef74e 100644 --- a/vsts/QodanaScan/index.js +++ b/vsts/QodanaScan/index.js @@ -152,15 +152,21 @@ function getCoverageFromSarif(sarifPath) { ); if (sarifContents.runs[0].properties["coverage"]) { return { - totalCoverage: sarifContents.runs[0].properties["coverage"]["totalCoverage"], - totalLines: sarifContents.runs[0].properties["coverage"]["totalLines"], - totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] + totalCoverage: sarifContents.runs[0].properties["coverage"]["totalCoverage"] || 0, + totalLines: sarifContents.runs[0].properties["coverage"]["totalLines"] || 0, + totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] || 0, + freshCoverage: sarifContents.runs[0].properties["coverage"]["freshCoverage"] || 0, + freshLines: sarifContents.runs[0].properties["coverage"]["freshLines"] || 0, + freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0 }; } else { return { totalCoverage: 0, totalLines: 0, - totalCoveredLines: 0 + totalCoveredLines: 0, + freshCoverage: 0, + freshLines: 0, + freshCoveredLines: 0 }; } }