Skip to content

Commit 32e759f

Browse files
committed
badge
1 parent 452c465 commit 32e759f

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

.github/workflows/backend.yml

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ permissions:
1515
jobs:
1616
api_test:
1717
runs-on: ubuntu-latest
18+
1819
steps:
1920
- uses: actions/checkout@v4
2021

@@ -30,14 +31,40 @@ jobs:
3031

3132
- name: Run tests with coverage
3233
working-directory: MyMusicBoxApi
33-
run: go test -v -coverprofile=coverage.out ./...
34+
run: |
35+
go test -v -coverprofile=coverage.out ./...
36+
go tool cover -func=coverage.out > coverage.txt
3437
35-
- name: Show coverage summary
36-
working-directory: MyMusicBoxApi
37-
run: go tool cover -func=coverage.out
38+
- name: Post test coverage to PR
39+
uses: actions/github-script@v7
40+
with:
41+
script: |
42+
const fs = require('fs');
43+
const coverage = fs.readFileSync('MyMusicBoxApi/coverage.txt', 'utf8');
44+
const lines = coverage.trim().split('\n');
45+
const totalLine = lines[lines.length - 1];
46+
const percentage = totalLine.split('\t').pop();
47+
48+
const body = `### 🧪 Go Test Coverage\n\n\`\`\`\n${totalLine}\n\`\`\``;
49+
50+
github.rest.issues.createComment({
51+
issue_number: context.issue.number,
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
body: body
55+
});
56+
57+
- name: Generate coverage badge
58+
uses: ncruces/go-coverage-report@v0.4.1
59+
with:
60+
coverage-file: MyMusicBoxApi/coverage.out
61+
output: .github/badges/coverage.svg
62+
token: ${{ secrets.GITHUB_TOKEN }}
63+
# Optional: Add a GIST_ID secret if you want to publish the badge externally
64+
# gist: ${{ secrets.GIST_ID }}
3865

39-
- name: Upload coverage artifact
66+
- name: Upload coverage badge as artifact (optional)
4067
uses: actions/upload-artifact@v4
4168
with:
42-
name: coverage-report
43-
path: MyMusicBoxApi/coverage.out
69+
name: coverage-badge
70+
path: .github/badges/coverage.svg

0 commit comments

Comments
 (0)