Skip to content

Commit

Permalink
feat: badge-json
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Jul 25, 2024
1 parent 35eb825 commit 0528b01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
80 changes: 43 additions & 37 deletions .github/workflows/test-badge.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: Test Badge

permissions:
contents: write

on:
push:
branches:
- dev
- master
- feature/badge

env:
BRANCH_NAME: 'feature/badge-json'

jobs:
test:
name: Generate Test Badge
Expand All @@ -25,18 +31,14 @@ jobs:
sudo apt-get update
sudo apt-get install -y xmlstarlet
- name: Extract Test Counts
id: test_counts
- name: Extract 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
echo "TESTS=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")" >> $GITHUB_ENV
echo "FAILURES=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")" >> $GITHUB_ENV
echo "ERRORS=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")" >> $GITHUB_ENV
- name: Get branch name
run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
- name: Set file name
run: echo "FILENAME=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-')-test-results.json" >> $GITHUB_ENV

- name: Prepare Content
uses: actions/github-script@v5
Expand All @@ -47,55 +49,59 @@ jobs:
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);
fs.writeFileSync("${{ env.FILENAME }}", content);
- name: Commit and push
- name: Check if file exists
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
github-token: ${{ secrets.GITHUB_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;
let fileExists = false;
try {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: filename,
ref: 'feature/use-github-actions'
path: "${{ env.FILENAME }}",
ref: "${{ env.BRANCH_NAME }}",
});
sha = data.sha;
fileExists = !!data;
} catch (error) {
if (error.status !== 404) {
throw error;
}
// File does not exist, so we'll create it
}
core.exportVariable('FILE_EXISTS', fileExists);
- name: Create or update file
uses: actions/github-script@v5
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}");
const fileContent = fs.readFileSync(filePath, 'utf8');
const params = {
owner: context.repo.owner,
repo: context.repo.repo,
path: filename,
message: `Update ${filename}`,
path: "${{ env.FILENAME }}",
message: `Update ${{ env.FILENAME }}`,
content: Buffer.from(fileContent).toString('base64'),
branch: 'feature/use-github-actions'
};
branch: "${{ env.BRANCH_NAME }}"
};
// Only add sha to params if it is defined
if (sha) {
params.sha = sha;
}
if (${{ env.FILE_EXISTS }}) {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: "${{ env.FILENAME }}",
ref: "${{ env.BRANCH_NAME }}"
});
params.sha = data.sha;
}
await github.rest.repos.createOrUpdateFileContents(params);
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
<p align="center">
<img alt="Node" src="https://img.shields.io/badge/node->=10.9.0-brightgreen">
<img alt="NPM" src="https://img.shields.io/npm/l/aelf-command">
<a href="http://commitizen.github.io/cz-cli/">
<img alt="Commitizen friendly" src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg">
</a>
<img alt="TESTS" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/eanzhao/aelf-command/feature/use-github-actions/master-test-results.json">
<a href="http://commitizen.github.io/cz-cli/"><img alt="Commitizen friendly" src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg"></a>
<img alt="TESTS" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/eanzhao/aelf-command/feature/badge-json/master-test-results.json">
</p>

## Descriptions
Expand Down

0 comments on commit 0528b01

Please sign in to comment.