Skip to content

Commit

Permalink
⚡ Add fresh coverage support
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed Jun 20, 2023
1 parent 95e5c52 commit e360cbd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 45 deletions.
22 changes: 18 additions & 4 deletions common/qodana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ export interface Coverage {
totalCoverage: number
totalLines: number
totalCoveredLines: number
freshCoverage: number
freshLines: number
freshCoveredLines: number
}

/**
Expand All @@ -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
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion scan/__tests__/data/empty.sarif.json
Original file line number Diff line number Diff line change
Expand Up @@ -8042,7 +8042,10 @@
"coverage": {
"totalCoverage": 0,
"totalLines": 100,
"totalCoveredLines": 0
"totalCoveredLines": 0,
"freshCoverage": 0,
"freshLines": 100,
"freshCoveredLines": 0
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions scan/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
\`\`\``
Expand All @@ -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
\`\`\``
Expand Down
50 changes: 32 additions & 18 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 27 additions & 14 deletions scan/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
}

/**
Expand Down
14 changes: 10 additions & 4 deletions vsts/QodanaScan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}
Expand Down

0 comments on commit e360cbd

Please sign in to comment.