|
| 1 | +name: 'Code Coverage' |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + type: |
| 6 | + description: Type of report (unit or integration) |
| 7 | + type: string |
| 8 | + resource_name: |
| 9 | + description: Resource name of coverage report |
| 10 | + type: string |
| 11 | + |
| 12 | +jobs: |
| 13 | + coverage-unit: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + name: Unit tests coverage |
| 16 | + if: ${{ inputs.type == 'unit' }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Download Coverage Report |
| 21 | + uses: actions/download-artifact@v4 |
| 22 | + with: |
| 23 | + name: ${{ inputs.resource_name }} |
| 24 | + path: report |
| 25 | + |
| 26 | + - uses: jwalton/gh-find-current-pr@v1 |
| 27 | + id: findPr |
| 28 | + |
| 29 | + - uses: ArtiomTr/jest-coverage-report-action@v2 |
| 30 | + with: |
| 31 | + prnumber: ${{ steps.findPr.outputs.number }} |
| 32 | + coverage-file: report/coverage/report.json |
| 33 | + base-coverage-file: report/coverage/report.json |
| 34 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + skip-step: all |
| 36 | + custom-title: Code Coverage - ${{ inputs.resource_name == 'report-be' && 'Backend' || 'Frontend' }} unit tests |
| 37 | + |
| 38 | + coverage-integration: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + name: Integration tests coverage |
| 41 | + if: ${{ inputs.type == 'integration' }} |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v4 |
| 44 | + |
| 45 | + - name: Download Coverage Report |
| 46 | + uses: actions/download-artifact@v4 |
| 47 | + with: |
| 48 | + name: ${{ inputs.resource_name }} |
| 49 | + |
| 50 | + - name: Parse Coverage Summary |
| 51 | + id: parse-coverage |
| 52 | + run: | |
| 53 | + # Extract coverage data from file. |
| 54 | + # Example of processed row: |
| 55 | + # Statements : 81.75% ( 16130/19730 ) |
| 56 | + # field '$3' = 81.75%, field '$5' = 16130 |
| 57 | + extract_coverage_data() { |
| 58 | + local keyword=$1 |
| 59 | + local field=$2 |
| 60 | + awk "/$keyword/ {print $field}" integration-coverage.txt | tr -d '\n|%' |
| 61 | + } |
| 62 | +
|
| 63 | + # Determine status based on percentage |
| 64 | + get_status() { |
| 65 | + if [ "$(echo "$1 < 50" | bc)" -eq 1 ]; then |
| 66 | + echo "🔴" |
| 67 | + elif [ "$(echo "$1 < 80" | bc)" -eq 1 ]; then |
| 68 | + echo "🟡" |
| 69 | + else |
| 70 | + echo "🟢" |
| 71 | + fi |
| 72 | + } |
| 73 | +
|
| 74 | + # Extract coverage data from the summary |
| 75 | + STATEMENTS_PERCENT=$(extract_coverage_data "Statements" '$3') |
| 76 | + STATEMENTS_COVERED=$(extract_coverage_data "Statements" '$5') |
| 77 | + STATEMENTS_STATUS=$(get_status $STATEMENTS_PERCENT) |
| 78 | +
|
| 79 | + BRANCHES_PERCENT=$(extract_coverage_data "Branches" '$3') |
| 80 | + BRANCHES_COVERED=$(extract_coverage_data "Branches" '$5') |
| 81 | + BRANCHES_STATUS=$(get_status $BRANCHES_PERCENT) |
| 82 | +
|
| 83 | + FUNCTIONS_PERCENT=$(extract_coverage_data "Functions" '$3') |
| 84 | + FUNCTIONS_COVERED=$(extract_coverage_data "Functions" '$5') |
| 85 | + FUNCTIONS_STATUS=$(get_status $FUNCTIONS_PERCENT) |
| 86 | +
|
| 87 | + LINES_PERCENT=$(extract_coverage_data "Lines" '$3') |
| 88 | + LINES_COVERED=$(extract_coverage_data "Lines" '$5') |
| 89 | + LINES_STATUS=$(get_status $LINES_PERCENT) |
| 90 | +
|
| 91 | + # Format as a Markdown table |
| 92 | + echo "| Status | Category | Percentage | Covered / Total |" > coverage-table.md |
| 93 | + echo "|-------------|-------------|-------------|-----------------|" >> coverage-table.md |
| 94 | + echo "| $STATEMENTS_STATUS | Statements | ${STATEMENTS_PERCENT}% | ${STATEMENTS_COVERED} |" >> coverage-table.md |
| 95 | + echo "| $BRANCHES_STATUS | Branches | ${BRANCHES_PERCENT}% | ${BRANCHES_COVERED} |" >> coverage-table.md |
| 96 | + echo "| $FUNCTIONS_STATUS | Functions | ${FUNCTIONS_PERCENT}% | ${FUNCTIONS_COVERED} |" >> coverage-table.md |
| 97 | + echo "| $LINES_STATUS | Lines | ${LINES_PERCENT}% | ${LINES_COVERED} |" >> coverage-table.md |
| 98 | +
|
| 99 | + - uses: jwalton/gh-find-current-pr@v1 |
| 100 | + id: findPr |
| 101 | + |
| 102 | + - name: Post or Update Coverage Summary Comment |
| 103 | + uses: actions/github-script@v7 |
| 104 | + with: |
| 105 | + script: | |
| 106 | + const fs = require('fs'); |
| 107 | + const table = fs.readFileSync('coverage-table.md', 'utf8'); |
| 108 | + const commentBody = `### Code Coverage - Integration Tests\n\n${table}`; |
| 109 | +
|
| 110 | + // Fetch existing comments on the pull request |
| 111 | + const { data: comments } = await github.rest.issues.listComments({ |
| 112 | + owner: context.repo.owner, |
| 113 | + repo: context.repo.repo, |
| 114 | + issue_number: process.env.RR_Number, |
| 115 | + }); |
| 116 | +
|
| 117 | + // Check if a comment with the same header already exists |
| 118 | + const existingComment = comments.find(comment => |
| 119 | + comment.body.startsWith('### Code Coverage - Integration Tests') |
| 120 | + ); |
| 121 | +
|
| 122 | + if (existingComment) { |
| 123 | + // Update the existing comment |
| 124 | + await github.rest.issues.updateComment({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + comment_id: existingComment.id, |
| 128 | + body: commentBody, |
| 129 | + }); |
| 130 | + } else { |
| 131 | + // Create a new comment |
| 132 | + await github.rest.issues.createComment({ |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + issue_number: process.env.RR_Number, |
| 136 | + body: commentBody, |
| 137 | + }); |
| 138 | + } |
| 139 | + env: |
| 140 | + RR_Number: ${{ steps.findPr.outputs.number }} |
0 commit comments