feat: git action #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test Badge | |
on: | |
push: | |
branches: | |
- feature/use-github-actions | |
jobs: | |
test: | |
name: Coverage Diff | |
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 | |
} | |
await github.rest.repos.createOrUpdateFileContents({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
path: filename, | |
message: `Update ${filename}`, | |
content: Buffer.from(fileContent).toString('base64'), | |
sha, | |
branch: 'feature/use-github-actions' | |
}); |