|
| 1 | +name: Test Badge |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +jobs: |
| 9 | + test: |
| 10 | + name: Coverage Diff |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - uses: actions/checkout@v2 |
| 15 | + - uses: actions/setup-node@v2 |
| 16 | + with: |
| 17 | + node-version: 20 |
| 18 | + cache: yarn |
| 19 | + - run: yarn install |
| 20 | + - run: yarn run test |
| 21 | + - name: Install xmlstarlet |
| 22 | + run: | |
| 23 | + sudo apt-get update |
| 24 | + sudo apt-get install -y xmlstarlet |
| 25 | +
|
| 26 | + - name: Extract Test Counts |
| 27 | + id: test_counts |
| 28 | + run: | |
| 29 | + tests=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml") |
| 30 | + failures=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml") |
| 31 | + errors=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml") |
| 32 | + echo "TESTS=$tests" >> $GITHUB_ENV |
| 33 | + echo "FAILURES=$failures" >> $GITHUB_ENV |
| 34 | + echo "ERRORS=$errors" >> $GITHUB_ENV |
| 35 | +
|
| 36 | + - name: Get branch name |
| 37 | + run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV |
| 38 | + |
| 39 | + - name: Prepare Content |
| 40 | + uses: actions/github-script@v5 |
| 41 | + with: |
| 42 | + github-token: ${{ secrets.COMMIT_TOKEN }} |
| 43 | + script: | |
| 44 | + const fs = require('fs'); |
| 45 | + const tests = "${{ env.TESTS }}"; |
| 46 | + const failures = "${{ env.FAILURES }}"; |
| 47 | + const errors = "${{ env.ERRORS }}"; |
| 48 | + let branchName = "${{ env.BRANCH_NAME }}"; |
| 49 | + branchName = branchName.replace(/\//g, '-'); |
| 50 | + const filename = `${branchName}-test-results.json`; |
| 51 | + const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen"); |
| 52 | + const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${failures} failures, ${errors} errors","color":"${color}"}`; |
| 53 | +
|
| 54 | + fs.writeFileSync(filename, content); |
| 55 | +
|
| 56 | + - name: Commit and push |
| 57 | + uses: actions/github-script@v5 |
| 58 | + with: |
| 59 | + github-token: ${{ secrets.COMMIT_TOKEN }} |
| 60 | + script: | |
| 61 | + const fs = require('fs'); |
| 62 | + const path = require('path'); |
| 63 | + let branchName = "${{ env.BRANCH_NAME }}"; |
| 64 | + branchName = branchName.replace(/\//g, '-'); |
| 65 | + const filename = `${branchName}-test-results.json`; |
| 66 | + const filePath = path.join(process.env.GITHUB_WORKSPACE, filename); |
| 67 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 68 | +
|
| 69 | + const { data: { sha } } = await github.rest.repos.getContent({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + path: filename, |
| 73 | + ref: 'feature/use-github-actions' |
| 74 | + }); |
| 75 | +
|
| 76 | + await github.rest.repos.createOrUpdateFileContents({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + path: filename, |
| 80 | + message: `Update ${filename}`, |
| 81 | + content: Buffer.from(fileContent).toString('base64'), |
| 82 | + sha, |
| 83 | + branch: 'feature/use-github-actions' |
| 84 | + }); |
0 commit comments