Skip to content

Commit 0528b01

Browse files
AbigailDengAbigailDeng
authored andcommitted
feat: badge-json
1 parent 35eb825 commit 0528b01

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

.github/workflows/test-badge.yml

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
name: Test Badge
22

3+
permissions:
4+
contents: write
5+
36
on:
47
push:
58
branches:
69
- dev
710
- master
811
- feature/badge
912

13+
env:
14+
BRANCH_NAME: 'feature/badge-json'
15+
1016
jobs:
1117
test:
1218
name: Generate Test Badge
@@ -25,18 +31,14 @@ jobs:
2531
sudo apt-get update
2632
sudo apt-get install -y xmlstarlet
2733
28-
- name: Extract Test Counts
29-
id: test_counts
34+
- name: Extract test counts
3035
run: |
31-
tests=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")
32-
failures=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")
33-
errors=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")
34-
echo "TESTS=$tests" >> $GITHUB_ENV
35-
echo "FAILURES=$failures" >> $GITHUB_ENV
36-
echo "ERRORS=$errors" >> $GITHUB_ENV
36+
echo "TESTS=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")" >> $GITHUB_ENV
37+
echo "FAILURES=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")" >> $GITHUB_ENV
38+
echo "ERRORS=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")" >> $GITHUB_ENV
3739
38-
- name: Get branch name
39-
run: echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
40+
- name: Set file name
41+
run: echo "FILENAME=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-')-test-results.json" >> $GITHUB_ENV
4042

4143
- name: Prepare Content
4244
uses: actions/github-script@v5
@@ -47,55 +49,59 @@ jobs:
4749
const tests = "${{ env.TESTS }}";
4850
const failures = "${{ env.FAILURES }}";
4951
const errors = "${{ env.ERRORS }}";
50-
let branchName = "${{ env.BRANCH_NAME }}";
51-
branchName = branchName.replace(/\//g, '-');
52-
const filename = `${branchName}-test-results.json`;
5352
const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen");
5453
const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${failures} failures, ${errors} errors","color":"${color}"}`;
5554
56-
fs.writeFileSync(filename, content);
55+
fs.writeFileSync("${{ env.FILENAME }}", content);
5756
58-
- name: Commit and push
57+
- name: Check if file exists
5958
uses: actions/github-script@v5
6059
with:
61-
github-token: ${{ secrets.COMMIT_TOKEN }}
60+
github-token: ${{ secrets.GITHUB_TOKEN }}
6261
script: |
63-
const fs = require('fs');
64-
const path = require('path');
65-
let branchName = "${{ env.BRANCH_NAME }}";
66-
branchName = branchName.replace(/\//g, '-');
67-
const filename = `${branchName}-test-results.json`;
68-
const filePath = path.join(process.env.GITHUB_WORKSPACE, filename);
69-
const fileContent = fs.readFileSync(filePath, 'utf8');
70-
71-
let sha;
62+
let fileExists = false;
7263
try {
7364
const { data } = await github.rest.repos.getContent({
7465
owner: context.repo.owner,
7566
repo: context.repo.repo,
76-
path: filename,
77-
ref: 'feature/use-github-actions'
67+
path: "${{ env.FILENAME }}",
68+
ref: "${{ env.BRANCH_NAME }}",
7869
});
79-
sha = data.sha;
70+
fileExists = !!data;
8071
} catch (error) {
8172
if (error.status !== 404) {
8273
throw error;
8374
}
84-
// File does not exist, so we'll create it
8575
}
76+
core.exportVariable('FILE_EXISTS', fileExists);
77+
78+
- name: Create or update file
79+
uses: actions/github-script@v5
80+
with:
81+
github-token: ${{ secrets.COMMIT_TOKEN }}
82+
script: |
83+
const fs = require('fs');
84+
const path = require('path');
85+
const filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}");
86+
const fileContent = fs.readFileSync(filePath, 'utf8');
8687
8788
const params = {
8889
owner: context.repo.owner,
8990
repo: context.repo.repo,
90-
path: filename,
91-
message: `Update ${filename}`,
91+
path: "${{ env.FILENAME }}",
92+
message: `Update ${{ env.FILENAME }}`,
9293
content: Buffer.from(fileContent).toString('base64'),
93-
branch: 'feature/use-github-actions'
94-
};
94+
branch: "${{ env.BRANCH_NAME }}"
95+
};
9596
96-
// Only add sha to params if it is defined
97-
if (sha) {
98-
params.sha = sha;
99-
}
97+
if (${{ env.FILE_EXISTS }}) {
98+
const { data } = await github.rest.repos.getContent({
99+
owner: context.repo.owner,
100+
repo: context.repo.repo,
101+
path: "${{ env.FILENAME }}",
102+
ref: "${{ env.BRANCH_NAME }}"
103+
});
104+
params.sha = data.sha;
105+
}
100106
101107
await github.rest.repos.createOrUpdateFileContents(params);

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +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>
9-
<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">
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">
108
</p>
119

1210
## Descriptions

0 commit comments

Comments
 (0)