Skip to content

feat: badge

feat: badge #3

Workflow file for this run

name: Test Badge
on:
push:
branches:
- dev
- master
- feature/badge
jobs:
test:
name: Generate Test Badge
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 20
cache: yarn
- run: yarn install
- run: yarn run test
- name: Install xmlstarlet
run: |
sudo apt-get update
sudo apt-get install -y xmlstarlet
- name: Extract Test Counts
id: test_counts
run: |
tests=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")
failures=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")
errors=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")
echo "TESTS=$tests" >> $GITHUB_ENV
echo "FAILURES=$failures" >> $GITHUB_ENV
echo "ERRORS=$errors" >> $GITHUB_ENV
- name: Get branch name
run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
- name: Prepare Content
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const tests = "${{ env.TESTS }}";
const failures = "${{ env.FAILURES }}";
const errors = "${{ env.ERRORS }}";
let branchName = "${{ env.BRANCH_NAME }}";
branchName = branchName.replace(/\//g, '-');
const filename = `${branchName}-test-results.json`;
const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen");
const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${failures} failures, ${errors} errors","color":"${color}"}`;
fs.writeFileSync(filename, content);
- name: Commit and push
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
let branchName = "${{ env.BRANCH_NAME }}";
branchName = branchName.replace(/\//g, '-');
const filename = `${branchName}-test-results.json`;
const filePath = path.join(process.env.GITHUB_WORKSPACE, filename);
const fileContent = fs.readFileSync(filePath, 'utf8');
let sha;
try {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: filename,
ref: 'feature/use-github-actions'
});
sha = data.sha;
} catch (error) {
if (error.status !== 404) {
throw error;
}
// File does not exist, so we'll create it
}
const params = {
owner: context.repo.owner,
repo: context.repo.repo,
path: filename,
message: `Update ${filename}`,
content: Buffer.from(fileContent).toString('base64'),
branch: 'feature/use-github-actions'
};
// Only add sha to params if it is defined
if (sha) {
params.sha = sha;
}
await github.rest.repos.createOrUpdateFileContents(params);