From fc0d008d29746e848ef1c415dde698027740b9a3 Mon Sep 17 00:00:00 2001 From: Liuying Zhang Date: Thu, 14 Mar 2024 14:28:11 +0800 Subject: [PATCH] remove lines header --- dest/index.js | 84 ++++++++++++++++++++++------------------------ src/DiffChecker.js | 78 +++++++++++++++++++++--------------------- src/index.js | 6 ++-- 3 files changed, 82 insertions(+), 86 deletions(-) diff --git a/dest/index.js b/dest/index.js index 90bbf96..3c0b8e7 100644 --- a/dest/index.js +++ b/dest/index.js @@ -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 */ @@ -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; } } } @@ -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 */ @@ -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); } } @@ -15246,6 +15259,7 @@ class DiffChecker { totalCoverageLines: this.getTotalCoverageReport(this.diffCoverageReport['total']), decreaseStatusLines, remainingStatusLines, + statusHeader: this.getStatusHeader(), } } @@ -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}` @@ -15375,23 +15387,14 @@ 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, @@ -15399,12 +15402,7 @@ class DiffChecker { } // 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, @@ -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(); @@ -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' } @@ -15801,7 +15799,7 @@ async function main() { messageToPost += '
' messageToPost += 'Click to view remaining coverage report\n\n' messageToPost += - 'Status | File | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n' + `Status | File ${statusHeader} \n -----|-----|---------|----------|---------|------ \n` messageToPost += remainingStatusLines.join('\n') messageToPost += '\n'; messageToPost += '
'; diff --git a/src/DiffChecker.js b/src/DiffChecker.js index 317d1b8..5e9b032 100644 --- a/src/DiffChecker.js +++ b/src/DiffChecker.js @@ -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 */ @@ -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; } } } @@ -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 */ @@ -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); } } @@ -125,6 +138,7 @@ export class DiffChecker { totalCoverageLines: this.getTotalCoverageReport(this.diffCoverageReport['total']), decreaseStatusLines, remainingStatusLines, + statusHeader: this.getStatusHeader(), } } @@ -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}` @@ -254,23 +266,14 @@ 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, @@ -278,12 +281,7 @@ export class DiffChecker { } // 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, diff --git a/src/index.js b/src/index.js index 9993624..8752a87 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); @@ -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' } @@ -138,7 +138,7 @@ async function main() { messageToPost += '
' messageToPost += 'Click to view remaining coverage report\n\n' messageToPost += - 'Status | File | Stmts | Branch | Funcs | Lines \n -----|-----|---------|----------|---------|------ \n' + `Status | File ${statusHeader} \n -----|-----|---------|----------|---------|------ \n` messageToPost += remainingStatusLines.join('\n') messageToPost += '\n'; messageToPost += '
';