Skip to content

Commit aeaf4aa

Browse files
andreiiurtiulpin
authored andcommitted
🐛 Make GHA respect coverage thresholds (QD-10150)
1 parent 2e34e66 commit aeaf4aa

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

common/qodana.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ export interface Coverage {
225225
freshCoverage: number
226226
freshLines: number
227227
freshCoveredLines: number
228+
totalCoverageThreshold: number
229+
freshCoverageThreshold: number
228230
}
229231

230232
/**
@@ -250,7 +252,16 @@ export function getCoverageFromSarif(sarifPath: string): Coverage {
250252
freshLines:
251253
sarifContents.runs[0].properties['coverage']['freshLines'] || 0,
252254
freshCoveredLines:
253-
sarifContents.runs[0].properties['coverage']['freshCoveredLines'] || 0
255+
sarifContents.runs[0].properties['coverage']['freshCoveredLines'] ||
256+
0,
257+
totalCoverageThreshold:
258+
sarifContents.runs[0].properties['qodanaFailureConditions']?.[
259+
'testCoverageThresholds'
260+
]?.['totalCoverage'] || COVERAGE_THRESHOLD,
261+
freshCoverageThreshold:
262+
sarifContents.runs[0].properties['qodanaFailureConditions']?.[
263+
'testCoverageThresholds'
264+
]?.['freshCoverage'] || COVERAGE_THRESHOLD
254265
}
255266
} else {
256267
return {
@@ -259,7 +270,9 @@ export function getCoverageFromSarif(sarifPath: string): Coverage {
259270
totalCoveredLines: 0,
260271
freshCoverage: 0,
261272
freshLines: 0,
262-
freshCoveredLines: 0
273+
freshCoveredLines: 0,
274+
totalCoverageThreshold: COVERAGE_THRESHOLD,
275+
freshCoverageThreshold: COVERAGE_THRESHOLD
263276
}
264277
}
265278
}

scan/__tests__/data/some.sarif.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8258,10 +8258,16 @@
82588258
"\n"
82598259
],
82608260
"properties": {
8261+
"qodanaFailureConditions": {
8262+
"testCoverageThresholds": {
8263+
"totalCoverage": 40,
8264+
"freshCoverage": 40
8265+
}
8266+
},
82618267
"coverage": {
8262-
"totalCoverage": 70.0,
8268+
"totalCoverage": 45.0,
82638269
"totalLines": 124.0,
8264-
"totalCoveredLines": 87.0
8270+
"totalCoveredLines": 56.0
82658271
}
82668272
}
82678273
}

scan/__tests__/main.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,14 @@ test('test empty summary output', () => {
101101

102102
test('test passed coverage output', () => {
103103
const result = getCoverageStats(
104-
getCoverageFromSarif('__tests__/data/some.sarif.json'),
105-
50
104+
getCoverageFromSarif('__tests__/data/some.sarif.json')
106105
)
107106
expect(result).toEqual(passedCoverageFixture())
108107
})
109108

110109
test('test failed coverage output', () => {
111110
const result = getCoverageStats(
112-
getCoverageFromSarif('__tests__/data/empty.sarif.json'),
113-
50
111+
getCoverageFromSarif('__tests__/data/empty.sarif.json')
114112
)
115113
expect(result).toEqual(failedCoverageFixture())
116114
})
@@ -353,8 +351,8 @@ Contact us at [[email protected]](mailto:[email protected]
353351
function passedCoverageFixture(): string {
354352
return `\`\`\`diff
355353
@@ Code coverage @@
356-
+ 70% total lines covered
357-
124 lines analyzed, 87 lines covered
354+
+ 45% total lines covered
355+
124 lines analyzed, 56 lines covered
358356
# Calculated according to the filters of your coverage tool
359357
\`\`\``
360358
}

scan/dist/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34337,7 +34337,9 @@ function getCoverageFromSarif(sarifPath) {
3433734337
totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] || 0,
3433834338
freshCoverage: sarifContents.runs[0].properties["coverage"]["freshCoverage"] || 0,
3433934339
freshLines: sarifContents.runs[0].properties["coverage"]["freshLines"] || 0,
34340-
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0
34340+
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0,
34341+
totalCoverageThreshold: sarifContents.runs[0].properties["qodanaFailureConditions"]?.["testCoverageThresholds"]?.["totalCoverage"] || COVERAGE_THRESHOLD,
34342+
freshCoverageThreshold: sarifContents.runs[0].properties["qodanaFailureConditions"]?.["testCoverageThresholds"]?.["freshCoverage"] || COVERAGE_THRESHOLD
3434134343
};
3434234344
} else {
3434334345
return {
@@ -34346,7 +34348,9 @@ function getCoverageFromSarif(sarifPath) {
3434634348
totalCoveredLines: 0,
3434734349
freshCoverage: 0,
3434834350
freshLines: 0,
34349-
freshCoveredLines: 0
34351+
freshCoveredLines: 0,
34352+
totalCoverageThreshold: COVERAGE_THRESHOLD,
34353+
freshCoverageThreshold: COVERAGE_THRESHOLD
3435034354
};
3435134355
}
3435234356
}
@@ -134644,14 +134648,14 @@ ${message}
134644134648
\`\`\``;
134645134649
}
134646134650
__name(wrapToDiffBlock, "wrapToDiffBlock");
134647-
function getCoverageStats(c, threshold) {
134651+
function getCoverageStats(c) {
134648134652
if (c.totalLines === 0 && c.totalCoveredLines === 0) {
134649134653
return "";
134650134654
}
134651134655
let stats = "";
134652134656
if (c.totalLines !== 0) {
134653134657
let conclusion = `${c.totalCoverage}% total lines covered`;
134654-
if (c.totalCoverage < threshold) {
134658+
if (c.totalCoverage < c.totalCoverageThreshold) {
134655134659
conclusion = `- ${conclusion}`;
134656134660
} else {
134657134661
conclusion = `+ ${conclusion}`;
@@ -134698,7 +134702,7 @@ ${c.freshLines} lines analyzed, ${c.freshCoveredLines} lines covered`;
134698134702
try {
134699134703
const problems = (0, annotations_1.parseSarif)(`${resultsDir}/${qodana_12.QODANA_SARIF_NAME}`);
134700134704
const reportUrl = getReportURL(resultsDir);
134701-
const coverageInfo = getCoverageStats((0, qodana_12.getCoverageFromSarif)(`${resultsDir}/${qodana_12.QODANA_SHORT_SARIF_NAME}`), qodana_12.COVERAGE_THRESHOLD);
134705+
const coverageInfo = getCoverageStats((0, qodana_12.getCoverageFromSarif)(`${resultsDir}/${qodana_12.QODANA_SHORT_SARIF_NAME}`));
134702134706
let licensesInfo = "";
134703134707
let packages = 0;
134704134708
const licensesJson = `${resultsDir}/projectStructure/${qodana_12.QODANA_LICENSES_JSON}`;

scan/src/output.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as core from '@actions/core'
1919
import * as fs from 'fs'
2020
import {
2121
Coverage,
22-
COVERAGE_THRESHOLD,
2322
getCoverageFromSarif,
2423
QODANA_LICENSES_JSON,
2524
QODANA_LICENSES_MD,
@@ -77,15 +76,15 @@ ${message}
7776
\`\`\``
7877
}
7978

80-
export function getCoverageStats(c: Coverage, threshold: number): string {
79+
export function getCoverageStats(c: Coverage): string {
8180
if (c.totalLines === 0 && c.totalCoveredLines === 0) {
8281
return ''
8382
}
8483

8584
let stats = ''
8685
if (c.totalLines !== 0) {
8786
let conclusion = `${c.totalCoverage}% total lines covered`
88-
if (c.totalCoverage < threshold) {
87+
if (c.totalCoverage < c.totalCoverageThreshold) {
8988
conclusion = `- ${conclusion}`
9089
} else {
9190
conclusion = `+ ${conclusion}`
@@ -153,8 +152,7 @@ export async function publishOutput(
153152
const problems = parseSarif(`${resultsDir}/${QODANA_SARIF_NAME}`)
154153
const reportUrl = getReportURL(resultsDir)
155154
const coverageInfo = getCoverageStats(
156-
getCoverageFromSarif(`${resultsDir}/${QODANA_SHORT_SARIF_NAME}`),
157-
COVERAGE_THRESHOLD
155+
getCoverageFromSarif(`${resultsDir}/${QODANA_SHORT_SARIF_NAME}`)
158156
)
159157
let licensesInfo = ''
160158
let packages = 0

vsts/QodanaScan/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9899,6 +9899,7 @@ function getQodanaScanArgs(args, resultsDir, cacheDir) {
98999899
return cliArgs;
99009900
}
99019901
function getCoverageFromSarif(sarifPath) {
9902+
var _a, _b, _c, _d;
99029903
if (fs.existsSync(sarifPath)) {
99039904
const sarifContents = JSON.parse(
99049905
fs.readFileSync(sarifPath, { encoding: "utf8" })
@@ -9910,7 +9911,9 @@ function getCoverageFromSarif(sarifPath) {
99109911
totalCoveredLines: sarifContents.runs[0].properties["coverage"]["totalCoveredLines"] || 0,
99119912
freshCoverage: sarifContents.runs[0].properties["coverage"]["freshCoverage"] || 0,
99129913
freshLines: sarifContents.runs[0].properties["coverage"]["freshLines"] || 0,
9913-
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0
9914+
freshCoveredLines: sarifContents.runs[0].properties["coverage"]["freshCoveredLines"] || 0,
9915+
totalCoverageThreshold: ((_b = (_a = sarifContents.runs[0].properties["qodanaFailureConditions"]) == null ? void 0 : _a["testCoverageThresholds"]) == null ? void 0 : _b["totalCoverage"]) || COVERAGE_THRESHOLD,
9916+
freshCoverageThreshold: ((_d = (_c = sarifContents.runs[0].properties["qodanaFailureConditions"]) == null ? void 0 : _c["testCoverageThresholds"]) == null ? void 0 : _d["freshCoverage"]) || COVERAGE_THRESHOLD
99149917
};
99159918
} else {
99169919
return {
@@ -9919,7 +9922,9 @@ function getCoverageFromSarif(sarifPath) {
99199922
totalCoveredLines: 0,
99209923
freshCoverage: 0,
99219924
freshLines: 0,
9922-
freshCoveredLines: 0
9925+
freshCoveredLines: 0,
9926+
totalCoverageThreshold: COVERAGE_THRESHOLD,
9927+
freshCoverageThreshold: COVERAGE_THRESHOLD
99239928
};
99249929
}
99259930
}

0 commit comments

Comments
 (0)