Skip to content

Commit

Permalink
Merge pull request #79 from AElfProject/feature/badge
Browse files Browse the repository at this point in the history
Feature/badge
  • Loading branch information
hzz780 authored Jul 26, 2024
2 parents 3478e3b + 2aa0332 commit bc17b9a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
88 changes: 55 additions & 33 deletions .github/workflows/test-badge.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
name: Test Badge

permissions:
contents: write

on:
push:
branches:
- dev
- master

env:
BRANCH_NAME: 'feature/badge-json'

jobs:
test:
name: Coverage Diff
name: Generate Test Badge
runs-on: ubuntu-latest

steps:
Expand All @@ -23,18 +30,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 @@ -45,40 +48,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: Check if file exists
uses: actions/github-script@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let fileExists = false;
try {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: "${{ env.FILENAME }}",
ref: "${{ env.BRANCH_NAME }}",
});
fileExists = !!data;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
core.exportVariable('FILE_EXISTS', fileExists);
- name: Commit and push
- 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');
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 filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}");
const fileContent = fs.readFileSync(filePath, 'utf8');
const { data: { sha } } = await github.rest.repos.getContent({
const params = {
owner: context.repo.owner,
repo: context.repo.repo,
path: filename,
ref: 'feature/use-github-actions'
});
await github.rest.repos.createOrUpdateFileContents({
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'),
sha,
branch: 'feature/use-github-actions'
});
branch: "${{ env.BRANCH_NAME }}"
};
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);
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +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>
<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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-command",
"version": "0.1.47-beta.7",
"version": "0.1.47-beta.8",
"description": "A CLI tools for AElf",
"main": "src/index.js",
"type": "module",
Expand Down

0 comments on commit bc17b9a

Please sign in to comment.