[feat] jacoco 커밋메시지 추가 #197
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI | |
| on: | |
| pull_request: | |
| branches: [ main, dev ] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| jobs: | |
| build-and-test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| cache: 'gradle' | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| # ✅ Spotless 검사 | |
| - name: Run Spotless Check | |
| run: ./gradlew spotlessCheck | |
| - name: Build and Test | |
| run: ./gradlew build | |
| - name: JaCoCo 커버리지 PR 코멘트 | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const xml = fs.readFileSync('build/reports/jacoco/test/jacocoTestReport.xml', 'utf8'); | |
| const pct = (covered, missed) => { | |
| const total = covered + missed; | |
| if (total === 0) return -1; | |
| return parseFloat(((covered / total) * 100).toFixed(1)); | |
| }; | |
| // Overall 커버리지: <report> 바로 아래 <counter> 태그에서 추출 | |
| const reportCounters = [...xml.matchAll(/<\/package>\s*([\s\S]*?)<\/report>/g)][0]?.[1] || ''; | |
| const overallLine = reportCounters.match(/<counter type="LINE" missed="(\d+)" covered="(\d+)"/); | |
| const overallBranch = reportCounters.match(/<counter type="BRANCH" missed="(\d+)" covered="(\d+)"/); | |
| const overallLinePct = overallLine ? pct(parseInt(overallLine[2]), parseInt(overallLine[1])) : -1; | |
| const overallBranchPct = overallBranch ? pct(parseInt(overallBranch[2]), parseInt(overallBranch[1])) : -1; | |
| const overallEmoji = overallLinePct >= 80 ? '🟢' : overallLinePct >= 50 ? '🟡' : '🔴'; | |
| // 패키지별 커버리지 | |
| const packageMatches = [...xml.matchAll(/<package name="([^"]+)"[\s\S]*?<\/package>/g)]; | |
| const packages = packageMatches.map(match => { | |
| const parts = match[1].split('/'); | |
| const pkg = parts.slice(-2).join('/'); | |
| const counters = [...match[0].matchAll(/<counter type="([^"]+)" missed="(\d+)" covered="(\d+)"/g)]; | |
| const lineCounter = counters.find(c => c[1] === 'LINE'); | |
| const branchCounter = counters.find(c => c[1] === 'BRANCH'); | |
| const linePct = lineCounter ? pct(parseInt(lineCounter[3]), parseInt(lineCounter[2])) : -1; | |
| const branchPct = branchCounter ? pct(parseInt(branchCounter[3]), parseInt(branchCounter[2])) : -1; | |
| return { pkg, linePct, branchPct }; | |
| }); | |
| packages.sort((a, b) => b.linePct - a.linePct); | |
| const rows = packages.map(({ pkg, linePct, branchPct }) => { | |
| const emoji = linePct >= 80 ? '🟢' : linePct >= 50 ? '🟡' : '🔴'; | |
| return `| ${emoji} \`${pkg}\` | ${linePct < 0 ? 'N/A' : linePct + '%'} | ${branchPct < 0 ? 'N/A' : branchPct + '%'} |`; | |
| }); | |
| const body = [ | |
| '## 📊 테스트 커버리지 리포트', | |
| '', | |
| `### Overall: ${overallEmoji} Line \`${overallLinePct < 0 ? 'N/A' : overallLinePct + '%'}\` | Branch \`${overallBranchPct < 0 ? 'N/A' : overallBranchPct + '%'}\``, | |
| '', | |
| '---', | |
| '', | |
| '### 📦 패키지별 커버리지', | |
| '', | |
| '| 패키지 | Line | Branch |', | |
| '|--------|------|--------|', | |
| ...rows, | |
| '', | |
| '> 🟢 80% 이상 🟡 50~79% 🔴 50% 미만', | |
| ].join('\n'); | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const existing = comments.find(c => | |
| c.user.login === 'github-actions[bot]' && | |
| c.body.includes('테스트 커버리지 리포트') | |
| ); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existing.id, | |
| body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body, | |
| }); | |
| } | |
| - name: Discord 알림 (CI 실패) | |
| if: failure() | |
| run: | | |
| curl -s -H "Content-Type: application/json" \ | |
| -d "{\"embeds\":[{\"title\":\"CI 빌드 실패 ❌\",\"description\":\"**브랜치**: ${{ github.head_ref }}\\n**PR**: ${{ github.event.pull_request.title }}\\n**커밋**: ${{ github.sha }}\",\"color\":15158332}]}" \ | |
| ${{ secrets.DISCORD_WEBHOOK_URL }} |