Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add github badge #182

Open
wants to merge 4 commits into
base: feature/test-chain-util-lxy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Unit Test Coverage

on:
push:
branches:
- feature/test-chain-util
pull_request:
branches:
- feature/test-chain-util

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: yarn install

- name: Run tests with coverage
run: yarn run test:coverage

- name: Generate coverage badge
uses: jaywcjlove/coverage-badges-cli@main
with:
source: coverage/coverage-summary.json
output: coverage/badges.svg

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./coverage
33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Publish
env:
CI: true
on:
push:
branches:
- feature/test-chain-util
tags:
- '!*'
jobs:
release:
name: Setup
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
registry-url: 'https://npm.pkg.github.com'

- name: Install dependencies
run: yarn install

- name: Publish to npm
run: |
npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN
npm publish || (echo "pnpm publish failed" && exit 1)
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108 changes: 108 additions & 0 deletions .github/workflows/test-badge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Test Badge

permissions:
contents: write

on:
push:
branches:
- dev
- master
- feature/test-chain-util

env:
BRANCH_NAME: 'feature/badge-json'

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:browser
- name: Install xmlstarlet
run: |
sudo apt-get update
sudo apt-get install -y xmlstarlet

- name: Extract test counts
run: |
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: 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
with:
github-token: ${{ secrets.COMMIT_TOKEN }}
script: |
const fs = require('fs');
const tests = "${{ env.TESTS }}";
const failures = "${{ env.FAILURES }}";
const errors = "${{ env.ERRORS }}";
const success = tests - failures;
const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen");
const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${success} success","color":"${color}"}`;

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: 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: "${{ env.FILENAME }}",
message: `Update ${{ env.FILENAME }}`,
content: Buffer.from(fileContent).toString('base64'),
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);
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
# aelf-sdk.js - AELF JavaScript API

| Statements | Branches | Functions | Lines |
<p>
<a href="https://nodejs.org/download/">
<img alt="Node version" src="https://img.shields.io/node/v/aelf-sdk.svg">
</a>
<img alt="NPM" src="https://img.shields.io/npm/l/aelf-sdk">
<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="https://github.com/AElfProject/aelf-web3.js/actions/workflows/publish.yml">
<img alt="coverage" src="https://github.com/AElfProject/aelf-web3.js/actions/workflows/publish.yml/badge.svg">
</a>
</p>

| Branch | Tests | Coverage |
|--------------|-----------------|----------------|
| `feature/test-chain-util` | ![Tests](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/aelf-web3.js/feature/badge-json/feature-test-chain-util-test-results.json) | ![Coverage](https://AElfProject.github.io/aelf-web3.js/badges.svg) |

<!-- | Statements | Branches | Functions | Lines |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](https://img.shields.io/badge/statements-96.16%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-92.62%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-98.61%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.1%25-brightgreen.svg?style=flat) |
| ![Statements](https://img.shields.io/badge/statements-96.16%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-92.62%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-98.61%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.1%25-brightgreen.svg?style=flat) | -->


## 1. Introduction
Expand Down
Loading
Loading