Skip to content

Commit

Permalink
remove lines header
Browse files Browse the repository at this point in the history
  • Loading branch information
islyn committed Mar 14, 2024
1 parent a52a4b2 commit fc0d008
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 86 deletions.
84 changes: 41 additions & 43 deletions dest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15124,6 +15124,9 @@ const decreasedCoverageIcon = ':red_circle:'
const newCoverageIcon = ':new:'
const removedCoverageIcon = ':yellow_circle:'
const sparkleIcon = ':sparkles:'
const statusHeaders = ['Stmts', 'Branch', 'Funcs', 'Lines'];
const statusMetrics = ['statements', 'branches', 'functions', 'lines'];

/**
* DiffChecker is the simple algorithm to compare coverage
*/
Expand Down Expand Up @@ -15161,20 +15164,19 @@ class DiffChecker {
for (const filePath of reportKeys) {
const newCoverage = coverageReportNew[filePath] || {};
const oldCoverage = coverageReportOld[filePath] || {};
const metricsNewKeys = Object.keys(newCoverage);
const metricsOldKeys = Object.keys(oldCoverage);
const metricsKeys = new Set([...metricsNewKeys, ...metricsOldKeys]);
console.log(filePath)
this.diffCoverageReport[filePath] = {};
for (const metric of metricsKeys) {
this.diffCoverageReport[filePath][metric] = typeof newCoverage[metric] === 'object'
? {
new: newCoverage[metric],
old: oldCoverage[metric],
newPct: this.getPercentage(newCoverage[metric]),
oldPct: this.getPercentage(oldCoverage[metric]),
}
: newCoverage[metric];
for (const metric of statusMetrics) {
if (coverageType === 'cobertura' && metric === 'lines') break;
this.diffCoverageReport[filePath][metric] = {
new: newCoverage[metric],
old: oldCoverage[metric],
newPct: this.getPercentage(newCoverage[metric]),
oldPct: this.getPercentage(oldCoverage[metric]),
}
}
if (coverageType === 'cobertura') {
this.diffCoverageReport[filePath].filename = newCoverage.filename || filePath;
}
}
}
Expand Down Expand Up @@ -15205,6 +15207,24 @@ class DiffChecker {
return true;
}

getStatusMessage(prefix, callback) {
let statusMessage = prefix;
for(const metric of statusMetrics) {
if (this.coverageType === 'cobertura' && metric === 'lines') break;
statusMessage += callback(metric);
}
return statusMessage;
}

getStatusHeader() {
let statusMessage = '';
for(const header of statusHeaders) {
if (this.coverageType === 'cobertura' && header === 'Lines') break;
statusMessage += `| ${header} `;
}
return statusMessage;
}

/**
* Create coverageDetails table
*/
Expand All @@ -15230,14 +15250,7 @@ class DiffChecker {
}
} else {
if (!diffOnly) {
const diffFileCoverageData = this.diffCoverageReport[key];
const metrics = Object.keys(diffFileCoverageData);
let statusMessage = ` ${key.replace(this.currentDirectory, '')} `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ${this.diffFileCoverageData[metric].newPct} `;
}
});
const statusMessage = this.getStatusMessage(` ${key.replace(this.currentDirectory, '')} `, (metric) => `| ${this.diffFileCoverageData[key][metric].newPct} `);
remainingStatusLines.push(statusMessage);
}
}
Expand All @@ -15246,6 +15259,7 @@ class DiffChecker {
totalCoverageLines: this.getTotalCoverageReport(this.diffCoverageReport['total']),
decreaseStatusLines,
remainingStatusLines,
statusHeader: this.getStatusHeader(),
}
}

Expand Down Expand Up @@ -15360,8 +15374,6 @@ class DiffChecker {
coverageData => coverageData.newPct === 0
)

const metrics = Object.keys(diffFileCoverageData);

const fileNameUrl = this.getFileNameUrl(name);
if (fileNewCoverage) {
let newCoverageStatusIcon = `${sparkleIcon} ${newCoverageIcon}`
Expand All @@ -15375,36 +15387,22 @@ class DiffChecker {
newCoverageStatusIcon = `${increasedCoverageIcon} ${newCoverageIcon}`;
}
}
let statusMessage = ` ${newCoverageStatusIcon} | **${fileNameUrl}** `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| **${diffFileCoverageData[metric].newPct}** `;
}
});
const statusMessage = this.getStatusMessage(` ${newCoverageStatusIcon} | **${fileNameUrl}** `, (metric) => `| **${diffFileCoverageData[metric].newPct}** `);

return {
status: 'new',
statusMessage,
};
} else if (fileRemovedCoverage) {
let statusMessage = ` ${removedCoverageIcon} | ~~${fileNameUrl}~~ `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ~~${diffFileCoverageData[metric].oldPct}~~ `;
}
});
const statusMessage = this.getStatusMessage(` ${removedCoverageIcon} | ~~${fileNameUrl}~~ `, (metric) => `| ~~${diffFileCoverageData[metric].oldPct}~~ `);
return {
status: 'removed',
statusMessage,
}
}
// Coverage existed before so calculate the diff status
const statusIcon = this.getStatusIcon(diffFileCoverageData)
let statusMessage = ` ${statusIcon} | ${fileNameUrl} `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ${diffFileCoverageData[metric].newPct} **(${this.getPercentageDiff(diffFileCoverageData[metric])})** `;
}
});
const statusMessage = this.getStatusMessage(` ${statusIcon} | ${fileNameUrl} `, (metric) => `| ${diffFileCoverageData[metric].newPct} **(${this.getPercentageDiff(diffFileCoverageData[metric])})** `);
return {
status: statusIcon === increasedCoverageIcon ? 'increase' : 'decrease',
statusMessage,
Expand Down Expand Up @@ -15760,7 +15758,7 @@ async function main() {

// Get coverage details.
// fullCoverage: This will provide a full coverage report. You can set it to false if you do not need full coverage
const { decreaseStatusLines, remainingStatusLines, totalCoverageLines } = diffChecker.getCoverageDetails(!fullCoverage)
const { decreaseStatusLines, remainingStatusLines, totalCoverageLines, statusHeader } = diffChecker.getCoverageDetails(!fullCoverage)

const isCoverageBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(delta);
const isNotFullCoverageOnNewFile = diffChecker.checkIfNewFileNotFullCoverage();
Expand Down Expand Up @@ -15791,7 +15789,7 @@ async function main() {
messageToPost += '--- \n\n'
if (decreaseStatusLines.length > 0) {
messageToPost +=
'Status | Changes Missing Coverage | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n'
`Status | Changes Missing Coverage ${statusHeader} \n -----|-----|---------|----------|---------|------ \n`
messageToPost += decreaseStatusLines.join('\n')
messageToPost += '\n--- \n\n'
}
Expand All @@ -15801,7 +15799,7 @@ async function main() {
messageToPost += '<details>'
messageToPost += '<summary markdown="span">Click to view remaining coverage report</summary>\n\n'
messageToPost +=
'Status | File | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n'
`Status | File ${statusHeader} \n -----|-----|---------|----------|---------|------ \n`
messageToPost += remainingStatusLines.join('\n')
messageToPost += '\n';
messageToPost += '</details>';
Expand Down
78 changes: 38 additions & 40 deletions src/DiffChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const decreasedCoverageIcon = ':red_circle:'
const newCoverageIcon = ':new:'
const removedCoverageIcon = ':yellow_circle:'
const sparkleIcon = ':sparkles:'
const statusHeaders = ['Stmts', 'Branch', 'Funcs', 'Lines'];
const statusMetrics = ['statements', 'branches', 'functions', 'lines'];

/**
* DiffChecker is the simple algorithm to compare coverage
*/
Expand Down Expand Up @@ -40,20 +43,19 @@ export class DiffChecker {
for (const filePath of reportKeys) {
const newCoverage = coverageReportNew[filePath] || {};
const oldCoverage = coverageReportOld[filePath] || {};
const metricsNewKeys = Object.keys(newCoverage);
const metricsOldKeys = Object.keys(oldCoverage);
const metricsKeys = new Set([...metricsNewKeys, ...metricsOldKeys]);
console.log(filePath)
this.diffCoverageReport[filePath] = {};
for (const metric of metricsKeys) {
this.diffCoverageReport[filePath][metric] = typeof newCoverage[metric] === 'object'
? {
new: newCoverage[metric],
old: oldCoverage[metric],
newPct: this.getPercentage(newCoverage[metric]),
oldPct: this.getPercentage(oldCoverage[metric]),
}
: newCoverage[metric];
for (const metric of statusMetrics) {
if (coverageType === 'cobertura' && metric === 'lines') break;
this.diffCoverageReport[filePath][metric] = {
new: newCoverage[metric],
old: oldCoverage[metric],
newPct: this.getPercentage(newCoverage[metric]),
oldPct: this.getPercentage(oldCoverage[metric]),
}
}
if (coverageType === 'cobertura') {
this.diffCoverageReport[filePath].filename = newCoverage.filename || filePath;
}
}
}
Expand Down Expand Up @@ -84,6 +86,24 @@ export class DiffChecker {
return true;
}

getStatusMessage(prefix, callback) {
let statusMessage = prefix;
for(const metric of statusMetrics) {
if (this.coverageType === 'cobertura' && metric === 'lines') break;
statusMessage += callback(metric);
}
return statusMessage;
}

getStatusHeader() {
let statusMessage = '';
for(const header of statusHeaders) {
if (this.coverageType === 'cobertura' && header === 'Lines') break;
statusMessage += `| ${header} `;
}
return statusMessage;
}

/**
* Create coverageDetails table
*/
Expand All @@ -109,14 +129,7 @@ export class DiffChecker {
}
} else {
if (!diffOnly) {
const diffFileCoverageData = this.diffCoverageReport[key];
const metrics = Object.keys(diffFileCoverageData);
let statusMessage = ` ${key.replace(this.currentDirectory, '')} `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ${this.diffFileCoverageData[metric].newPct} `;
}
});
const statusMessage = this.getStatusMessage(` ${key.replace(this.currentDirectory, '')} `, (metric) => `| ${this.diffFileCoverageData[key][metric].newPct} `);
remainingStatusLines.push(statusMessage);
}
}
Expand All @@ -125,6 +138,7 @@ export class DiffChecker {
totalCoverageLines: this.getTotalCoverageReport(this.diffCoverageReport['total']),
decreaseStatusLines,
remainingStatusLines,
statusHeader: this.getStatusHeader(),
}
}

Expand Down Expand Up @@ -239,8 +253,6 @@ export class DiffChecker {
coverageData => coverageData.newPct === 0
)

const metrics = Object.keys(diffFileCoverageData);

const fileNameUrl = this.getFileNameUrl(name);
if (fileNewCoverage) {
let newCoverageStatusIcon = `${sparkleIcon} ${newCoverageIcon}`
Expand All @@ -254,36 +266,22 @@ export class DiffChecker {
newCoverageStatusIcon = `${increasedCoverageIcon} ${newCoverageIcon}`;
}
}
let statusMessage = ` ${newCoverageStatusIcon} | **${fileNameUrl}** `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| **${diffFileCoverageData[metric].newPct}** `;
}
});
const statusMessage = this.getStatusMessage(` ${newCoverageStatusIcon} | **${fileNameUrl}** `, (metric) => `| **${diffFileCoverageData[metric].newPct}** `);

return {
status: 'new',
statusMessage,
};
} else if (fileRemovedCoverage) {
let statusMessage = ` ${removedCoverageIcon} | ~~${fileNameUrl}~~ `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ~~${diffFileCoverageData[metric].oldPct}~~ `;
}
});
const statusMessage = this.getStatusMessage(` ${removedCoverageIcon} | ~~${fileNameUrl}~~ `, (metric) => `| ~~${diffFileCoverageData[metric].oldPct}~~ `);
return {
status: 'removed',
statusMessage,
}
}
// Coverage existed before so calculate the diff status
const statusIcon = this.getStatusIcon(diffFileCoverageData)
let statusMessage = ` ${statusIcon} | ${fileNameUrl} `;
metrics.forEach(metric => {
if (typeof diffFileCoverageData[metric] === 'object') {
statusMessage += `| ${diffFileCoverageData[metric].newPct} **(${this.getPercentageDiff(diffFileCoverageData[metric])})** `;
}
});
const statusMessage = this.getStatusMessage(` ${statusIcon} | ${fileNameUrl} `, (metric) => `| ${diffFileCoverageData[metric].newPct} **(${this.getPercentageDiff(diffFileCoverageData[metric])})** `);
return {
status: statusIcon === increasedCoverageIcon ? 'increase' : 'decrease',
statusMessage,
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function main() {

// Get coverage details.
// fullCoverage: This will provide a full coverage report. You can set it to false if you do not need full coverage
const { decreaseStatusLines, remainingStatusLines, totalCoverageLines } = diffChecker.getCoverageDetails(!fullCoverage)
const { decreaseStatusLines, remainingStatusLines, totalCoverageLines, statusHeader } = diffChecker.getCoverageDetails(!fullCoverage)

const isCoverageBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(delta);
const isNotFullCoverageOnNewFile = diffChecker.checkIfNewFileNotFullCoverage();
Expand Down Expand Up @@ -128,7 +128,7 @@ async function main() {
messageToPost += '--- \n\n'
if (decreaseStatusLines.length > 0) {
messageToPost +=
'Status | Changes Missing Coverage | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n'
`Status | Changes Missing Coverage ${statusHeader} \n -----|-----|---------|----------|---------|------ \n`
messageToPost += decreaseStatusLines.join('\n')
messageToPost += '\n--- \n\n'
}
Expand All @@ -138,7 +138,7 @@ async function main() {
messageToPost += '<details>'
messageToPost += '<summary markdown="span">Click to view remaining coverage report</summary>\n\n'
messageToPost +=
'Status | File | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n'
`Status | File ${statusHeader} \n -----|-----|---------|----------|---------|------ \n`
messageToPost += remainingStatusLines.join('\n')
messageToPost += '\n';
messageToPost += '</details>';
Expand Down

0 comments on commit fc0d008

Please sign in to comment.