|
| 1 | +name: Build and Test with Coverage 🧱🧪 |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + checks: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +env: |
| 14 | + DOTNET_VERSION: "9.x" |
| 15 | + |
| 16 | +jobs: |
| 17 | + # ---------------------- |
| 18 | + # 🔹 JOB BUILD |
| 19 | + # ---------------------- |
| 20 | + build: |
| 21 | + name: Build Solution 🧱 |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Setup .NET |
| 28 | + uses: actions/setup-dotnet@v4 |
| 29 | + with: |
| 30 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 31 | + |
| 32 | + - name: Restore dependencies |
| 33 | + run: dotnet restore CleanArchitecture.sln |
| 34 | + |
| 35 | + - name: Build solution |
| 36 | + run: dotnet build CleanArchitecture.sln --configuration Release --no-restore -v:detailed |
| 37 | + |
| 38 | + # ---------------------- |
| 39 | + # 🔹 JOB TEST |
| 40 | + # ---------------------- |
| 41 | + test: |
| 42 | + name: Run Tests & Coverage 🧪📊 |
| 43 | + runs-on: ubuntu-latest |
| 44 | + needs: build # dépend du build pour la séquence, mais reste dans un job séparé |
| 45 | + steps: |
| 46 | + - name: Checkout code |
| 47 | + uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: Setup .NET |
| 50 | + uses: actions/setup-dotnet@v4 |
| 51 | + with: |
| 52 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 53 | + |
| 54 | + - name: Restore dependencies |
| 55 | + run: dotnet restore CleanArchitecture.sln |
| 56 | + |
| 57 | + - name: Build solution for tests |
| 58 | + run: dotnet build CleanArchitecture.sln --configuration Release --no-restore |
| 59 | + |
| 60 | + - name: Run all tests with coverage |
| 61 | + run: | |
| 62 | + mkdir -p tests-results |
| 63 | + dotnet test CleanArchitecture.sln \ |
| 64 | + --logger "trx;LogFileName=test-results.trx" \ |
| 65 | + --collect:"XPlat Code Coverage" \ |
| 66 | + --results-directory ./tests-results \ |
| 67 | + -v:detailed |
| 68 | + continue-on-error: true |
| 69 | + |
| 70 | + - name: Publish test results |
| 71 | + uses: EnricoMi/publish-unit-test-result-action@v2 |
| 72 | + if: always() |
| 73 | + with: |
| 74 | + files: tests-results/test-results.trx |
| 75 | + |
| 76 | + - name: Install xmllint |
| 77 | + run: sudo apt-get update && sudo apt-get install -y libxml2-utils |
| 78 | + |
| 79 | + - name: Extract code coverage % |
| 80 | + id: coverage_output |
| 81 | + if: always() |
| 82 | + run: | |
| 83 | + coverage_file=$(find tests-results -name 'coverage.cobertura.xml' | head -n 1) |
| 84 | +
|
| 85 | + if [ -z "$coverage_file" ]; then |
| 86 | + echo "percentage=0" >> $GITHUB_OUTPUT |
| 87 | + echo "❌ Aucun fichier de couverture trouvé." |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + coverage=$(xmllint --xpath "string(/coverage/@line-rate)" "$coverage_file") |
| 92 | + percentage=$(awk "BEGIN { printf \"%.2f\", $coverage * 100 }") |
| 93 | + echo "📊 **Couverture du code : $percentage%**" |
| 94 | + echo "percentage=$percentage" >> $GITHUB_OUTPUT |
| 95 | +
|
| 96 | + - name: Publish coverage as check |
| 97 | + uses: actions/github-script@v7 |
| 98 | + if: always() |
| 99 | + with: |
| 100 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 101 | + script: | |
| 102 | + const percentage = "${{ steps.coverage_output.outputs.percentage }}"; |
| 103 | + const sha = context.payload.pull_request |
| 104 | + ? context.payload.pull_request.head.sha |
| 105 | + : context.sha; |
| 106 | +
|
| 107 | + await github.rest.checks.create({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + name: "Code Coverage", |
| 111 | + head_sha: sha, |
| 112 | + status: "completed", |
| 113 | + conclusion: "neutral", |
| 114 | + output: { |
| 115 | + title: "Code Coverage Result", |
| 116 | + summary: `Coverage: **${percentage}%**` |
| 117 | + } |
| 118 | + }); |
| 119 | +
|
| 120 | + - name: Comment coverage on PR |
| 121 | + if: always() |
| 122 | + uses: actions/github-script@v7 |
| 123 | + with: |
| 124 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + script: | |
| 126 | + const percentage = "${{ steps.coverage_output.outputs.percentage }}"; |
| 127 | + const pr = context.payload.pull_request?.number; |
| 128 | + if (!pr) return; |
| 129 | +
|
| 130 | + const body = `📊 **Code Coverage:** ${percentage}%`; |
| 131 | +
|
| 132 | + const comments = await github.rest.issues.listComments({ |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + issue_number: pr, |
| 136 | + }); |
| 137 | +
|
| 138 | + const botComment = comments.data.find(c => |
| 139 | + c.body?.includes("📊 **Code Coverage:**") |
| 140 | + ); |
| 141 | +
|
| 142 | + if (botComment) { |
| 143 | + await github.rest.issues.updateComment({ |
| 144 | + owner: context.repo.owner, |
| 145 | + repo: context.repo.repo, |
| 146 | + comment_id: botComment.id, |
| 147 | + body, |
| 148 | + }); |
| 149 | + } else { |
| 150 | + await github.rest.issues.createComment({ |
| 151 | + owner: context.repo.owner, |
| 152 | + repo: context.repo.repo, |
| 153 | + issue_number: pr, |
| 154 | + body, |
| 155 | + }); |
| 156 | + } |
0 commit comments