|
| 1 | +const fs = require('fs'); |
| 2 | + |
| 3 | +// Configuration thresholds |
| 4 | +const THRESHOLDS = { |
| 5 | + good: 80, |
| 6 | + needsImprovement: 60, |
| 7 | + poor: 40 |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Parse lcov.info file and extract coverage metrics |
| 12 | + * @param {string} content - The lcov.info file content |
| 13 | + * @returns {object} Coverage metrics |
| 14 | + */ |
| 15 | +function parseLcovContent(content) { |
| 16 | + const lines = content.split('\n'); |
| 17 | + let totalLines = 0; |
| 18 | + let coveredLines = 0; |
| 19 | + let totalFunctions = 0; |
| 20 | + let coveredFunctions = 0; |
| 21 | + let totalBranches = 0; |
| 22 | + let coveredBranches = 0; |
| 23 | + |
| 24 | + // LF:, LH:, FNF:, FNH:, BRF:, BRH: are on separate lines |
| 25 | + // We need to track them separately and sum them up |
| 26 | + for (let i = 0; i < lines.length; i++) { |
| 27 | + const line = lines[i].trim(); |
| 28 | + |
| 29 | + // Lines Found and Lines Hit |
| 30 | + if (line.startsWith('LF:')) { |
| 31 | + totalLines += parseInt(line.substring(3)) || 0; |
| 32 | + } |
| 33 | + if (line.startsWith('LH:')) { |
| 34 | + coveredLines += parseInt(line.substring(3)) || 0; |
| 35 | + } |
| 36 | + |
| 37 | + // Functions Found and Functions Hit |
| 38 | + if (line.startsWith('FNF:')) { |
| 39 | + totalFunctions += parseInt(line.substring(4)) || 0; |
| 40 | + } |
| 41 | + if (line.startsWith('FNH:')) { |
| 42 | + coveredFunctions += parseInt(line.substring(4)) || 0; |
| 43 | + } |
| 44 | + |
| 45 | + // Branches Found and Branches Hit |
| 46 | + if (line.startsWith('BRF:')) { |
| 47 | + totalBranches += parseInt(line.substring(4)) || 0; |
| 48 | + } |
| 49 | + if (line.startsWith('BRH:')) { |
| 50 | + coveredBranches += parseInt(line.substring(4)) || 0; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + totalLines, |
| 56 | + coveredLines, |
| 57 | + totalFunctions, |
| 58 | + coveredFunctions, |
| 59 | + totalBranches, |
| 60 | + coveredBranches |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Calculate coverage percentage |
| 66 | + * @param {number} covered - Number of covered items |
| 67 | + * @param {number} total - Total number of items |
| 68 | + * @returns {number} Coverage percentage |
| 69 | + */ |
| 70 | +function calculateCoverage(covered, total) { |
| 71 | + return total > 0 ? Math.round((covered / total) * 100) : 0; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Get badge color based on coverage percentage |
| 76 | + * @param {number} coverage - Coverage percentage |
| 77 | + * @returns {string} Badge color |
| 78 | + */ |
| 79 | +function getBadgeColor(coverage) { |
| 80 | + if (coverage >= THRESHOLDS.good) return 'brightgreen'; |
| 81 | + if (coverage >= THRESHOLDS.needsImprovement) return 'yellow'; |
| 82 | + if (coverage >= THRESHOLDS.poor) return 'orange'; |
| 83 | + return 'red'; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Generate coverage report comment body |
| 88 | + * @param {object} metrics - Coverage metrics |
| 89 | + * @returns {string} Markdown formatted comment body |
| 90 | + */ |
| 91 | +function generateCoverageReport(metrics) { |
| 92 | + const lineCoverage = calculateCoverage(metrics.coveredLines, metrics.totalLines); |
| 93 | + const functionCoverage = calculateCoverage(metrics.coveredFunctions, metrics.totalFunctions); |
| 94 | + const branchCoverage = calculateCoverage(metrics.coveredBranches, metrics.totalBranches); |
| 95 | + |
| 96 | + const badgeColor = getBadgeColor(lineCoverage); |
| 97 | + const badge = ``; |
| 98 | + |
| 99 | + return `## Coverage Report\n` + |
| 100 | + `${badge}\n\n` + |
| 101 | + `| Metric | Coverage | Details |\n` + |
| 102 | + `|--------|----------|----------|\n` + |
| 103 | + `| **Lines** | ${lineCoverage}% | ${metrics.coveredLines}/${metrics.totalLines} lines |\n` + |
| 104 | + `| **Functions** | ${functionCoverage}% | ${metrics.coveredFunctions}/${metrics.totalFunctions} functions |\n` + |
| 105 | + `| **Branches** | ${branchCoverage}% | ${metrics.coveredBranches}/${metrics.totalBranches} branches |\n\n`; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Main function to post coverage comment |
| 110 | + * @param {object} github - GitHub API object |
| 111 | + * @param {object} context - GitHub Actions context |
| 112 | + */ |
| 113 | +async function postCoverageComment(github, context) { |
| 114 | + const file = 'lcov.info'; |
| 115 | + |
| 116 | + if (!fs.existsSync(file)) { |
| 117 | + console.log('Coverage file not found.'); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const content = fs.readFileSync(file, 'utf8'); |
| 122 | + const metrics = parseLcovContent(content); |
| 123 | + |
| 124 | + console.log('Coverage Metrics:'); |
| 125 | + console.log('- Lines:', metrics.coveredLines, '/', metrics.totalLines); |
| 126 | + console.log('- Functions:', metrics.coveredFunctions, '/', metrics.totalFunctions); |
| 127 | + console.log('- Branches:', metrics.coveredBranches, '/', metrics.totalBranches); |
| 128 | + |
| 129 | + const body = generateCoverageReport(metrics); |
| 130 | + |
| 131 | + await github.rest.issues.createComment({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + issue_number: context.issue.number, |
| 135 | + body: body |
| 136 | + }); |
| 137 | + |
| 138 | + console.log('Coverage comment posted successfully!'); |
| 139 | +} |
| 140 | + |
| 141 | +module.exports = { postCoverageComment }; |
| 142 | + |
0 commit comments