|
| 1 | +const core = require('@actions/core'); |
| 2 | + |
| 3 | +try { |
| 4 | + // 점수 지표 파일 정보 |
| 5 | + const fs = require('fs'); |
| 6 | + const results = JSON.parse(fs.readFileSync('./lhci_reports/manifest.json')); |
| 7 | + const totalReports = results.length; |
| 8 | + |
| 9 | + // LightHouse 점수 지표 |
| 10 | + const averageScores = { |
| 11 | + performance: 0, |
| 12 | + accessibility: 0, |
| 13 | + 'best-practices': 0, |
| 14 | + seo: 0, |
| 15 | + pwa: 0, |
| 16 | + }; |
| 17 | + const auditSummaries = { |
| 18 | + 'first-contentful-paint': 0, |
| 19 | + 'largest-contentful-paint': 0, |
| 20 | + interactive: 0, |
| 21 | + 'total-blocking-time': 0, |
| 22 | + 'cumulative-layout-shift': 0, |
| 23 | + }; |
| 24 | + |
| 25 | + // 점수 평균 |
| 26 | + results.forEach(result => { |
| 27 | + const { summary } = result; |
| 28 | + |
| 29 | + for (const key in averageScores) { |
| 30 | + averageScores[key] += summary[key]; |
| 31 | + } |
| 32 | + |
| 33 | + const details = JSON.parse(fs.readFileSync(result.jsonPath)); |
| 34 | + [ |
| 35 | + 'first-contentful-paint', |
| 36 | + 'largest-contentful-paint', |
| 37 | + 'interactive', |
| 38 | + 'total-blocking-time', |
| 39 | + 'cumulative-layout-shift', |
| 40 | + ].forEach(auditName => { |
| 41 | + if (details.audits[auditName]) { |
| 42 | + const auditDetails = details.audits[auditName]; |
| 43 | + auditSummaries[auditName] += parseFloat(auditDetails.displayValue) || 0; |
| 44 | + } |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + // 점수 색상 표시 |
| 49 | + const formatScore = res => (res >= 90 ? '🟢' : res >= 70 ? '🟠' : '🔴'); |
| 50 | + |
| 51 | + // 상세 지표 점수 색상 표시 |
| 52 | + const detailScore = (value, metric) => { |
| 53 | + switch (metric) { |
| 54 | + case 'first-contentful-paint': |
| 55 | + return value <= 1.8 ? '🟢' : value <= 3 ? '🟠' : '🔴'; |
| 56 | + case 'largest-contentful-paint': |
| 57 | + return value <= 2.5 ? '🟢' : value <= 4 ? '🟠' : '🔴'; |
| 58 | + case 'interactive': |
| 59 | + return value <= 3.8 ? '🟢' : value <= 7.3 ? '🟠' : '🔴'; |
| 60 | + case 'total-blocking-time': |
| 61 | + return value <= 300 ? '🟢' : value <= 600 ? '🟠' : '🔴'; |
| 62 | + case 'cumulative-layout-shift': |
| 63 | + return value <= 0.1 ? '🟢' : value <= 0.25 ? '🟠' : '🔴'; |
| 64 | + default: |
| 65 | + return '🔴'; // Default to red if metric is unknown |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // comments 파싱 |
| 70 | + let comments = |
| 71 | + '⚡️ Lighthouse Average Scores Across Reports:\n| Category | Score |\n| --- | --- |\n'; |
| 72 | + Object.keys(averageScores).forEach(key => { |
| 73 | + const avgScore = Math.round((averageScores[key] / totalReports) * 100); |
| 74 | + comments += `| ${formatScore(avgScore)} ${key.replace( |
| 75 | + /-/g, |
| 76 | + ' ' |
| 77 | + )} | ${avgScore} |\n`; |
| 78 | + }); |
| 79 | + |
| 80 | + comments += |
| 81 | + '\n⚡️ Average Details Across All Reports:\n| Category | Score |\n| --- | --- |\n'; |
| 82 | + Object.keys(auditSummaries).forEach(auditName => { |
| 83 | + const average = auditSummaries[auditName] / totalReports; |
| 84 | + const formattedName = auditName.replace(/-/g, ' '); |
| 85 | + const colorCode = detailScore(average, auditName); |
| 86 | + const unit = |
| 87 | + auditName === 'total-blocking-time' |
| 88 | + ? 'ms' |
| 89 | + : auditName === 'cumulative-layout-shift' |
| 90 | + ? '' |
| 91 | + : 's'; |
| 92 | + comments += `| ${colorCode} ${formattedName} | ${average.toFixed( |
| 93 | + 1 |
| 94 | + )}${unit} |\n`; |
| 95 | + }); |
| 96 | + |
| 97 | + // comments 내보내기 |
| 98 | + core.setOutput('comments', comments); |
| 99 | +} catch (error) { |
| 100 | + console.error(error); |
| 101 | +} |
0 commit comments