Skip to content

Commit

Permalink
🐛 Make GHA respect coverage thresholds (QD-10150)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiiur authored and tiulpin committed Dec 3, 2024
1 parent 2e34e66 commit aeaf4aa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
17 changes: 15 additions & 2 deletions common/qodana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export interface Coverage {
freshCoverage: number
freshLines: number
freshCoveredLines: number
totalCoverageThreshold: number
freshCoverageThreshold: number
}

/**
Expand All @@ -250,7 +252,16 @@ export function getCoverageFromSarif(sarifPath: string): Coverage {
freshLines:
sarifContents.runs[0].properties['coverage']['freshLines'] || 0,
freshCoveredLines:
sarifContents.runs[0].properties['coverage']['freshCoveredLines'] || 0
sarifContents.runs[0].properties['coverage']['freshCoveredLines'] ||
0,
totalCoverageThreshold:
sarifContents.runs[0].properties['qodanaFailureConditions']?.[
'testCoverageThresholds'
]?.['totalCoverage'] || COVERAGE_THRESHOLD,
freshCoverageThreshold:
sarifContents.runs[0].properties['qodanaFailureConditions']?.[
'testCoverageThresholds'
]?.['freshCoverage'] || COVERAGE_THRESHOLD
}
} else {
return {
Expand All @@ -259,7 +270,9 @@ export function getCoverageFromSarif(sarifPath: string): Coverage {
totalCoveredLines: 0,
freshCoverage: 0,
freshLines: 0,
freshCoveredLines: 0
freshCoveredLines: 0,
totalCoverageThreshold: COVERAGE_THRESHOLD,
freshCoverageThreshold: COVERAGE_THRESHOLD
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions scan/__tests__/data/some.sarif.json
Original file line number Diff line number Diff line change
Expand Up @@ -8258,10 +8258,16 @@
"\n"
],
"properties": {
"qodanaFailureConditions": {
"testCoverageThresholds": {
"totalCoverage": 40,
"freshCoverage": 40
}
},
"coverage": {
"totalCoverage": 70.0,
"totalCoverage": 45.0,
"totalLines": 124.0,
"totalCoveredLines": 87.0
"totalCoveredLines": 56.0
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions scan/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ test('test empty summary output', () => {

test('test passed coverage output', () => {
const result = getCoverageStats(
getCoverageFromSarif('__tests__/data/some.sarif.json'),
50
getCoverageFromSarif('__tests__/data/some.sarif.json')
)
expect(result).toEqual(passedCoverageFixture())
})

test('test failed coverage output', () => {
const result = getCoverageStats(
getCoverageFromSarif('__tests__/data/empty.sarif.json'),
50
getCoverageFromSarif('__tests__/data/empty.sarif.json')
)
expect(result).toEqual(failedCoverageFixture())
})
Expand Down Expand Up @@ -353,8 +351,8 @@ Contact us at [qodana-support@jetbrains.com](mailto:qodana-support@jetbrains.com
function passedCoverageFixture(): string {
return `\`\`\`diff
@@ Code coverage @@
+ 70% total lines covered
124 lines analyzed, 87 lines covered
+ 45% total lines covered
124 lines analyzed, 56 lines covered
# Calculated according to the filters of your coverage tool
\`\`\``
}
Expand Down
14 changes: 9 additions & 5 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34337,7 +34337,9 @@ function getCoverageFromSarif(sarifPath) {
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
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0,
totalCoverageThreshold: sarifContents.runs[0].properties["qodanaFailureConditions"]?.["testCoverageThresholds"]?.["totalCoverage"] || COVERAGE_THRESHOLD,
freshCoverageThreshold: sarifContents.runs[0].properties["qodanaFailureConditions"]?.["testCoverageThresholds"]?.["freshCoverage"] || COVERAGE_THRESHOLD
};
} else {
return {
Expand All @@ -34346,7 +34348,9 @@ function getCoverageFromSarif(sarifPath) {
totalCoveredLines: 0,
freshCoverage: 0,
freshLines: 0,
freshCoveredLines: 0
freshCoveredLines: 0,
totalCoverageThreshold: COVERAGE_THRESHOLD,
freshCoverageThreshold: COVERAGE_THRESHOLD
};
}
}
Expand Down Expand Up @@ -134644,14 +134648,14 @@ ${message}
\`\`\``;
}
__name(wrapToDiffBlock, "wrapToDiffBlock");
function getCoverageStats(c, threshold) {
function getCoverageStats(c) {
if (c.totalLines === 0 && c.totalCoveredLines === 0) {
return "";
}
let stats = "";
if (c.totalLines !== 0) {
let conclusion = `${c.totalCoverage}% total lines covered`;
if (c.totalCoverage < threshold) {
if (c.totalCoverage < c.totalCoverageThreshold) {
conclusion = `- ${conclusion}`;
} else {
conclusion = `+ ${conclusion}`;
Expand Down Expand Up @@ -134698,7 +134702,7 @@ ${c.freshLines} lines analyzed, ${c.freshCoveredLines} lines covered`;
try {
const problems = (0, annotations_1.parseSarif)(`${resultsDir}/${qodana_12.QODANA_SARIF_NAME}`);
const reportUrl = getReportURL(resultsDir);
const coverageInfo = getCoverageStats((0, qodana_12.getCoverageFromSarif)(`${resultsDir}/${qodana_12.QODANA_SHORT_SARIF_NAME}`), qodana_12.COVERAGE_THRESHOLD);
const coverageInfo = getCoverageStats((0, qodana_12.getCoverageFromSarif)(`${resultsDir}/${qodana_12.QODANA_SHORT_SARIF_NAME}`));
let licensesInfo = "";
let packages = 0;
const licensesJson = `${resultsDir}/projectStructure/${qodana_12.QODANA_LICENSES_JSON}`;
Expand Down
8 changes: 3 additions & 5 deletions scan/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as core from '@actions/core'
import * as fs from 'fs'
import {
Coverage,
COVERAGE_THRESHOLD,
getCoverageFromSarif,
QODANA_LICENSES_JSON,
QODANA_LICENSES_MD,
Expand Down Expand Up @@ -77,15 +76,15 @@ ${message}
\`\`\``
}

export function getCoverageStats(c: Coverage, threshold: number): string {
export function getCoverageStats(c: Coverage): string {
if (c.totalLines === 0 && c.totalCoveredLines === 0) {
return ''
}

let stats = ''
if (c.totalLines !== 0) {
let conclusion = `${c.totalCoverage}% total lines covered`
if (c.totalCoverage < threshold) {
if (c.totalCoverage < c.totalCoverageThreshold) {
conclusion = `- ${conclusion}`
} else {
conclusion = `+ ${conclusion}`
Expand Down Expand Up @@ -153,8 +152,7 @@ export async function publishOutput(
const problems = parseSarif(`${resultsDir}/${QODANA_SARIF_NAME}`)
const reportUrl = getReportURL(resultsDir)
const coverageInfo = getCoverageStats(
getCoverageFromSarif(`${resultsDir}/${QODANA_SHORT_SARIF_NAME}`),
COVERAGE_THRESHOLD
getCoverageFromSarif(`${resultsDir}/${QODANA_SHORT_SARIF_NAME}`)
)
let licensesInfo = ''
let packages = 0
Expand Down
9 changes: 7 additions & 2 deletions vsts/QodanaScan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9899,6 +9899,7 @@ function getQodanaScanArgs(args, resultsDir, cacheDir) {
return cliArgs;
}
function getCoverageFromSarif(sarifPath) {
var _a, _b, _c, _d;
if (fs.existsSync(sarifPath)) {
const sarifContents = JSON.parse(
fs.readFileSync(sarifPath, { encoding: "utf8" })
Expand All @@ -9910,7 +9911,9 @@ function getCoverageFromSarif(sarifPath) {
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
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0,
totalCoverageThreshold: ((_b = (_a = sarifContents.runs[0].properties["qodanaFailureConditions"]) == null ? void 0 : _a["testCoverageThresholds"]) == null ? void 0 : _b["totalCoverage"]) || COVERAGE_THRESHOLD,
freshCoverageThreshold: ((_d = (_c = sarifContents.runs[0].properties["qodanaFailureConditions"]) == null ? void 0 : _c["testCoverageThresholds"]) == null ? void 0 : _d["freshCoverage"]) || COVERAGE_THRESHOLD
};
} else {
return {
Expand All @@ -9919,7 +9922,9 @@ function getCoverageFromSarif(sarifPath) {
totalCoveredLines: 0,
freshCoverage: 0,
freshLines: 0,
freshCoveredLines: 0
freshCoveredLines: 0,
totalCoverageThreshold: COVERAGE_THRESHOLD,
freshCoverageThreshold: COVERAGE_THRESHOLD
};
}
}
Expand Down

0 comments on commit aeaf4aa

Please sign in to comment.