|
| 1 | +name: Pull Request |
| 2 | + |
| 3 | +# Note: All actions are pinned to full-length commit SHAs for security compliance |
| 4 | +# To verify/update commit SHAs, visit each action's GitHub repository and check the Releases page |
| 5 | +# Example: https://github.com/actions/checkout/releases/tag/v4 |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + - develop |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + packages: read |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +env: |
| 19 | + DOTNET_VERSION: '8.0.x' |
| 20 | + |
| 21 | +jobs: |
| 22 | + lint: |
| 23 | + name: Lint Code |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 28 | + |
| 29 | + - name: Setup .NET |
| 30 | + uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 |
| 31 | + with: |
| 32 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 33 | + |
| 34 | + - name: Restore dependencies |
| 35 | + run: dotnet restore |
| 36 | + |
| 37 | + - name: Format check |
| 38 | + run: dotnet format --verify-no-changes --verbosity diagnostic |
| 39 | + |
| 40 | + unit-tests: |
| 41 | + name: Unit Tests |
| 42 | + runs-on: ubuntu-latest |
| 43 | + needs: lint |
| 44 | + steps: |
| 45 | + - name: Checkout code |
| 46 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 47 | + |
| 48 | + - name: Setup .NET |
| 49 | + uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 |
| 50 | + with: |
| 51 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 52 | + |
| 53 | + - name: Restore dependencies |
| 54 | + run: dotnet restore |
| 55 | + |
| 56 | + - name: Run unit tests |
| 57 | + run: | |
| 58 | + dotnet test \ |
| 59 | + --filter "Category=Unit" \ |
| 60 | + --results-directory ./test-results \ |
| 61 | + --logger "trx;LogFileName=unit-tests.trx" \ |
| 62 | + --logger "console;verbosity=detailed" |
| 63 | +
|
| 64 | + - name: Upload unit test results |
| 65 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 |
| 66 | + if: always() |
| 67 | + with: |
| 68 | + name: unit-test-results |
| 69 | + path: ./test-results/**/*.trx |
| 70 | + |
| 71 | + component-tests: |
| 72 | + name: Component Tests |
| 73 | + runs-on: ubuntu-latest |
| 74 | + needs: lint |
| 75 | + steps: |
| 76 | + - name: Checkout code |
| 77 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 78 | + |
| 79 | + - name: Setup .NET |
| 80 | + uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 |
| 81 | + with: |
| 82 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 83 | + |
| 84 | + - name: Restore dependencies |
| 85 | + run: dotnet restore |
| 86 | + |
| 87 | + - name: Run component tests |
| 88 | + run: | |
| 89 | + dotnet test \ |
| 90 | + --filter "Category=Component" \ |
| 91 | + --results-directory ./test-results \ |
| 92 | + --logger "trx;LogFileName=component-tests.trx" \ |
| 93 | + --logger "console;verbosity=detailed" |
| 94 | +
|
| 95 | + - name: Upload component test results |
| 96 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 |
| 97 | + if: always() |
| 98 | + with: |
| 99 | + name: component-test-results |
| 100 | + path: ./test-results/**/*.trx |
| 101 | + |
| 102 | + integration-tests: |
| 103 | + name: Integration Tests |
| 104 | + runs-on: ubuntu-latest |
| 105 | + needs: [unit-tests, component-tests] |
| 106 | + steps: |
| 107 | + - name: Checkout code |
| 108 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 109 | + |
| 110 | + - name: Setup .NET |
| 111 | + uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 |
| 112 | + with: |
| 113 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 114 | + |
| 115 | + - name: Restore dependencies |
| 116 | + run: dotnet restore |
| 117 | + |
| 118 | + - name: Run integration tests |
| 119 | + run: | |
| 120 | + dotnet test \ |
| 121 | + --filter "Category=Integration" \ |
| 122 | + --results-directory ./test-results \ |
| 123 | + --logger "trx;LogFileName=integration-tests.trx" \ |
| 124 | + --logger "console;verbosity=detailed" |
| 125 | +
|
| 126 | + - name: Upload integration test results |
| 127 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 |
| 128 | + if: always() |
| 129 | + with: |
| 130 | + name: integration-test-results |
| 131 | + path: ./test-results/**/*.trx |
| 132 | + |
| 133 | + contract-tests: |
| 134 | + name: Contract Tests |
| 135 | + runs-on: ubuntu-latest |
| 136 | + needs: [unit-tests, component-tests] |
| 137 | + steps: |
| 138 | + - name: Checkout code |
| 139 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 140 | + |
| 141 | + - name: Setup .NET |
| 142 | + uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 |
| 143 | + with: |
| 144 | + dotnet-version: ${{ env.DOTNET_VERSION }} |
| 145 | + |
| 146 | + - name: Restore dependencies |
| 147 | + run: dotnet restore |
| 148 | + |
| 149 | + - name: Run contract tests |
| 150 | + run: | |
| 151 | + dotnet test \ |
| 152 | + --filter "Category=Contract" \ |
| 153 | + --results-directory ./test-results \ |
| 154 | + --logger "trx;LogFileName=contract-tests.trx" \ |
| 155 | + --logger "console;verbosity=detailed" |
| 156 | +
|
| 157 | + - name: Upload contract test results |
| 158 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 |
| 159 | + if: always() |
| 160 | + with: |
| 161 | + name: contract-test-results |
| 162 | + path: ./test-results/**/*.trx |
| 163 | + |
| 164 | + status-comment: |
| 165 | + name: PR Status Comment |
| 166 | + runs-on: ubuntu-latest |
| 167 | + needs: [lint, unit-tests, component-tests, integration-tests, contract-tests] |
| 168 | + if: always() && github.event_name == 'pull_request' |
| 169 | + steps: |
| 170 | + - name: Checkout code |
| 171 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 |
| 172 | + |
| 173 | + - name: Download all test results |
| 174 | + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 |
| 175 | + continue-on-error: true |
| 176 | + with: |
| 177 | + path: ./test-results |
| 178 | + |
| 179 | + - name: Parse test results |
| 180 | + id: test-results |
| 181 | + run: | |
| 182 | + set -e |
| 183 | + |
| 184 | + # Initialize counters |
| 185 | + TOTAL_TESTS=0 |
| 186 | + PASSED_TESTS=0 |
| 187 | + FAILED_TESTS=0 |
| 188 | + |
| 189 | + # Find and parse all TRX files |
| 190 | + for trx in $(find ./test-results -name "*.trx" 2>/dev/null || true); do |
| 191 | + if [ -f "$trx" ]; then |
| 192 | + TOTAL=$(grep -oP '(?<=total=")[0-9]+' "$trx" || echo "0") |
| 193 | + PASSED=$(grep -oP '(?<=passed=")[0-9]+' "$trx" || echo "0") |
| 194 | + FAILED=$(grep -oP '(?<=failed=")[0-9]+' "$trx" || echo "0") |
| 195 | + TOTAL_TESTS=$((TOTAL_TESTS + TOTAL)) |
| 196 | + PASSED_TESTS=$((PASSED_TESTS + PASSED)) |
| 197 | + FAILED_TESTS=$((FAILED_TESTS + FAILED)) |
| 198 | + fi |
| 199 | + done |
| 200 | + |
| 201 | + echo "total=$TOTAL_TESTS" >> $GITHUB_OUTPUT |
| 202 | + echo "passed=$PASSED_TESTS" >> $GITHUB_OUTPUT |
| 203 | + echo "failed=$FAILED_TESTS" >> $GITHUB_OUTPUT |
| 204 | + |
| 205 | + # Determine overall status |
| 206 | + if [ "${{ needs.lint.result }}" == "failure" ]; then |
| 207 | + echo "status=❌ Linting failed" >> $GITHUB_OUTPUT |
| 208 | + elif [ "${{ needs.unit-tests.result }}" == "failure" ] || \ |
| 209 | + [ "${{ needs.component-tests.result }}" == "failure" ] || \ |
| 210 | + [ "${{ needs.integration-tests.result }}" == "failure" ] || \ |
| 211 | + [ "${{ needs.contract-tests.result }}" == "failure" ]; then |
| 212 | + echo "status=❌ Tests failed" >> $GITHUB_OUTPUT |
| 213 | + else |
| 214 | + echo "status=✅ All checks passed" >> $GITHUB_OUTPUT |
| 215 | + fi |
| 216 | +
|
| 217 | + - name: Create PR comment |
| 218 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 219 | + with: |
| 220 | + script: | |
| 221 | + const status = '${{ steps.test-results.outputs.status }}'; |
| 222 | + const total = '${{ steps.test-results.outputs.total }}'; |
| 223 | + const passed = '${{ steps.test-results.outputs.passed }}'; |
| 224 | + const failed = '${{ steps.test-results.outputs.failed }}'; |
| 225 | + |
| 226 | + const lintStatus = '${{ needs.lint.result }}' === 'success' ? '✅' : '❌'; |
| 227 | + const unitStatus = '${{ needs.unit-tests.result }}' === 'success' ? '✅' : '❌'; |
| 228 | + const componentStatus = '${{ needs.component-tests.result }}' === 'success' ? '✅' : '❌'; |
| 229 | + const integrationStatus = '${{ needs.integration-tests.result }}' === 'success' ? '✅' : '❌'; |
| 230 | + const contractStatus = '${{ needs.contract-tests.result }}' === 'success' ? '✅' : '❌'; |
| 231 | + |
| 232 | + const body = `## 🔍 Pull Request CI Status |
| 233 | + |
| 234 | + ${status} |
| 235 | + |
| 236 | + ### Test Results Summary |
| 237 | + - **Total Tests:** ${total} |
| 238 | + - **Passed:** ${passed} |
| 239 | + - **Failed:** ${failed} |
| 240 | + |
| 241 | + ### Job Status |
| 242 | + | Job | Status | |
| 243 | + |-----|--------| |
| 244 | + | Linting | ${lintStatus} | |
| 245 | + | Unit Tests | ${unitStatus} | |
| 246 | + | Component Tests | ${componentStatus} | |
| 247 | + | Integration Tests | ${integrationStatus} | |
| 248 | + | Contract Tests | ${contractStatus} | |
| 249 | + |
| 250 | + --- |
| 251 | + *This comment is automatically updated on each workflow run.* |
| 252 | + `; |
| 253 | + |
| 254 | + // Find existing comment |
| 255 | + const comments = await github.rest.issues.listComments({ |
| 256 | + owner: context.repo.owner, |
| 257 | + repo: context.repo.repo, |
| 258 | + issue_number: context.issue.number, |
| 259 | + }); |
| 260 | + |
| 261 | + const botComment = comments.data.find(comment => |
| 262 | + comment.user.type === 'Bot' && |
| 263 | + comment.body.includes('Pull Request CI Status') |
| 264 | + ); |
| 265 | + |
| 266 | + if (botComment) { |
| 267 | + await github.rest.issues.updateComment({ |
| 268 | + owner: context.repo.owner, |
| 269 | + repo: context.repo.repo, |
| 270 | + comment_id: botComment.id, |
| 271 | + body: body, |
| 272 | + }); |
| 273 | + } else { |
| 274 | + await github.rest.issues.createComment({ |
| 275 | + owner: context.repo.owner, |
| 276 | + repo: context.repo.repo, |
| 277 | + issue_number: context.issue.number, |
| 278 | + body: body, |
| 279 | + }); |
| 280 | + } |
| 281 | +
|
0 commit comments