@@ -15,6 +15,7 @@ permissions:
1515jobs :
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