Skip to content

Commit bc17b9a

Browse files
authored
Merge pull request #79 from AElfProject/feature/badge
Feature/badge
2 parents 3478e3b + 2aa0332 commit bc17b9a

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

.github/workflows/test-badge.yml

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
name: Test Badge
22

3+
permissions:
4+
contents: write
5+
36
on:
47
push:
58
branches:
9+
- dev
610
- master
711

12+
env:
13+
BRANCH_NAME: 'feature/badge-json'
14+
815
jobs:
916
test:
10-
name: Coverage Diff
17+
name: Generate Test Badge
1118
runs-on: ubuntu-latest
1219

1320
steps:
@@ -23,18 +30,14 @@ jobs:
2330
sudo apt-get update
2431
sudo apt-get install -y xmlstarlet
2532
26-
- name: Extract Test Counts
27-
id: test_counts
33+
- name: Extract test counts
2834
run: |
29-
tests=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")
30-
failures=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")
31-
errors=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")
32-
echo "TESTS=$tests" >> $GITHUB_ENV
33-
echo "FAILURES=$failures" >> $GITHUB_ENV
34-
echo "ERRORS=$errors" >> $GITHUB_ENV
35+
echo "TESTS=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")" >> $GITHUB_ENV
36+
echo "FAILURES=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")" >> $GITHUB_ENV
37+
echo "ERRORS=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")" >> $GITHUB_ENV
3538
36-
- name: Get branch name
37-
run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
39+
- name: Set file name
40+
run: echo "FILENAME=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-')-test-results.json" >> $GITHUB_ENV
3841

3942
- name: Prepare Content
4043
uses: actions/github-script@v5
@@ -45,40 +48,59 @@ jobs:
4548
const tests = "${{ env.TESTS }}";
4649
const failures = "${{ env.FAILURES }}";
4750
const errors = "${{ env.ERRORS }}";
48-
let branchName = "${{ env.BRANCH_NAME }}";
49-
branchName = branchName.replace(/\//g, '-');
50-
const filename = `${branchName}-test-results.json`;
5151
const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen");
5252
const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${failures} failures, ${errors} errors","color":"${color}"}`;
5353
54-
fs.writeFileSync(filename, content);
54+
fs.writeFileSync("${{ env.FILENAME }}", content);
55+
56+
- name: Check if file exists
57+
uses: actions/github-script@v5
58+
with:
59+
github-token: ${{ secrets.GITHUB_TOKEN }}
60+
script: |
61+
let fileExists = false;
62+
try {
63+
const { data } = await github.rest.repos.getContent({
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
path: "${{ env.FILENAME }}",
67+
ref: "${{ env.BRANCH_NAME }}",
68+
});
69+
fileExists = !!data;
70+
} catch (error) {
71+
if (error.status !== 404) {
72+
throw error;
73+
}
74+
}
75+
core.exportVariable('FILE_EXISTS', fileExists);
5576
56-
- name: Commit and push
77+
- name: Create or update file
5778
uses: actions/github-script@v5
5879
with:
5980
github-token: ${{ secrets.COMMIT_TOKEN }}
6081
script: |
6182
const fs = require('fs');
6283
const path = require('path');
63-
let branchName = "${{ env.BRANCH_NAME }}";
64-
branchName = branchName.replace(/\//g, '-');
65-
const filename = `${branchName}-test-results.json`;
66-
const filePath = path.join(process.env.GITHUB_WORKSPACE, filename);
84+
const filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}");
6785
const fileContent = fs.readFileSync(filePath, 'utf8');
6886
69-
const { data: { sha } } = await github.rest.repos.getContent({
87+
const params = {
7088
owner: context.repo.owner,
7189
repo: context.repo.repo,
72-
path: filename,
73-
ref: 'feature/use-github-actions'
74-
});
75-
76-
await github.rest.repos.createOrUpdateFileContents({
77-
owner: context.repo.owner,
78-
repo: context.repo.repo,
79-
path: filename,
80-
message: `Update ${filename}`,
90+
path: "${{ env.FILENAME }}",
91+
message: `Update ${{ env.FILENAME }}`,
8192
content: Buffer.from(fileContent).toString('base64'),
82-
sha,
83-
branch: 'feature/use-github-actions'
84-
});
93+
branch: "${{ env.BRANCH_NAME }}"
94+
};
95+
96+
if (${{ env.FILE_EXISTS }}) {
97+
const { data } = await github.rest.repos.getContent({
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
path: "${{ env.FILENAME }}",
101+
ref: "${{ env.BRANCH_NAME }}"
102+
});
103+
params.sha = data.sha;
104+
}
105+
106+
await github.rest.repos.createOrUpdateFileContents(params);

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
<p align="center">
44
<img alt="Node" src="https://img.shields.io/badge/node->=10.9.0-brightgreen">
55
<img alt="NPM" src="https://img.shields.io/npm/l/aelf-command">
6-
<a href="http://commitizen.github.io/cz-cli/">
7-
<img alt="Commitizen friendly" src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg">
8-
</a>
6+
<a href="http://commitizen.github.io/cz-cli/"><img alt="Commitizen friendly" src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg"></a>
7+
<img alt="TESTS" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/eanzhao/aelf-command/feature/badge-json/master-test-results.json">
98
</p>
109

1110
## Descriptions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-command",
3-
"version": "0.1.47-beta.7",
3+
"version": "0.1.47-beta.8",
44
"description": "A CLI tools for AElf",
55
"main": "src/index.js",
66
"type": "module",

0 commit comments

Comments
 (0)