|
| 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 | + * @param {object} commitInfo - Optional commit information |
| 90 | + * @returns {string} Markdown formatted comment body |
| 91 | + */ |
| 92 | +function generateCoverageReport(metrics, commitInfo = {}) { |
| 93 | + const lineCoverage = calculateCoverage(metrics.coveredLines, metrics.totalLines); |
| 94 | + const functionCoverage = calculateCoverage(metrics.coveredFunctions, metrics.totalFunctions); |
| 95 | + const branchCoverage = calculateCoverage(metrics.coveredBranches, metrics.totalBranches); |
| 96 | + |
| 97 | + const badgeColor = getBadgeColor(lineCoverage); |
| 98 | + const badge = ``; |
| 99 | + |
| 100 | + // Generate timestamp |
| 101 | + const timestamp = new Date().toUTCString(); |
| 102 | + |
| 103 | + // Build commit link if info is available |
| 104 | + let commitLink = ''; |
| 105 | + if (commitInfo.sha && commitInfo.owner && commitInfo.repo) { |
| 106 | + const shortSha = commitInfo.sha.substring(0, 7); |
| 107 | + commitLink = ` for commit [\`${shortSha}\`](https://github.com/${commitInfo.owner}/${commitInfo.repo}/commit/${commitInfo.sha})`; |
| 108 | + } |
| 109 | + |
| 110 | + return `## Coverage Report\n` + |
| 111 | + `${badge}\n\n` + |
| 112 | + `| Metric | Coverage | Details |\n` + |
| 113 | + `|--------|----------|----------|\n` + |
| 114 | + `| **Lines** | ${lineCoverage}% | ${metrics.coveredLines}/${metrics.totalLines} lines |\n` + |
| 115 | + `| **Functions** | ${functionCoverage}% | ${metrics.coveredFunctions}/${metrics.totalFunctions} functions |\n` + |
| 116 | + `| **Branches** | ${branchCoverage}% | ${metrics.coveredBranches}/${metrics.totalBranches} branches |\n\n` + |
| 117 | + `*Last updated: ${timestamp}*${commitLink}\n`; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Main function to post coverage comment |
| 122 | + * @param {object} github - GitHub API object |
| 123 | + * @param {object} context - GitHub Actions context |
| 124 | + */ |
| 125 | +async function postCoverageComment(github, context) { |
| 126 | + const file = 'lcov.info'; |
| 127 | + |
| 128 | + if (!fs.existsSync(file)) { |
| 129 | + console.log('Coverage file not found.'); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + const content = fs.readFileSync(file, 'utf8'); |
| 134 | + const metrics = parseLcovContent(content); |
| 135 | + |
| 136 | + console.log('Coverage Metrics:'); |
| 137 | + console.log('- Lines:', metrics.coveredLines, '/', metrics.totalLines); |
| 138 | + console.log('- Functions:', metrics.coveredFunctions, '/', metrics.totalFunctions); |
| 139 | + console.log('- Branches:', metrics.coveredBranches, '/', metrics.totalBranches); |
| 140 | + |
| 141 | + const body = generateCoverageReport(metrics); |
| 142 | + |
| 143 | + await github.rest.issues.createComment({ |
| 144 | + owner: context.repo.owner, |
| 145 | + repo: context.repo.repo, |
| 146 | + issue_number: context.issue.number, |
| 147 | + body: body |
| 148 | + }); |
| 149 | + |
| 150 | + console.log('Coverage comment posted successfully!'); |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Generate coverage report and save to file (for workflow artifacts) |
| 155 | + */ |
| 156 | +function generateCoverageFile() { |
| 157 | + const file = 'lcov.info'; |
| 158 | + |
| 159 | + if (!fs.existsSync(file)) { |
| 160 | + console.log('Coverage file not found.'); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + const content = fs.readFileSync(file, 'utf8'); |
| 165 | + const metrics = parseLcovContent(content); |
| 166 | + |
| 167 | + console.log('Coverage Metrics:'); |
| 168 | + console.log('- Lines:', metrics.coveredLines, '/', metrics.totalLines); |
| 169 | + console.log('- Functions:', metrics.coveredFunctions, '/', metrics.totalFunctions); |
| 170 | + console.log('- Branches:', metrics.coveredBranches, '/', metrics.totalBranches); |
| 171 | + |
| 172 | + // Get commit info from environment variables |
| 173 | + const commitInfo = { |
| 174 | + sha: process.env.COMMIT_SHA, |
| 175 | + owner: process.env.REPO_OWNER, |
| 176 | + repo: process.env.REPO_NAME |
| 177 | + }; |
| 178 | + |
| 179 | + const body = generateCoverageReport(metrics, commitInfo); |
| 180 | + fs.writeFileSync('coverage-report.md', body); |
| 181 | + console.log('Coverage report saved to coverage-report.md'); |
| 182 | +} |
| 183 | + |
| 184 | +// If run directly (not as module), generate the file |
| 185 | +if (require.main === module) { |
| 186 | + generateCoverageFile(); |
| 187 | +} |
| 188 | + |
| 189 | +module.exports = { postCoverageComment, generateCoverageFile }; |
| 190 | + |
0 commit comments