Skip to content

Commit 3da788a

Browse files
authored
Merge pull request #3 from glideapps/make-and-upload-badges
Make and upload badges
2 parents 1da6055 + 1f45ae0 commit 3da788a

25 files changed

+1485
-152
lines changed

.github/workflows/example.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Example Coverage Workflow
2+
on:
3+
pull_request:
4+
branches: [main]
5+
push:
6+
branches: [main]
7+
8+
permissions:
9+
pull-requests: write
10+
contents: write # Required for GitHub Pages upload
11+
12+
jobs:
13+
coverage:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: "20"
22+
23+
- name: Install dependencies
24+
run: npm ci
25+
26+
- name: Run tests with coverage
27+
run: npm test
28+
29+
- name: Report Coverage
30+
uses: ./
31+
with:
32+
coverage-file: "coverage/coverage-summary.json"
33+
title: "🧪 Test Coverage Report"
34+
show-files: "true"
35+
coverage-threshold: "80"
36+
make-badges: "true"
37+
38+
# Upload badges to GitHub Pages (only on main branch)
39+
- name: Upload Badges to GitHub Pages
40+
if: github.ref == 'refs/heads/main'
41+
uses: ./
42+
with:
43+
action: "badge-upload-action.yml"
44+
badges-dir: "badges"
45+
pages-branch: "gh-pages"
46+
pages-badges-dir: "badges"

ACTION_README.md

Lines changed: 143 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Vitest Coverage Reporter GitHub Action
22

3-
A GitHub Action that automatically creates beautiful coverage reports in pull requests using vitest coverage data.
3+
A GitHub Action that automatically creates beautiful coverage reports in pull requests using vitest coverage data. Built with pure YAML and shell commands for maximum reliability.
44

55
## Features
66

@@ -9,7 +9,9 @@ A GitHub Action that automatically creates beautiful coverage reports in pull re
99
- 📁 **File-level Details**: Option to show individual file coverage breakdowns
1010
- 🔄 **Smart Updates**: Updates existing comments instead of creating duplicates
1111
- 🎨 **Visual Indicators**: Uses emojis to quickly identify coverage status
12-
-**Lightweight**: Fast execution with minimal dependencies
12+
- 🏷️ **Coverage Badges**: Generates shields.io compatible badges locally
13+
-**Lightweight**: Pure YAML and shell commands, no JavaScript complexity
14+
- 🔧 **Reliable**: No module system issues or permission headaches
1315

1416
## Usage
1517

@@ -45,16 +47,19 @@ jobs:
4547
uses: glideapps/vitest-v8-json-coverage-summary@v1
4648
```
4749
48-
### Advanced Usage
50+
### Advanced Usage with Badge Upload
4951
5052
```yaml
51-
name: Coverage Report
53+
name: Coverage Report with Badge Upload
5254
on:
5355
pull_request:
5456
branches: [main]
57+
push:
58+
branches: [main]
5559

5660
permissions:
5761
pull-requests: write
62+
contents: write # Required for GitHub Pages upload
5863

5964
jobs:
6065
coverage:
@@ -73,24 +78,56 @@ jobs:
7378
- name: Run tests with coverage
7479
run: npm test
7580

81+
# Coverage reporting (runs on PRs and main)
7682
- name: Report Coverage
7783
uses: glideapps/[email protected]
7884
with:
7985
coverage-file: "coverage/coverage-summary.json"
8086
title: "🧪 Test Coverage Report"
8187
show-files: "true"
8288
coverage-threshold: "90"
89+
make-badges: "true"
90+
91+
# Badge upload (only runs on main branch)
92+
upload-badges:
93+
if: github.ref == 'refs/heads/main'
94+
runs-on: ubuntu-latest
95+
needs: coverage
96+
steps:
97+
- uses: actions/checkout@v4
98+
with:
99+
fetch-depth: 0 # Needed for git operations
100+
101+
- name: Upload Badges to GitHub Pages
102+
uses: glideapps/[email protected]
103+
with:
104+
action: "badge-upload-action.yml"
105+
badges-dir: "badges"
106+
pages-branch: "gh-pages"
107+
pages-badges-dir: "badges"
83108
```
84109
85110
## Inputs
86111
87-
| Input | Description | Required | Default |
88-
| -------------------- | ------------------------------------------------------- | -------- | -------------------------------- |
89-
| `coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
90-
| `token` | GitHub token for creating comments | No | `${{ github.token }}` |
91-
| `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
92-
| `show-files` | Whether to show individual file coverage details | No | `true` |
93-
| `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
112+
| Input | Description | Required | Default |
113+
| -------------------- | --------------------------------------------------------- | -------- | -------------------------------- |
114+
| `coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
115+
| `token` | GitHub token for creating comments | No | `${{ github.token }}` |
116+
| `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
117+
| `show-files` | Whether to show individual file coverage details | No | `true` |
118+
| `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
119+
| `make-badges` | Whether to generate coverage badges in a badges directory | No | `true` |
120+
121+
## Badge Upload Action
122+
123+
For uploading badges to GitHub Pages, use the separate `badge-upload-action.yml`:
124+
125+
| Input | Description | Required | Default |
126+
| ------------------ | ------------------------------------------------- | -------- | ---------------------------------- |
127+
| `badges-dir` | Directory containing the badges to upload | No | `badges` |
128+
| `pages-branch` | Branch to upload badges to for GitHub Pages | No | `gh-pages` |
129+
| `pages-badges-dir` | Directory within the pages branch to store badges | No | `badges` |
130+
| `commit-message` | Commit message for badge updates | No | `Update coverage badges [skip ci]` |
94131

95132
## Coverage File Format
96133

@@ -182,3 +219,98 @@ Contributions are welcome! Please feel free to submit a Pull Request.
182219
## License
183220

184221
MIT License - see the [LICENSE](LICENSE) file for details.
222+
223+
## Coverage Badges
224+
225+
When `make-badges` is enabled (default: `true`), the action creates a `badges` directory with shields.io compatible JSON files:
226+
227+
### Generated Badges
228+
229+
- `badges/coverage.json` - Overall coverage percentage
230+
- `badges/statements.json` - Statement coverage
231+
- `badges/branches.json` - Branch coverage
232+
- `badges/functions.json` - Function coverage
233+
- `badges/lines.json` - Line coverage
234+
235+
### Badge Colors
236+
237+
- 🟢 **Bright Green**: 90% or higher
238+
- 🟢 **Green**: 80-89%
239+
- 🟡 **Yellow**: 70-79%
240+
- 🟠 **Orange**: 60-69%
241+
- 🔴 **Red**: Below 60%
242+
243+
### Using Badges with GitHub Pages
244+
245+
The badge upload action automatically uploads badges to your GitHub Pages branch.
246+
247+
**Automatic Setup:**
248+
249+
- Creates the `gh-pages` branch if it doesn't exist
250+
- Uploads badges to the specified directory
251+
- Commits and pushes changes automatically
252+
253+
**Enable GitHub Pages** in your repository settings:
254+
255+
1. Go to Settings → Pages
256+
2. Set source to "Deploy from a branch"
257+
3. Select your `gh-pages` branch (or the branch specified in `pages-branch`)
258+
4. Set folder to `/ (root)` or `/badges` depending on your preference
259+
260+
**Badge URLs** will be available at:
261+
262+
```
263+
https://yourusername.github.io/yourrepo/badges/coverage.json
264+
https://yourusername.github.io/yourrepo/badges/statements.json
265+
https://yourusername.github.io/yourrepo/badges/branches.json
266+
https://yourusername.github.io/yourrepo/badges/functions.json
267+
https://yourusername.github.io/yourrepo/badges/lines.json
268+
```
269+
270+
**Use in your README.md:**
271+
272+
```markdown
273+
![Coverage](https://yourusername.github.io/yourrepo/badges/coverage.json)
274+
![Statements](https://yourusername.github.io/yourrepo/badges/statements.json)
275+
![Branches](https://yourusername.github.io/yourrepo/badges/branches.json)
276+
```
277+
278+
### Required Permissions
279+
280+
For basic functionality, your workflow needs:
281+
282+
```yaml
283+
permissions:
284+
pull-requests: write # For creating/updating PR comments
285+
```
286+
287+
For GitHub Pages deployment, add:
288+
289+
```yaml
290+
permissions:
291+
contents: write # For uploading to GitHub Pages
292+
```
293+
294+
## How It Works
295+
296+
The action is built as a **composite action** using pure YAML and shell commands:
297+
298+
1. **File Validation** - Checks if coverage file exists
299+
2. **Badge Generation** - Creates shields.io compatible JSON badges
300+
3. **Report Generation** - Builds formatted markdown coverage report
301+
4. **PR Comment** - Posts report to pull request using GitHub API
302+
303+
### Shell Tools Used
304+
305+
- **`jq`** - JSON parsing and data extraction
306+
- **`bc`** - Mathematical calculations
307+
- **`curl`** - GitHub API communication
308+
- **`bash`** - Scripting and logic
309+
310+
### No More JavaScript Headaches
311+
312+
- ✅ No ESM/CJS confusion
313+
- ✅ No module system warnings
314+
- ✅ No permission issues with git operations
315+
- ✅ No complex dependency management
316+
- ✅ No build/compilation steps

0 commit comments

Comments
 (0)